How do I query a table by list of values? - sql

I have 3 tables:
Person
-----------
id
name
doctor_id
Doctor
-----------
id
name
Person_Doctor
-------------
id
person_id
doctor_id
The idea is that a person can have more than one doctor, and a doctor can have more than one patient (person).
What I want to do is get all of a person's doctors by id. Here's my prelim strategy:
SELECT * FROM Person_Doctor WHERE person_id=:id
and then map each Person_Doctor to the corresponding doctor by doctor_id. The thing with this approach is that I'll have to query the DB for each Person_Doctor object, seems expensive. Is there a better way to do this?

Select p.name as patient, d.name as doctor
From person as p inner join person_doctor as pd on pd.person_id = id
inner join doctor as d on pd.doctor_id = d.id
where p.id = SomeID

This is a fairly basic JOIN, which is a fundamental element of relational databases. I'd recommend once you've finished this assignment, studying up and perhaps taking a college level course in databases. The problems you'll encounter while working in SQL will all be harder than this one.
SELECT d.name as 'doctor name'
FROM doctor d
INNER JOIN person_doctor pd on d.doctor_id = pd.doctor_id
INNER JOIN person ON p p.person_id = pd.person_id
WHERE p.person_id = #id

SELECT DISTINCT p.id
FROM person p
join person_doctor pd
on p.id = pd.person_id
join doctor d
on pd.doctor_id = d.id
WHERE p.doctor_id IS NOT NULL

Related

Sql select clause with three table

I am trying to write a sql query to show student list for each course.
The diagram below show the database relationship.
The SQL query I have written is:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = StudentId
where Courses.CourseId = 1
The issue is that i am getting returned alot more data than I expected as only one student is registered for the course but i get ten entries. I am not sure if i have done somethings fundamental wrong or is my query the issue.
This is the data
This is the result
I expected only two rows to be returned.
Thanks
Every column in your query must be qualified with the table's name.
You did not qualify the column StudentId in this join:
inner join Student on Enrollments.StudentId = StudentId
If you did you would find the error which is that there is no column StudentId in the table Student and you should use the column Id:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = Student.Id
where Courses.CourseId = 1
or better with aliases for the tables:
select *
from Courses as c
inner join Enrollments as e on e.CourseId = c.CourseId
inner join Student as s on e.StudentId = s.Id
where c.CourseId = 1
The primary key of table Student is Id, not StudentId.
So the correct query is:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = Student.Id
where Courses.CourseId = 1

Fetching columns, 4 tables

I’ve ran in to some problems with the following SQL assignment. I’m to retrieve the ID, firstname and lastname of any member who is registered in section that contains the word ‘xyz’.
So far I’ve managed the following:
SELECT m.id, p.firstname, p.lastname FROM member m
INNER JOIN person p ON m.id = p.id
WHERE m.id IN (SELECT id FROM membersection);
How do I go forward from here? I have no idea how to retrieve the sectionid from the membersection table then fetch the section name from the section id using that ID so I can check if the section name contains the previously stated word.
member:
id
member_number
registration_date
membersection:
memberid
sectionid
person:
id
firstname
lastname
section:
id
name
Just keep joining. And in the end use LIKE to check if section.name contains 'xyz'.
SELECT m.id,
p.firstname,
p.lastname
FROM member m
INNER JOIN person p
ON p.id = m.id
INNER JOIN membersection ms
ON ms.memberid = m.id
INNER JOIN section s
ON s.id = ms.sectionid
WHERE s.name LIKE '%xyz%';
There is some ambiguity in your question with regards to how your data are structured; what are the primary and foreign keys?
But, making some assumptions, you're almost there, you can chain multiple join statements together:
select
m.id,
p.firstname,
p.lastname
from
member m
inner join person p on
m.id = p.id
inner join membersection ms on
m.id = ms.memberid
inner join section s on
ms.sectionid = s.id
where
s.name like '%xyz%'
It's not super obvious what's going on with your Data Relationships, but this would be the basic route you might want to take (LEFT JOIN as I do not know the relationship):
SELECT m.id, p.firstname, p.lastname, ms.sectionid
FROM member m
INNER JOIN person p ON m.id = p.id
LEFT JOIN section s ON m.id = s.id
LEFT JOIN membersection ms ON m.id = ms.memberid
WHERE s.name = 'xyz'

