SQL query to pull data from three different tables - sql

I have to create a SQL query to list all the Nurses in the ‘Sparrow’ Wing ordered by last name, first name.
However, I need to pull Nurse_name and Nurse_surname from the Nurse table, which is linked to another table called Sister by the foreign key Sister_ID, this table is then linked to another table called Wing which has the foreign key Sister_ID.
The nurse is managed by the sister and the sister manages the wing.
Can anyone help me with an SQL query for this? As it is, I am only able to get the data from nurse and sister tables.

Since you seem to be aware that you should use the inner join to connect tables (but apparently not that the connection needs to be via the related columns) you should apply that knowledge to connect all the tables you need to answer the query.
If you start at the end result and work your way backwards you first chose the columns you need:
select Nurse.Nurse_name, Nurse.Nurse_surname
and then as they belong to the Nurse table you use that as source in the from clause
from Nurse
to get the Wing you need to connect the Sister table, but to connect that and Nurse you first need the SisterNurse table joined on the shared attribute
join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID
now you can join Sister on the attribute shared with SisterNurse
join Sister on Sister.Sister_ID = SisterNurse.Sister_Id
and finally you can join Wing
join Wing on Wing.sister_ID = Sister.Sister_ID
limit the Wings to the one names 'Sparrow'
where Wing.Wing_Name = 'Sparrow'
and order the data
order by Nurse.Nurse_surname, Nurse.Nurse_name
Put it all together and you get:
select Nurse.Nurse_name, Nurse.Nurse_surname
from Nurse
join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID
join Sister on Sister.Sister_ID = SisterNurse.Sister_Id
join Wing on Wing.sister_ID = Sister.Sister_ID
where Wing.Wing_Name = 'Sparrow'
order by Nurse.Nurse_surname, Nurse.Nurse_name

You don't give much information about the schema involved but maybe this will help:
select
n.Nurse_name
, n.Nurse_surname
, w.Wing_name
, managing_nurse.Nurse_name
, managing_nurse.surname
from
Nurse n
join Sister s on n.Sister_ID=n.Sister_ID
join Wing w on s.Sister_ID=w.Sister_ID
join Nurse managing_nurse on w.Nurse_Manager_ID=managing_nurse.Sister_ID

A simple way is to use 'union':
select * from (
select nurse_name, nurse_surname, 'nurse' as nurse_type from nurse_table
union
select nurse_name, nurse_surname, 'sister' as nurse_type from syster join nurse_table on nurse_table.syster_id = syster.syster_id
union
select nurse_name, nurse_surname, 'wing' as nurse_type from wing join syster on wing.syster_id = syster.syster_id join nurse_table on nurse_table.syster_id = syster.syster_id )
order by nurse_surname, nurse_name
Hope this help you!
P.S.
I assume that syster and wing tables have nurse_name and nurse_surname fields.
If not you have to give the same alias to all the selected columns

Related

Complex SQL Query with Dead Ends and 7 Tables

I was tasked with creating a complex query that incudes all of the data from all of the tables minus the Keys. I am having an issue with the dead end tables and how to circle back around to include the data of the connecting table. I need to select columns DivisionName, ProgramName, ProgramChairFName, ProgramChairLName, CourseID, OutcomeDescription from the listed tables.
SQL Diagram
The 'dead-ends' aren't really dead-ends. When you join all the tables by the appropriate keys, you'll get an assembly of the information you want.
Consider a really simple example:
table person
id name
1 Alice
table pet
id person_id animal
1 1 cat
table hobby
id person_id activity
1 1 dancing
Here, the two tables pet and hobby link to the person table via the person_id key.
In your thinking, "pet" could be considered a "dead-end" because it doesn't link to hobby. But it doesn't need to. The query:
SELECT name, animal, activity
FROM person
JOIN pet ON person.id = pet.person_id
JOIN hobby ON person.id = hobby.person_id;
creates the correct joins back to the person table. It's not a linear path (person -> pet -> hobby). The nature of the joins are specified by the "ON" part of the query. You can see this simple example works here: http://sqlfiddle.com/#!9/02c94b/1
So, in your case, you can have a series of JOINs:
SELECT [all the columns you want]
FROM Division d JOIN Program p
ON d.DivisionKey = p.DivisionKey
JOIN ProgramChairMap pcm
ON p.ProgramKey = pcm.ProgramKey
JOIN ProgramChair pc
ON pcm.ProgramChairKey = pc.ProgramChairKey
JOIN Course c
ON p.ProgramKey = c.ProgramKey
JOIN CourseOutcome co
ON c.CourseKey = co.CourseKey
JOIN Outcome o
ON co.OutsomeKey = o.OutcomeKey

