SQL three tables to return one row - mysql

Hey people sorry if this question isnt really clean but it goes as following
i have a homework quest which goes along the lines of this.
Write a quer to return how many subjects this student takes with the LastName "Field"
the three tables are:
Students:
StudentID
FirstName
LastName
StudentCourses:
StudentCourseID
StudentID
CourseID
Courses:
Maths
Science
English
P.E
Drama
Film Studies
I have something like
SELECT Students.studentID,
Students.Lastname,
Students.CourseID
FROM Students
WHERE Students.LastName = "Field"
INNER JOIN StudentCourses
ON Student.CourseID = StudentCourses.CourseID
FULL JOIN Student
ON Student.RoleID = Courses.CourseID
This is written so i cannot test it but does this seem correct to anyone?

Use COUNT() function to find the number of courses the student takes and use WHERE clause to filter the student’s last name as ‘Field’.
SELECT s.StudentID, s.LastName, COUNT(sc.CourseID)
FROM Students s
JOIN StudentCourses sc
ON s.StudentID = sc.StudentID
WHERE s.LastName = 'Field’;

Where should come last. You don't really need the 3rd join to the courses table because you aren't selecting anything from it.
SELECT st.STUDENTID,
st.LASTNAME,
count(stc.COURSEID) as COURSECOUNT
FROM STUDENTS st
INNER JOIN STUDENTCOURSES stc
ON st.COURSEID = stc.COURSEID
WHERE st.LASTNAME = 'Field'

You can try the following query. WHERE clause should be after FROM and JOIN clauses
SELECT s.studentid,
s.lastname,
COUNT(sc.courseid)
FROM student s
JOIN studentcourses sc
ON s.studentid = sc.studentid
WHERE s.lastname = 'Field'

Related

my sql output the same data more than one time when i use select statement

I created three tables "student" and "course" and "lecturer" and I inserted data into them.
Now I want to retrieve some data by select.
When I want to show: Subject taken by Kumar
SELECT STUDENT.NAME, COURSE.SUBJECT1, COURSE.SUBJECT2, COURSE.SUBJECT3
FROM STUDENT,COURSE
WHERE STUDENT.COURSE = COURSE.COURSE = 'MLVK'
it repeats the data more than one time.
I hope anyone help me
All the best
Learn to use proper, explicit, standard JOIN syntax.
And, table aliases:
SELECT s.NAME, c.SUBJECT1, c.SUBJECT2, c.SUBJECT3
FROM STUDENT s JOIN
COURSE c
ON s.COURSE = c.COURSE
WHERE c.COURSE = 'MLVK'
If I were going to use these tables to pick subjects taken by Kumar, I would write something like:
SELECT
s.name, c.course, c.subject1, c.subject2, c.subject3
FROM
student as s
LEFT JOIN course as c on c.course = s.course
WHERE
s.no_matrik = '23456'
GROUP BY
s.name, c.course, c.subject1, c.subject2, c.subject3
I think this version makes the intent slightly more clear (pick subject for a particular student, Kumar) than the previous answer (select subjects for any student having course = 'MLVK'). This answer will also return information for Kumar even if he has no course value in the Student table (pre-enrollment?).
First of all I would suggest that to model your data properly in BCNF form, where you should have modelled another table to persist subjects and map the lecturer who take that subject.
Ex: (Subject Table)
SubjectId LECT_ID
---------- ------------
TT234 L123
TT235 L003
and, your Course table would be more of course to subject mapping, like:
CourseName SubjectId
------------- --------------
DTM TT235
DTM TT695
...
then you use query as:
Select sub.SubjectId, l.NAME
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.Lecturer
Where s.NAME = 'Aminah'
the above query will result as:
SubjectId NAME
--------- ----------
PP563 Ahmad
SS003 Ahmad
PP999 John
as Ahmad happens to be teaching 2 subjects in course DPG. but if you wish to report distinct Lecturers for Aminah, you can change query as:
SELECT NAME FROM Lecturer where LECT_ID
in (
Select l.LET_ID
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.LECT_ID
Where s.NAME = 'Aminah'
) a

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
;

Get all data from a table that contains two foreign key data

Table instructor: ID, name, dept_name, salary
Table student: ID, name, dept_name, tot_cred
Table advisor: s_ID, i_ID which contains student id and instructor id for the two table.
I need to find all the instructor and student's name where the department of the advisor is CComp.Sci
I can find all the id of the instructor and student whrer the intructor is fom Computer science. And only the name of the students.
But can't figure out both the name at the same time.
I wrote this:
SELECT student.name
FROM student
WHERE student.ID in (SELECT advisor.s_ID
FROM advisor
, instructor
WHERE advisor.i_ID = instructor.ID
and instructor.dept_name = 'Comp.Sci')
The root solution I think you need here is just a simple join between the three tables. But since you need a single list of both student and instructor names, this complicates things. One option is to union together a query which finds the matching students along with a query which finds the matching instructors.
SELECT s.name, 'student' AS type
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
UNION ALL
SELECT DISTINCT i.name, 'instructor'
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
Using "where .. in (..)" will give you bad performance, also it will not allow you to get the data from the table in the where clause.
Here is a solution if you mean to get the result of instructor name side alone with student name.
SELECT S.name AS Student,I.name AS Instructor
FROM Students AS S
JOIN Advisor AS A ON A.s_ID = S.Id
JOIN Instructor AS I ON I.Id = A.i_ID
WHERE I.dept_name = 'Comp.Sci'
*notice that I used alias
** if you want for example to get all students even those who lack instructor use LEFT JOIN

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.

SQL count / query assistance

So I'm learning MySQL and I'm trying to do the following:
For each instructor list his / her name and the number of students he / she mentors.
The relevant part of the schema is:
Person(ID, Name)
Student(StudentID, MentorID)
Instructor(InstructorID)
Both InstructorID and StudentID map to Person.ID, and MentorID maps to InstructorID (Each student has an instructor mentor, and both instructors and students are Persons).
I've tried the following to no avail:
select p.Name, count(select s.StudentID
from Student s
where s.MentorID = i.InstructorID)
from Person p, Instructor i
where p.ID = i.InstructorID;
Also this after reading some things on StackOverflow:
select InstructorDetails.Name, count(Mentees)
from Instructor i
inner join Person as InstructorDetails
on InstructorDetails.ID = i.InstructorID
inner join Student as Mentees
on Mentees.MentorID = i.InstructorID;
Any suggestions?
You lack GROUP BY on your query,
SELECT InstructorDetails.Name, count(*) totalCount
FROM Instructor i
INNER JOIN Person as InstructorDetails
ON InstructorDetails.ID = i.InstructorID
INNER JOIN Student as Mentees
ON Mentees.MentorID = i.InstructorID
GROUP BY InstructorDetails.Name