Learning SQL, and was given an assignment to use a single table of students and show the ones that are roommates, selecting by last name. And then the output should be the selected last name ("smith"), and that student's home address, and then the roommate's name and the roommate's home address.
I found this post here to be quite helpful for setting up my recursion:
SQL Recursive query over same table
My problem is figuring out how to get that "static" information, the selected starting person's name and address, into the output.
Data table is titled college (I am ignoring the quotes that should be on the strings...)
StuID
lastName
firstName
homeAdd
dormRoom
1
Johnson
John
237 Maple St
b101
2
Smith
Linda
345 Juniper St
b101
3
Levi
Grant
789 SE 54th St
a304
4
Sousa
Susi
1024 NE 76th Pl
b101
Here is the code:
WITH RECURSIVE roommates AS
(SELECT lastName, firstName, homeAdd, dormRoom
FROM college WHERE lastName = 'Smith'
UNION
SELECT c.lastName, c.firstName, c.homeAdd, c.dormRoom
FROM roommates r
JOIN college c ON c.dormRoom = r.dormRoom
)
SELECT firstName || ' ' || lastName AS Roommate, homeAdd, dormRoom
FROM roommates
The problem is that I need each line to be basically:
Smith 345 Juniper St John Johnson 237 Maple St
Smith 345 Juniper St Susi Sousa 1024 NE 76th Pl
Thoughts?
It doesn't need to use recursive SQL in your case, you should use LEFT JOIN to solve your problem.
SELECT (c1.firstName || ' ' || c1.lastName) AS student,
c1.homeAdd AS studentAddress,
c1.dormRoom,
(c2.firstName || ' ' || c2.lastName) AS roommate,
c2.homeAdd AS roommateAddress
FROM college c1
LEFT JOIN college c2 ON c1.dormRoom = c2.dormRoom AND c1.StuID <> c2.StuID
WHERE c1.lastName LIKE 'Smith%';
Demo
Related
Get all Users from first table that have any word matching in another table;
I have Users table that contain, among others, column with FullName, that column is Full Text Search Indexed. Sometimes first "word" is name, sometimes first "word" is surname from the FullName column;
Like John Smith or Smith John.
And another table that have just local firstname.
I want to get all Users that have matching local name.
Users Table:
John Smith
Rebecca Mark
Maria Anna
Lance Maria
Emilia Clark
Snow John
Natalie Butler
Name Table:
Maria
Smith
Result of Query:
John Smith
Maria Anna
Lance Maria
Snow John
I can do only single Name with Contains function.
SELECT * FROM Users WHERE CONTAINS(FullName, 'John');
But I need it for each row in Name Table.
Every row from FullName contains any of Name Table... But in SQL Query.
To avoid a case where you are search for 'Maria' and the matching name is 'Marianne', check for 2 conditions: (1) the name is at the start or (2) at the end of FullName:
(1):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
u.FullName LIKE concat(n.name, ' %')
OR
u.FullName LIKE concat('% ', n.name)
or (2):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
concat(' ', u.FullName, ' ') LIKE concat('% ', n.name, ' %')
use join and like for matching
select u.* from table_users u join table_name b on
u.users like concat('%',b.name,'%')
You can use exists for this:
select u.*
from users u
where exists (select 1
from nametable n
where u.fullname like '%' + n.name + '%'
);
If you want to avoid partial matches on names, then take the delimiters into account:
where exists (select 1
from nametable n
where ' ' + u.fullname + ' ' like '% ' + n.name + ' %'
);
Consider these 2 tables:
CustAddress
Name Address
---- -------
John 116 States Ave
Mike 404 16th Street
CustPhone
Name Phone
---- -------
John 342-345-456
Smith 678-435-567
How do I combine these into a single result like this in Oracle:
Customer info with Address and Phone:
Name Phone Address
---- ------- -------
John 342-345-456 116 States Ave
Smith 678-435-567
Mike 404 16th Street
Full Outer Join doesn't help as I end up losing some data.
Here is a SQL Fiddle to play with:
http://sqlfiddle.com/#!9/ebb9b/1
Using a full join.
select coalesce(ca.name,cp.name) as name, cp.phone, ca.address
from custaddress ca
full join custphone cp on ca.name=cp.name
Or using union all assuming there is at most one record per name in either of the tables.
select name,max(phone) as phone,max(address) as address
from (select name,phone,null as address from custphone
union all
select name,null,address from custaddress
) x
group by name
Kinda ugly but
select Names.Name, CustAddress.Address, CustPhone.Phone from
(select Name from CustAddress union select Name from CustPhone) as Names
left join CustAddress on Names.Name = CustAddress.Name
left join CustPhone on Names.Name = CustPhone.Name;
from this table..
Employees Table
Question: List the employees who report to those employees who report to Diane Murphy. Use the CONCAT function to combine the employee's first name and last name into a single field for reporting.
My non-working query..
SELECT employeeNumber, CONCAT(firstName, ' ', lastName), reportsTo
FROM Employees
WHERE reportsto= 1002
AND WHERE (SELECT CONCAT(firstName, ' ', lastName), reportsTo
FROM employees
WHERE reportsTo= 1056 AND 1076);
I am really confused by this question to be honest.
1002= Diane Murphy
1056= Mary Patterson
1076= Jeff Firrelli
Based on my understanding, im trying to query this based on a hierarchal system.
I know that both Mary and Jeff report to Diane Murphy and i can look at the table to see who reports to Mary and Jeff, but im lost exactly i go about querying this.
Any help would be appreciated!
Thank you.
SELECT A.employeeNumber, CONCAT(A.firstName, ' ', A.lastName), A.reportsTo
FROM Employees A
INNER JOIN Employees B ON A.reportsTo = B.employeeNumber
WHERE B.reportsto = 1002
Alias B and condition will fetch employees reports to 1002 & thus you will get employees who reports to 1056 & 1076.
This should do what you want:
select e1.employeeNumber, CONCAT(e1.firstName, ' ', e1.lastName), e1.reportsTo
from Employees as e1
inner join Employees as e2 on e1.reportsTo = e2.employeeNumber
where e2.reportsTo = 1002
I am using Oracle SQL (TeraTerm),
and
I am trying to join specific information from two tables CONSULTANT, and PROJECT_CONSULTANT, and I need to retrieve only the employees who worked over 40 hours. Here are the tables
Project Consultant
PROJECT_ID CONSULTANT_ID NUMBER_HOURS
--------------- --------------- ------------
94738949 49620928 6
45699847 34879223 57
45699847 95928792 44
45699847 04875034 59
19870398 49620928 32
30495394 95928792 57
30495394 07811473 50
62388923 07811473 82
and Consultant
CONSULTA NAME ZIP START_DT
-------- -------------------------------- ----- ---------
CON_TITLE
-------------------------
49620928 Tom Jones 39875 01-SEP-98
Junior Consultant
04875034 Jack Johnson 29087 05-OCT-93
Manager
34879223 Lanny Harris 03944 30-APR-04
Principal
CONSULTA NAME ZIP START_DT
-------- -------------------------------- ----- ---------
CON_TITLE
-------------------------
95928792 Michael Johnson 02953 22-JUN-02
Senior Manager
07811473 Wendy Adams 29087 05-JUL-05
Senior Consultant
The code I came up with is
select Consultant_ID, Name, Zip, and Number_Hours
from Consultant
Inner Join project_consultant
ON Consultant.Consultant_ID=project_consultant.Consultant_ID
WHERE project_consultant.number_Hours>40;
I am getting an error
ERROR at line 1:
ORA-00936: missing expression
I just wanna know how to write the join statement correctly any help would be awesome, because I am having trouble knowing how to fix this join statement
You don't use and in the select clause:
select c.Consultant_ID, c.Name, c.Zip, pc.Number_Hours
from Consultant c Inner Join
project_consultant pc
on c.Consultant_ID = pc.Consultant_ID
where pc.number_Hours > 40;
You also need a table alias in the select clause to be clear what table Consultant_Id refers to.
EDIT:
You might actually want to sum the hours for employees. If so, you need an aggregation:
select c.Consultant_ID, c.Name, c.Zip, sum(pc.Number_Hours)
from Consultant c Inner Join
project_consultant pc
on c.Consultant_ID = pc.Consultant_ID
group by c.Consultant_ID, c.Name, c.Zip
having sum(pc.number_Hours) > 40;
You can't use and in Select Clause
Try this
SELECT C.Consultant_ID, C.Name, C.ip, PC.Number_Hours
FROM Consultant C
INNER Join project_consultant PC
ON C.Consultant_ID=PC.Consultant_ID
WHERE PC.number_Hours > 40;
select c.Consultant_ID, c.Name, c.Zip, p.Number_Hours
from Consultant c
Inner Join project_consultant p
ON c.Consultant_ID=p.Consultant_ID
WHERE p.number_Hours>40;
I am revising for my exam and I am a bit struggling with SQL queries..
Task for me is:
write a SQL query to list the full names of all the clients that
attend Allen Moore‟s classes, in alphabetical order of surname (i.e.
Lname column)
Client
Cid Fname Lname Fitness
C129 Julie Summer 1
C525 Max Hedrum 3
C628 John Long 3
C772 Warren Peace 2
C829 Anna Heart 2
Programme
Code Title Fitness
AR02 Aerobics 2
EN99 Endurance 3
TU10 Tune-Up 1
UB01 Upper-Body 2
YG02 Yoga 1
Staff
Sid Fname Lname Position Salary
S09 Jenny Sayer Psychologist 23500
S22 Allen Moore Instructor 21500
S28 Polly Purves Instructor 19000
S35 Jim Shoe Instructor 18000
S55 Mark Spencer Manager 25500
Class
Code Sid Cid
AR02 S35 C772
EN99 S22 C525
TU10 S35 C129
UB01 S28 C628
YG02 S22 C829
YG02 S22 C12
Is it something like:
SELECT Cliente.Fname, Cliente.Lname
FROM Staff, Class, Cliente
WHERE Staff.Sid = Class.Sid AND Staff.Fname = "Allen" AND Staff.Lname = "Moore
AND Class.Cid = Cliente.Cid
GROUP BY Cliente.lName ASC;
Thanks!
select c.fname + ' ' + c.lname
from Client c
inner join Class cl
on c.cid = cl.cid
inner join Staff s
on s.sid = cl.sid
where s.fname = 'allen'
and s.lname = 'moore'
order by c.lname
Using different aliases and a more popular convention for JOINs, but your query should also work. Also, ORDER BY ascending is default, so ASC is optional...
SELECT CLNT.Fname, CLNT.Lname
FROM Class AS CLS
INNER JOIN Client AS CLNT ON CLS.Cid = CLNT.Cid
INNER JOIN Staff AS S ON CLS.Sid = S.Sid
WHERE S.Fname = 'Allen'
AND S.Lname = 'Moore'
ORDER BY CLNT.Lname;
The GROUP BY in your query has to go, you don't have to aggregate anything. I would prefer the SQL92 syntax for a join:
SELECT
Client.Fname, Client.Lname
FROM
Staff
INNER JOIN
Class ON Staff.Sid = Class.Sid AND Staff.Fname = "Allen" AND Staff.Lname = "Moore"
INNER JOIN
Client ON Class.Cid = Cliente.Cid
ORDER BY Cliente.lName ASC;
Try brushing up on your use of the JOIN...ON clause.
http://www.w3schools.com/sql/sql_join_inner.asp
Since none of the information from other tables is actually being used, why JOIN at all?
SELECT FName, LName
FROM Client
WHERE
Cid IN (
SELECT Cid
FROM Class
WHERE Sid = (
SELECT Sid FROM Staff WHERE FName = 'Allen' AND LName = 'Moore'
)
)
ORDER BY LName ASC;