How to link tables correctly in SQL to add roles to staff?

Currently I have a staff table with columns:
Staff_Id, first_name, Surname.
My second table is:
Id, management_role.
When I link the tables each staff member gets added to every management role. So for example a person in first table called Jim is added three times as manager, supervisor, intern and this happens for every staff.
Some things to consider that are your ID columns are primary keys for their respective tables. If not are every value in the column is unique? Also are ids not
From your description you might be using a cross join here. The thing you need is inner join so it joins the matching id's together.
So you can do
SELECT *
FROM staff_table as st
INNER JOIN management_table as mt
ON st.Staff_Id = mt.ID

How to retrieve students data based on teachers ID

According to a simple scenario ER diagram as follows.
Based on that, I developed a database mapping as follows.
In there, orange color columns shows the primary key of the tables and yellow color columns shows foreign key of the tables. Is this mapping correct? Now my problem is I need to retrieve students data who learned from some teacher, which means need to retrieve some teacher's sudents who learned from his/her.
You need to learn about how to join tables using different types of available JOINs.
For your scenario , try like following, it will give you Student and the Teacher mapping.
select s.*
tbl_student s
inner join tbl_Course_Subjects tcs on s.Course_Id= tcs.Course_Id
inner join tbl_Subjects_Teacher tst on tst.SubjectId=tcs.Subject_Id
inner join tbl_Teacher t on t.Teacher_Id=tst.Teacher_Id
where t.Teacher_Id = #SomeTeacherId

Parent child relationship Join tables in oracle sql

I have a requirement below.
Now I have to get output like below
How can this be achieved ?
I have written the below SQL but parent_position_id is coming, not parent_position_code
select
hapf.position_code,
pphf.parent_position_id
from
hr_all_positions_f hapf, PER_POSITION_HIERARCHY_F pphf
where
hapf.position_id = pphf.position_id
Should I write a sub query? How should I proceed ?
This is Oracle SQL
Thanks,
Shivam
Noone ever said you could only join a table in once:
select
chi.position_code,
par.position_code as parent_position_code
from
hr_all_positions_f hapf
INNER JOIN PER_POSITION_HIERARCHY_F chi on hapf.position_id = chi.position_id
INNER JOIN PER_POSITION_HIERARCHY_F par on hapf.parent_position_id = par.position_id
Bear it in mind; I see people coming to thinking all the time that they can only join a table once. If one table decodes a value in 3 different columns, then you sure can join that same table in 3 times... Imagine if it were an address table, and a Student had a HomeAddressId, WorkAddressId and StudyAddressId, and the Address table held all these addresses - you'd join the addresses table to the Student table 3 times to get all the data..

SQL fetch multiple values on join

Hi I have an SQL table which has two tables which make reference to the same foreign key in a separate table twice... something like
SALES table
idSales idClient1 idClient2
1 1 2
CLIENT table
idClient ClientName
1 Bob
2 Mick
I want to join the SALES table to the CLIENT table and return data as follows:
idSales idClientClientName1 idClientClientName2
1 Bob Mick
Can anyone help with the SQL for this? I'm getting ambiguous column name errors on my join.
Thank you
You need to basically join table Client on table Sales twice because there are two columns on table Sales that are dependent on table Client.
SELECT a.idSales,
b.ClientName ClientName1,
c.ClientName ClientName2
FROM Sales a
INNER JOIN Client b
ON a.idClient1 = b.idClient
INNER JOIN Client c
ON a.idClient2 = c.idClient
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But when one of the columns or both columns are nullable, INNER JOIN will not give you all records from Sales because it will only select where it has atleast one match on the other table. Instead use LEFT JOIN.
I might add that in cases like this, I use table aliases that hint at what entity you are linking to in the joined table. If for example, the foreign keys were to an address table, and you had a work address, and a Home address, I would use tables aliases of h and w for the two joins. In your case, i.e.,
Selext s.idSales,
c1.ClientName ClientName1,
c2.ClientName ClientName2
From Sales s
Join Client c1
On c1.idClient = s.idClient1
Join Client c2
On c2.idClient = s.idClient2
For those beginner SQL folks who may see this question in the future, it's helpful to add in the AS words, it makes it clearer still:
SELECT
Sale.idSales,
c1.ClientName AS ClientName1,
c2.ClientName AS ClientName2
FROM
Sales AS Sale
INNER JOIN Client AS c1 ON Sale.idClient1 = c1.idClient
INNER JOIN Client AS c2 ON Sale.idClient2 = c2.idClient