JOINING Same Table gives duplicate information

I want to join the same table to get some information.
I have a Person Table
PersonId, FirstName, LastName, Address
Also Patient Table
PatientId, PersonId, ResponsiblePersonId
Below is the query i tried to get Patient's FirstName, Last Name also the Responsible Person FirstName, Last Name. For this I JOINED Person table once again to get Responsible Persons First, Last names.
But I got many duplicate records.
SELECT PAT.PatientId
,PER.PersonNumber
,PER.FirstName
,PER.LastName
,RES_PER.FirstName AS ResFirstName
,RES_PER.LastName AS ResLastName
,PER.Address
FROM dbo.Patient AS PAT
INNER JOIN dbo.Person AS PER
ON PAT.PersonId = PER.PersonId
INNER JOIN dbo.Person AS RES_PER
ON PAT.ResponsiblePersonId = PER.PersonId
How can i get Patient FirstName, LastName & Responsible Person's First Name, Last Name for the patient record?
Basically, you need to join table Person twice on table Patient in order to get the two dependent columns on it.
SELECT b.FirstName Patient_FirstName,
b.LastName Patient_LastName,
b.Address Patient_Address,
c.FirstName Responsible_Firstname,
c.LastName Responsible_LastName,
c.Address Responsible_Address
FROM Patient a
INNER JOIN Person b
ON a.PersonID = b.PersonID
INNER JOIN Person c
ON a.ResponsiblePersonId = c.PersonID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
All you need to do is replace PER with RES_PER in the very last bit of your SQL. (i.e. PER.PersonId becomes RES_PER.PersonId).
SELECT PAT.PatientId
,PER.PersonNumber
,PER.FirstName
,PER.LastName
,RES_PER.FirstName AS ResFirstName
,RES_PER.LastName AS ResLastName
,PER.Address
FROM dbo.Patient AS PAT
INNER JOIN dbo.Person AS PER
ON PAT.PersonId = PER.PersonId
INNER JOIN dbo.Person AS RES_PER
ON PAT.ResponsiblePersonId = RES_PER.PersonId

How to select SQL results based on multiple tables

I need to select results from one table based on certain matching values in a couple of other tables. I have the following tables:
person: id, firstname, lastname
team: id, teamname
player: id, person_id(FK), team_id(FK)
coach: id, person_id(FK), team_id(FK)
I need to return all the coaches and players names for each team. I've only ever used inner joins, and it doesn't seem like I can use those here, so any idea how to do this?
This will give you the coach:
SELECT team.Teamname, person.Firstname, person.Lastname
FROM person
JOIN coach ON person.id = coach.person_id
JOIN team ON coach.team_id = team.id
And this will give you the players:
SELECT team.Teamname, person.Firstname, person.Lastname
FROM person
JOIN player ON person.id = player.person_id
JOIN team ON player.team_id = team.id
So, the non-elegant, simple answer is to just toss it all together with UNION.
Just use an OR in the join to Team
SELECT
P.firstname,
P.lastname,
T.teamname
FROM
person p id
LEFT JOIN player pl
ON p.id = pl.person_id
LEFT JOIN coach c
ON p.id = c.person_id
LEFT JOIN team t
ON pl.team_id = t.id
or.c.team_id = t.id
Or if you perfer if and your database has COALESCE
LEFT JOIN team t
ON COALESCE(pl.team_id,c.team_id) = t.id

mysql - three joins on the same table

I have two tables:
persons
- person_id
- fullname
students
- student_id
_ person_id
- father_id
- mother_id
In students table the last three columns store ids from persons table. What SELECT could retrieve the following data:
- student name
- father name
- mother name
We assume no WHERE, ORDER BY, or LIMIT for simplicity
try this:
select sp.fullname studentname, fp.fullname fathername, mp.fullname mothername
from students s
inner join persons sp on (s.student_id = sp.person_id)
inner join persons fp on (s.father_id = fp.person_id)
inner join persons mp on (s.mother_id = mp.person_id)
Try below query -
SELECT p.fullname,m.fullname,f.fullname from students s
LEFT JOIN persons p ON s.person_id = p.id
LEFT JOIN mother m ON s.mother_id = m.id
LEFT JOIN father f ON s.father_id = f.id
WHERE s.student_id = 'id of which student record you want';