Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008

Similar presentations


Presentation on theme: "CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008"— Presentation transcript:

1 CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008
Lab 6: Subqueries CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008 College of Information Technology, Universiti Tenaga Nasional

2 Introduction to Subqueries
Suppose that you: Want to know all the other products supplied by the same company that supplies Outback Lager. Don’t know who supplies Outback Lager. You need to solve the sub-problem before you can solve the main problem. There are two ways to do this. Let’s call this part the main problem. And this part, the sub-problem. College of Information Technology, Universiti Tenaga Nasional

3 Introduction to Subqueries – cont.
Solution 1 Find out who the supplier is by executing: SELECT SupplierID FROM Products WHERE ProductName = ‘Outback Lager’ 7 College of Information Technology, Universiti Tenaga Nasional

4 Introduction to Subqueries – cont.
Solution 1 – cont. Then, use the Supplier ID obtained to display the other products: SELECT ProductName FROM Products WHERE SupplierID = This method requires you to execute two queries: One query to solve the sub-problem Another query to solve the main problem Instead, you could solve the sub-problem and the main problem in just one query. 7 College of Information Technology, Universiti Tenaga Nasional

5 Introduction to Subqueries – cont.
Solution 2 Type out the query that solves the main problem. SELECT ProductName FROM Products WHERE SupplierID = 7 College of Information Technology, Universiti Tenaga Nasional

6 Introduction to Subqueries – cont.
Solution 2 – cont. At the search condition, delete the Supplier ID value and type in a pair of parentheses. SELECT ProductName FROM Products WHERE SupplierID = () College of Information Technology, Universiti Tenaga Nasional

7 Introduction to Subqueries – cont.
Solution 2 – cont. Within the parentheses, type in the query that solves the sub-problem. College of Information Technology, Universiti Tenaga Nasional

8 Introduction to Subqueries – cont.
Solution 2 – cont. SELECT ProductName FROM Products WHERE SupplierID = ( SELECT SupplierID FROM Products WHERE ProductName = ‘Outback Lager’ ) 7 College of Information Technology, Universiti Tenaga Nasional

9 Where You Can Use Subqueries
In the WHERE clause In the HAVING clause In the FROM clause (later) College of Information Technology, Universiti Tenaga Nasional

10 Example of Subquery in WHERE
Display employees who have the same superior as Michael Suyama. SELECT LastName, ReportsTo FROM Employees WHERE ReportsTo = ( SELECT ReportsTo WHERE LastName = ‘Suyama’) College of Information Technology, Universiti Tenaga Nasional

11 Example of Subquery in HAVING
Display the number of products supplied by each supplier, but only for suppliers who supply products more than or as many as Supplier 7. Also display the supplier’s company name. College of Information Technology, Universiti Tenaga Nasional

12 Example of Subquery in HAVING – cont.
SELECTS.SupplierID, CompanyName, Count(ProductID) ‘No. of Products’ FROM Products P JOIN Suppliers S ON P.SupplierID = S.SupplierID GROUP BY S.SupplierID, CompanyName HAVING Count(ProductID) >= (SELECT Count(ProductID) FROM Products WHERE SupplierID = 7) College of Information Technology, Universiti Tenaga Nasional

13 Important!!! You may not have an ORDER BY clause in the subquery
If the subquery returns more than one value i.e. a list of values, than you must use the IN operator in the main query’s search condition College of Information Technology, Universiti Tenaga Nasional

14 More Operators Operator IS NULL Example: ReportsTo IS NULL IS NOT NULL
The negation of the operators that you have learnt NOT BETWEEN NOT IN NOT LIKE College of Information Technology, Universiti Tenaga Nasional

15 Using More Than One Search Conditions with AND or OR
Display the customers who are located in the North America. SELECT CompanyName, Country FROM Customers WHERE Country = ‘USA’ OR Country = ‘Canada’ OR Country = ‘Mexico’ Country IN (‘USA’, ‘Canada’, ‘Mexico’ College of Information Technology, Universiti Tenaga Nasional

16 Using More Than One Search Conditions with AND or OR – cont.
Display products that are under the Beverages and Seafood category and are not discontinued. SELECT ProductName FROM Products WHERE Discontinued = 0 AND CategoryID IN ( SELECT CategoryID FROM Categories WHERE CategoryName IN (‘Beverages’, ‘Seafood’)) College of Information Technology, Universiti Tenaga Nasional


Download ppt "CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008"

Similar presentations


Ads by Google