150 likes | 256 Views
SQL Sub (or Nested ) Query. Examples. Q: Find students whose GPA is below the average. The criteria itself require s a SQL statement. SELECT * FROM student WHERE gpa < (SELECT AVG(gpa) FROM student); Q: Find employees with higher than average salary.
E N D
Examples • Q: Find students whose GPA is below the average. • The criteria itself requires a SQL statement. • SELECT * FROM student • WHERE gpa < (SELECT AVG(gpa) FROM student); • Q: Find employees with higher than average salary. • Q: Find employees who are hired before E2.
Sub Query with IN • SELECT sid, sname FROM student • WHERE sid in (‘S1’, ‘S2’); • Use a subquery to retrieve keys that meet criteria. • Example: Display students’ ID, name who owe university more than $2000. (Note: not showing balance) • SELECT sid,sname • FROM student • WHERE SID IN (SELECTSIDFROM account WHERE balance > 2000); • Example: Display students’ ID, name and Balance who owe university more than $2000.
Sub Query with IN, NOT IN • Use a subquery to retrieve keys that meet criteria. • Q: Display faculty’s ID and name if the faculty advises at least one student. • SELECT fid, fname FROM faculty • WHERE fid IN (SELECT DISTINCT fid FROM student); • Q: Display faculty’s name and name if the faculty does not advise any student. • SELECT fid, fname FROM faculty • WHERE fid NOT IN (SELECT DISTINCT fid FROM student);
Q: Display students’ ID and name who are taking at least one course. • Q: Display students’ ID and name who do not take any course.
Sub Query with IN • Q: Find students who take more than 5 courses and display their ID and name. • SELECT sid, sname FROM • Student NATURAL JOIN Registration • GROUP BY sid,sname • HAVING COUNT(cid)>5; • SELECT sid, sname FROM student • WHERE sid IN (SELECT sid FROM Registration GROUP BY sid • HAVING COUNT(cid) > 5);
Sub Query with Join Display students’ ID, name and Balance who owe university more than $2000. SELECT sid,sname, Balance FROM student NATURAL JOIN (SELECT * FROM account WHERE balance > 2000); Display students’ ID and name who owe university more than $2000. SELECT sid,sname FROM student NATURAL JOIN (SELECT * FROM account WHERE balance > 2000); SELECT sid,sname FROM student WHERE SID IN (SELECTSIDFROM account WHERE balance > 2000);
Sub Query with ALL/SOME/ANY • Q: Find students whose gpa is greater than all/some bus majors’ gpa: • SELECT sid, sname FROM student • WHERE gpa > ALL(SELECT gpa FROM student WHERE major=‘bus’); • SELECT sid, sname FROM student • WHERE gpa > SOME (SELECT gpa FROM student WHERE major=‘bus’); • SELECT sid, sname FROM student • WHERE gpa > ANY (SELECT gpa FROM student WHERE major=‘bus’);
Comparing SQL Statements Compute the number of courses taken by each student: (1) SELECT sid, sname, courses FROM student NATURAL JOIN (SELECT sid, COUNT(cid) AS courses FROM registration GROUP BY sid); (2) SELECT sid, sname, COUNT(cid) AS courses FROM student NATURAL JOIN registration GROUP BY sid, sname;
InLine View • When a multiple-column subquery is used in the FROM clause of an outer query, it basically creates a temporary table that can be referenced by other clauses of the outer query. The temporary table is called InLine view.
Select from InLine View SELECT GPAGroup, Count(SID), avg(gpa) FROM (SELECT SID,GPA, CASE WHEN gpa < 2.0 THEN 'Poor' WHEN gpa < 3.0 THEN 'Good' ELSE 'Excellent' END AS GPAGroup, FROM student) GROUPBY GPAGroup; Note: Calculated field cannot be used in the Where or Group By clause. Select eid,ename, salary*.1 As Tax From Employee Where Tax > 1000; ---- This will cause error.
Top (Last) n Analysis • Find students with the top 3 GPA. • Can we do: • SELECT * FROM student ORDER BY GPA desc WHERE ROWNUM<=3; ? --- No! • SELECT * FROM • (SELECT * FROM student ORDER BY GPA DESC) • WHERE ROWNUM<=3; • Note: Use the ROWNUM of the InLineView.
Example: Find Business students who owe university more than $2000 and display their ID, name and balance. • Select sid, sname, Balance From • (select * from Student where major='bus') natural join (select * from account where balance >2000);
Subquery in the SELECT List • SELECT eid,ename,salary,(select AVG(salary) from employee)as AvgSal FROM employee; • SELECT eid,ename,salary-(select AVG(salary) from employee)as DeviateFromMeanFROM employee;
Multiple Levels Subquery • Find students who are taking one-unit courses and display their ID and name. • Join: • Select sid, sname, cid from student natural join registration natural join course where units=1; • Without Join: • Select sid, sname From Student • Where sid in (select sid from registration where cid in • (select cid from course where units=1));