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
Related
im using SQL and i have three tables: Owner, PetType and PetAndOwner. i want to list all the people in the owner table who own a dog. these are the tables and their attributes
https://imgur.com/a/FJRJlsU
the trouble that im having is that i cant seem to find the right code to use to be able to find out which owners have dogs because you have to get the data from the PetAndOwner table and then join it back with the other tables to print the data.
my final result should show all owners names who have a dog
There seems to be a missing table in your query, so assuming that table holds details of a pet:
SELECT Owner.FirstName,Owner.LastName,Pet.Name
FROM PetType
JOIN Pet ON Pet.PetTypeId = 1 AND PetType.PetTypeId = Pet.PetTypeId
JOIN PetAndOwner ON Pet.PetId = PetAndOwner.PetId
JOIN Owner ON Owner.ownerId = PetAndOwner.ownerId
Here is a Demo
Ok, I've got brain cells melting at an alarming rate on this SQL statement. Not my database, but I've been tasked with extracting data. So here's what I'm dealing with...
It is medical data. We have a database where ALL of the people are listed in one table- patients, as well as doctors. Each person has a unique PersonID. Let's just start with the Person table:
Person:
PersonID, PersonType, LastName, FirstName
I have another table that is hospital admissions.
Admissions:
AdmissionID, PersonID and PrimaryMD
where the Primary MD is the same as the Person ID for a doctor.
I need to extract each Admission, with the last name, and then the first name of the patient, but then I need to go back, based on the PrimaryMD identifier and use that value to pull the last name and first name of the doctor so that my results look like:
Admission | PatientLastName | PatientFirstName | DoctorLastName | DoctorFirstName
Ultimately, I'll need to pull address information for both the patient, and the doctor which is all stored in an address table with the same PersonID as in the person table, and then pull the doctor's address using the primarymd against the person table. But I can't figure how to write two queries in the same statement against these similar columns. I tried using aliases, and some left and inner joins and even a union, but I can't seem to get things right.
Any assistance would be hugely appreciated.
Try this:
SELECT
a.AdmissionID,
pat.LastName AS PatientLastName,
pat.FirstName AS PatientFirstName,
doc.LastName AS DoctorLastName,
doc.FirstName AS DoctorFirstName
FROM Admissions a
INNER JOIN Person pat
ON a.PersonID = pat.PersonID
INNER JOIN Person doc
ON a.PrimaryMD = doc.PersonID
For getting addresses use the same steps:
SELECT
a.AdmissionID,
pat.LastName AS PatientLastName,
pat.FirstName AS PatientFirstName,
doc.LastName AS DoctorLastName,
doc.FirstName AS DoctorFirstName
FROM Admissions a
INNER JOIN Person pat
ON a.PersonID = pat.PersonID
INNER JOIN Person doc
ON a.PrimaryMD = doc.PersonID
INNER JOIN Addresses addPat
ON pat.PersonID = addPat.PersonID
INNER JOIN Addresses addDoc
ON doc.PersonID = addDoc.PersonID
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
I'm having an issue creating a query. Here are the specifics.
There are 2 tables company_career and company_people.
People contains person information (Name, Address, etc) and Career contains historical career information (job_title, department, etc.)
People is linked to Career by job_ref_id.
Direct_Report_id lies in the career table and contains a unique id that correlates to job_ref_id.
Example: job_ref_id = '1' results in direct_report_id ='A'. I then use the value produced from direct-report_id (i.e., 'A') and query the job_ref_id = 'A' and this produces the employee name. Since it produces the employee name (which is actually the manager) I need to know how I would query this to present this as the manager name.
I think I know what you are looking for, you just need to use joins and aliases. For example:
SELECT
cp.name AS [EmployeeName],
cp.address AS [EmployeeAddress],
cc.job_title AS [EmployeeTitle],
cc.department AS [EmployeeDept],
m.name AS [ManagerName]
FROM company_people cp
LEFT JOIN company_career cc ON cc.job_ref_id = cp.job_ref_id
LEFT JOIN company_people m ON m.job_ref_id = cc.direct_report_id
Result needed: The registration number of the cars used by Instructors at the Glasgow, Bearsden office.
The 3 relevant tables I have are; ( I have omitted the non-relevant table information, to simplify this. The database is called easydrive.
No this is not a School or UNI assignment, it is a question from a text book, that we have been given to do so we can learn how to do it, no marks are awarded for this, so you are not doing my homework for me.
**++Staff++**
(PK) Staff ID
(FK) Address ID
(FK) Office
(FK) Car Allocation Number
First Name
Last Name
Position/Title
Office
**++CarAllocation++**
(PK) Car Allocation Number
(FK) Staff ID
(FK) Car ID
**++Car++**
(PK) Car ID
Car Rego
So I need to so a join I think and I think it needs to go something along these lines but am very confused.
SELECT car.rego
FROM car
WHERE staff.office=’Glasgow’ OR ‘Bearsden’
Can someone please fill in the blanks so I can learn how to perform this, do I need to make a new table?
You query most probably needs to look something like this:
SELECT first_name, last_name, car_rego
FROM staff
JOIN carallocation ON (carallocation.staff_id = staff.staff_id)
JOIN car ON (car.car_id = carallocation.car_id)
WHERE staff.office = 'Glasgow' OR staff.office = 'Bearsden';
Note that JOIN is a synonym for an INNER JOIN in MySQL.
I haven't tested it but this should help.
select car.rego
from car
inner join carrallocation ca on ca.carid = car.carid
inner join staff s on ca.staffid = s.staffid
where s.office in ('Glasgow', 'Bearsden')
What you are missing is the joins between the tables. You need to join to the intermediary table to get the relationship between the staff and car tables.
As far as car_allocation is an intermediate table, it's rather better to put it into the FROM clause and JOIN others:
SELECT car.rego
FROM car_allocation A
JOIN car ON A.car_id = car.car_id
JOIN stuff ON A.stuff_id = stuff.stuff_id
AND (staff.office = 'Glasgow' OR staff.office = 'Bearsden' -- OR some another condition, etc)
Pay attention to brackets, they groups conditions.
or
SELECT car.rego
FROM car_allocation A
JOIN car ON A.car_id = car.car_id
JOIN stuff ON A.stuff_id = stuff.stuff_ids
AND staff.office IN ('Glasgow', 'Bearsden')
If you have an enumeration you can use IN operator.
Also JOIN means the same as INNER JOIN. Here's MySQL manual considering JOIN syntax