Querying multiple mySQL tables in a single query - mysql

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

Related

Two tables. One contains the school info and the other personal. A quire that picks students who are from CA and like either skateboard or soccer.

What currently happens is it just selects students from CA and students who like to skateboard. I need it to return only students who are both from CA and play soccer.
SELECT *
FROM schooldata a
INNER JOIN studentinfo b
ON b.schooldata_id = a.id
WHERE a.state = "ca"
AND ( activity = "soccer"
OR activity = "skateboard" )
You will have to do inner join based on lastname and firstname column ex:-b.lastname=a.lastname and b.firstname=a.firstname . ideally you should be maintaining primary key column of type integer in schooldata table and its foreign key reference in studentinfo and join based on those columns.
You should use join clauses. And I think base on your question. Inner Join is the best clause you should use.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

Return all courses a student is taking (Many to Many SQL Database example)

I'm fairly new to MySQL, and trying to understand the many-to-many relationship since these examples can popup in interviews
There are 3 tables, and since a Student can have many courses and a Course can have many students, this is a Many-to-Many relationship right?
The tables are
Student- has student ID, name, date of birth, and department.
Courses- Has ID, Name of course
Student_Courses- Has student_id, course_id
How would I display these 2 questions-
1) Given a studentID, return all the names of the courses the student is taking
2) Return the name of students who is taking X amount of courses or more (Ex. 4 or more courses).
Im trying to write queries on these, but I'm stuck...
In the case of selecting all of the courses for a given student ID you could try the following, which will return one row for each Course a Student is associated with.
select
s.name as StudentName,
c.name as CourseName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
where
(s.`ID` = 'given_Student_ID_here')
;
As for selecting a list of the names of Students taking N or more courses, for this you might use an aggregating sub-select as a WHERE clause in which we reference one of the outer tables (i.e. [Student]) so that the result of the aggregation is personalised per Student record:
select
s.name as StudentName
from `Student` as s
where
(
(
select count(*)
from `Student_Course` as sc
inner join `Course` as c on (c.ID = sc.course_id)
where (sc.student_id = s.ID)
) >= 4
)
;
You might also consider an alternative approach to this second problem by using the GROUP BY and HAVING clauses:
select
s.name as StudentName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
group by
s.name
having
count(*) >= 4
;

Joining three tables with parent-child relationship

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

get data from 3 relational tables

I have three tables Guardian, Student and StudentsGuardian. Table information is as under
Guardian:
id(pk)
Student:
id(pk)
name
address
StudentsGuardian:
student_id(fk)
guardian_id(fk)
I want to select those students whose guardian_id=2(suppose). Actually these are relational tables so i am unable to think a way to accomplish it. If i apply join it would return a joint table but i need only the information of those students having guardian_id= specific id.
It could be a basic question but i am stuck in it. Thanks
Use below query:
SELECT s.id, s.name, s.address
FROM Student s
INNER JOIN StudentsGuardian sg ON s.id = sg.student_id
WHERE sg.guardian_id = 'somespecific_id'
SELECT
*
FROM Guardian
INNER JOIN StudentsGuardian ON StudentsGuardian.guardian_id = Guardian.id
INNER JOIN Student ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
SELECT Student.name, Student.address
FROM Student JOIN StudentsGuardian ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
That should do.

MySQL COUNT() with multiple joins

I am building a course system, with courses, instructors and a table to relate the two.
Symplified table instructors:
id | name | ...
Symplified table courses:
id | name | instructors_needed | ...
Symplified table link:
id | course_id | instructor_id
I created the following query to fetch the names of the instructors associated to a particular course:
SELECT i.name, c.name, c.instructors_needed FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
This works fine. I created the following query to find the number of instructors on each course:
SELECT COUNT(i.name) as number, c.id, c.name, c.instructors_needed FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
GROUP BY c.ID
I want to combine the two queries, to get all details about the instructors for a particular case, but also the total number of instructors on the course and the number of instructors needed. How do I do that? I understand that the GROUP BY is the problem here.
I searched but I could only find examples with 2 tables instead of 3, and I somehow can't figure it out for three tables.
Your help is really appreciated, thank you!
Try:
SELECT i.name, c.name, c.instructors_needed, Ctr.CourseCount FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
LEFT OUTER JOIN
(SELECT link.course_id, COUNT(*) as CourseCount FROM link GROUP BY link.course_id) Ctr
ON link.course_id = Ctr.course_id