Example of Simple SQL Join

·

Student (sno: integer, stname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: string, room: string, fid: integer)
Enrolled (sno: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)


Find names and majors of students who have enrolled in at least one class.

SELECT S.stname, S.major
FROM Student S, Enrolled E
WHERE S.sno = E.sno

Find the number of students who have enrolled in at least two classes.

SELECT COUNT(DISTINCT S.stname)
FROM Student S, Enrolled E1, Enrolled E2
WHERE E1.sno = E2.sno AND
E1.cnum <> E2.cnum AND
S.sno = E1.sno

Live Traffic Feed

About this blog

Site Sponsors