Download presentation
Presentation is loading. Please wait.
1
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007
2
2 AGENDA: How Do Subqueries Work? Subqueries and Join Correlated Subqueries Subquery Example Subqueries Operators: EXISTS, ANY, SOME, ALL, using COUNT Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 11-12
3
3 How Do Subqueries Work? SQL provides you the ability to nest queries within one another The inner queries generates values that are tested in the predicate of the outer query SELECT * FROM Orders WHERE snum = (SELECT snum FROM Salespeople WHERE sname = ‘Motika’);
4
4 How Do Subqueries Work? (cont’d) Using aggregate functions in Subqueries SELECT * FROM Orders WHERE amt > (SELECT AVG(amt) FROM Orders WHERE odate = ’10/04/2000’); Using IN with Subqueries that produce multiple rows SELECT * FROM Orders WHERE snum in (SELECT snum FROM Salespeople WHERE city – ‘London’);
5
5 How Do Subqueries Work? (cont’d) Using expressions in Subqueries SELECT * FROM Customers WHERE cnum = (SELECT snum + 1000 FROM Salespeople WHERE sname = ‘Serres’);
6
6 Subqueries and Join The subquery version would execute more efficiently. In the Joins version, the DBMS would have to go through each possible combination of rows from the two tables and test it against the compound predicates. Subqueries in HAVING – SELECT rating, COUNT(DISTINCT cnum) FROM Customers GROUP BY rating HAVING rating > (SELECT AVG(rating) FROM Customers WHERE city = ‘San Jose’);
7
7 Correlated Subqueries When you use subqueries in SQL, you can refer in the inner query to the table in the FROM clause of the outer query, forming a correlated subqueries How the Correlated Subquery Works – Select a row from the table named in the outer query, called the current candidate row. – Store the values from his candidates row in the alias named in the FROM clause of the outer query – Perform the subquery. Use the value from the outer query’s candidate row in the subquery. It is call outer reference. – Evaluate the predicate of the outer query on the basis of the results of the subquery performed. – Repeat the procedure for the next candidate row of the outer query and so on until all the candidate rows have been tested.
8
8 Correlated Subqueries (cont’d) Using Correlated Subqueries to Find Anomalies – SELECT * FROM Orders main WHERE NOT snum = (SELECT snum FZROM Customers WHERE cnum = main.cnum); Correlated a Table with Itself – Enabling you to extract certain complex forms of derived information. – SELECT * FROM Orders outer WHERE amt > (SELECT AVG(amt) FROM Orders inner WHERE inner.cnum = outer.cnum);
9
9 Correlated Subqueries (cont’d) Correlated Subqueries in HAVING – SELECT odate, SUM(amt) FROM Orders a GROUP BY odate HAVING SUM(amt) > (SELECT 2000.00 + MAX(amt) FROM Orders b WHERE a.odate = b.odate);
10
10 Subquery Example Advanced Subquery Example SELECT * FROM Salespeople first WHERE EXISTS (SELECT * FROM Customers second WHERE first.snum = second.snum AND 1 < (SELECT COUNT(*) FROM Orders WHERE Orders.cnum = second.cnum));
11
11 Subqueries Operators: EXISTS EXISTS is an operator that produces a TRUE or FALSE values, in other words, a Boolean expression. EXISTS simply checks whether a query produces output and therefore cannot be UNKNOWN. SELECT cnum, cname, city FROM Customers WHERE EXISTS (SELECT * FROM Customers WHERE city = ‘San Jose’); – The inner query selected all data for all customers in San Jose. – The EXISTS operator in the outer predicate noted that the subquery produced some output and made the predicate to be TRUE.
12
12 Subqueries Operators: EXISTS (cont’d) Using EXISTS with Correlated Subqueries – With a correlated subquery, the EXISTS clause is evaluated separately for each row of the table referenced in the outer query, as are other predicate operators used with correlated subqueries. – SELECT DISTINCT snum FROM Customers outer WHERE EXISTS (SELECT * FROM Customers inner WHERE inner.snum = outer.snum AND inner.cnum <> outer.cnum);
13
13 Subqueries Operators: EXISTS (cont’d) Combining EXIST with Joins – SELECT DISTINCT first.snum, sname, first.city FROM Salespeople first, Customers second WHERE EXISTS (SELECT * FROM Customers third WHERE second.snum = third.snum AND second.cnum <> third.cnum) AND first.snum = second.snum); Using NOT EXISTS EXISTS and Aggregates
14
14 Subqueries Operators: ANY or SOME SOME and ANY are interchangeable; wherever we use the term ANY, SOME would work just the same. SELECT * FROM Salespeople WHERE city = ANY (SELECT city FROM Customers); – The ANY operator takes all values produced by the subquery. – And evaluates them to TRUE if ANY of them equal to the value of the current row of the outer query. – The datatypes should be comparable. – Opposite of EXISTS.
15
15 Subqueries Operators: ALL With ALL, the predicate is TRUE if every value selected by the subquery satisfies the condition in the predicate of the outer query. SELECT * FROM Customers WHERE rating > ALL (SELECT rating FROM Customers WHERE city = ‘Rome’); ALL is used primarily with inequalities rather than equalities because a value can be “equal to all’ of the results of a subquery only if all of those results are, in fact, identical. Use ALL with nonequalities, using the <> operator.
16
16 Subqueries Operators (cont’d) One significant difference between ALL and ANY is the way the deal with the situation in which the subquery returns no values. – ALL is automatically TRUE – ANY is automatically FALSE Using COUNT – SELECT * FROM Customers outer WHERE 1 > (SELECT COUNT(*) FROM Customer inner WHERE outer.rating <= inner.rating AND inner.city =‘Rome’);
17
17 End of Sub Queries Thank you
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.