I have 3 tables one called Doctor, Person and one called Appointment. They have doctor_id in common relationship but only person holds their Last_Name.
I need to be able Produce a list of doctor IDs together with the number of appointments made for each doctor with zero or more appointment and also the doctors name.
How would I implement the the name of the doctor?
Here are the tables:
This is what I have so far:
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
GROUP BY doctor.doctor_id;
Just add one more left join for the new table :
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP, person.Last_name
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
LEFT JOIN person on person.doctor_id = doctor.doctor_id
GROUP BY doctor.doctor_id;
Assuming the person table contains a link to doctors (will update when yo post third table if needed)
SELECT d.doctor_id, p.last_name, COUNT(a.appt_time) AS No_APP
FROM doctor d
LEFT JOIN appointment a ON d.doctor_id = a.doctor_id
LEFT JOIN person p ON d.doctor_id = p.doctor_id
GROUP BY d.doctor_id;
Related
I am having a major problem joining 5 tables because each table only has 1 column in common with only 1 other table.
Here are my tables and columns in each table:
TABLE (COLUMNS)
person (person_id, first_name, last name)
building (building_id, building_name)
room (room_id, room_number, building_id, capacity)
meeting (meeting_id, room_id, meeting_start, meeting_end)
person_meeting (person_id, meeting_id)
OK, now here is what I am trying to do (pasted from a homework assignment):
Construct the SQL statement to find all the meetings that person_id #1 has to attend. Display the following columns:
Person’s first name
Person’s last name
Building name
Room number
Meeting start date and time
Meeting end date and time
Now I know how to join 2 tables but I have no idea how to pull info from 5 different tables like this.
I tried looking up how to do this and it just says to do a UNION command, and I am just learning and have yet to cover that.
As UNION is used to combine the result from multiple SELECT statements into a single result set, you don't need it for this scenario. You have to join all the tables one by one based on their Id.
SELECT P.First_Name, P.Last_Name, B.Building_name, R.Room_Number,
M.Meeting_Start, M.Meeting_End FROM Person P
JOIN Person_Meeting PM ON P.Person_Id = PM.Person_Id
JOIN Meeting M ON PM.Meeting_Id = M.Meeting_Id
JOIN Room R ON M.Room_Id = R.Room_Id
JOIN Building B ON R.Building_Id = B.Building_Id
WHERE P.Person_Id = 1
Suppose that we have four tables in the following structure (Table name - fields):
person - id, name
doctor - id_person, specialty
pacient - id_person, disease
appointment - doctor_id, pacient_id, date
How can I construct a query to return the doctor's name and specialty, the pacient's name and disease and the appointment date?
Here's where I've got so far:
SELECT person.name, doctor.specialty, pacient.disease, appointment.date
FROM appointment
INNER JOIN person
ON appointment.pacient_id=person.id
INNER JOIN doctor
ON appointment.doctor_id=doctor.id_person
INNER JOIN pacient
ON appointment.pacient_id=pacient.id_person
But this is not returning the right fields. I think the problem resides in returning the same field (person.name) for two different ids (doctor and pacient) in the same row.
You need to do two separate joins to the person table, and use aliases to identify the individual tables like so:
select
dp.name as DoctorName
, doctor.specialty
, pp.name as PacientName
, pacient.disease
, appointment.date
from appointment
inner join doctor
on appointment.doctor_id = doctor.id_person
inner join person dp
on appointment.doctor_id = dp.id
inner join pacient
on appointment.pacient_id = pacient.id_person
inner join person pp
on appointment.pacient_id = pp.id
Hi I have these 2 problems below and my attempt. Please give me some advice.
SQL database:
CUSTOMER table: CUST_ID, CUST_NAME, ...
PRODUCT table: PROD_ID, PROD_DESCRIPTION, ...
CUSTOMER_PRODUCTS table: CUST_ID and PROD_ID.
This database is used to track the products a customer owns, so CUSTOMER_PRODUCTS has an entry for each product a customer owns.
I want to:
A:
Write a SQL query that will return a list of all customers who do not own any products.
Here is my attempt:
SELECT CUSTOMER _PRODUCTS.CUST_ID, CUSTOMER.CUST_ID, CUSTOMER.CUST_NAME
FROM CUSTOMER
INNER JOIN CUSTOMER_PRODUCTS
ON CUSTOMER_PRODUCTS.CUST_ID != CUSTOMER.CUST_ID;
B:
Write a SQL query that will return a list of all customers who own a product with ‘SAW’ in the name.
Here is my attempt:
SELECT CUSTOMER _PRODUCTS.CUST_ID, CUSTOMER.CUST_ID, CUSTOMER.CUST_NAME
FROM CUSTOMER
INNER JOIN CUSTOMER_PRODUCTS
ON CUSTOMER_PRODUCTS.CUST_ID = CUSTOMER.CUST_ID and WHERE PROD_DESCRIPTION LIKE 'SAW';
What do you guys think? am I headed in the right direction?
For B
SELECT CUSTOMER _PRODUCTS.CUST_ID, CUSTOMER.CUST_ID, CUSTOMER.CUST_NAME
FROM CUSTOMER
INNER JOIN CUSTOMER_PRODUCTS ON CUSTOMER_PRODUCTS.CUST_ID = CUSTOMER.CUST_ID
and WHERE PROD_DESCRIPTION LIKE '%SAW%';
Other person beat me to part A
For the A, you may want to use LEFT JOIN and IS NULL:
SELECT CUSTOMER.CUST_NAME
FROM CUSTOMER
LEFT JOIN CUSTOMER_PRODUCTS ON CUSTOMER.CUST_ID = CUSTOMER_PRODUCTS.CUST_ID
WHERE CUSTOMER_PRODUCTS.CUST_ID IS NULL;
for the B, pease ask separate question, as it is separate.
I need to write a query for a scout database that compares the requirements of a badge with the skills a given member has already earned. The purpose being that several skills are applicable to multiple badges. My relevant tables (there are many) look like this:
Badge_Table:
Badge_ID,
Badge_Name,
Badge_Description,
Badge_Skills_Table:
Badge_Skill_ID,
Badge_ID,
Skill_ID,
Skills_Table:
Skill_ID,
Skill_Name,
Skill_Description,
Skills_Earned_Table:
Skills_Earned_ID
Skill_ID
User_ID
User_Table:
User_ID,
Name,
Age,
Address
Primary keys are shown in italics, and the foreign key relationships go from Badge_table to Badge_Skills_Table to Skills_Table to Skills_Earned_table to User_Table.
So far I have came up with the following ideas:
Selects all badges for named skill
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id)
WHERE Skills_Table.Skill_Id = 1;
Selects all badges for each skill
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id)
WHERE Skills_Table.Skill_Id = Skill_Badge_Table.Skill_Id
Selects all badges for named skill for named User - not quite working
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id
INNER JOIN Skills_Earned_Table On skill_ID
INNER JOIN users_table ON user_ID)
WHERE Skills_Earned_Table.User_ID= 1 AND Skills_Earned_Table.SKILL_ID = Skill_Badge_Table.skill_ID
So can anyone help guide me with the following:
How to return all badges that a given skill is applicable for. (Done)
How to return all badges that a given scout has earned skills towards.
To return all badges the a given scout has earned all the skills for.
I'd appreciate any help you can offer,
You have no <conditions> in your ON clause. Try my query below:
SELECT A.badge_name
FROM badge_table A
INNER JOIN badge_skills_table B ON A.badge_ID=B.badge_ID
INNER JOIN Skills_Table C ON B.skill_Id=C.skill_ID
INNER JOIN Skills_Earned_Table D ON C.skill_ID=D.skill_ID
INNER JOIN users_table E ON user_ID ON D.user_ID=E.user_ID
WHERE D.User_ID= 1 AND D.skill_ID = B.skill_ID
I'm trying to join these 4 tables:
customers:
id_customer;
name;
surname
employers:
id_employer;
id_customer;
products_to_employer
id_product;
id_employer;
products
id_product;
name_product;
code;
price;
Where i basically try to get all products of a customer. Basically each product is related whit a id_employer, but each employer is related whit a customer, so what i want is to bring all the products that is not related whit the employer but whit his "parent" the customer.
The result I want to get is:
Result:
Example: variable $id_customer: 4
products result:
id_customer (id nr 4)
name_product;
name_product;
code;
price;
This is the requirement:
Where i basically try to get all products of a customer
So it is just a matter of applying some joins and a where clause for the filter on id_customer
select p.* from products p
join products_to_employer pe on p.id_product = pe.id_product
join employers e on pe.id_employer = e.id_employer
join customers c on e.id_customer = c.id_customer
where c.id_customer = 4
NOTE: There shouldn't be any need of adding the id_customer to the results as you are already filtering by it.
select c.id_customer, c.name, p.name_product, p.code, p.price
from products p
inner join products_to_employer pte on (pte.id_product = p.id_product)
inner join employers e on (e.id_employer = pte.id_employer)
inner join customers c on (c.id_customer = e.id_customer)
where c.id_customer = 4