Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010

Similar presentations


Presentation on theme: "Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010"— Presentation transcript:

1 Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
College of Information Technology, Universiti Tenaga Nasional

2 Introduction to Subqueries
Suppose that you: Want to know all the other employees that work in the same department as Zarina Majid Don’t know Zarina Majid’s department. 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 the department by executing: SELECT Dept FROM emp WHERE Name = ‘Zarina Majid’ Admin College of Information Technology, Universiti Tenaga Nasional

4 Introduction to Subqueries – cont.
Solution 1 – cont. Then, use the Deparment name obtained to display the other employees: SELECT Name, Title FROM emp WHERE Dept = 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. Admin College of Information Technology, Universiti Tenaga Nasional

5 Introduction to Subqueries – cont.
Solution 2 Type out the query that solves the main problem. SELECT Name, Title FROM emp WHERE Dept = ‘Admin’ College of Information Technology, Universiti Tenaga Nasional

6 Introduction to Subqueries – cont.
Solution 2 – cont. At the search condition, delete the Deparment value and type in a pair of parentheses. SELECT Name, Title FROM emp WHERE Dept = () 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 Name, Title FROM emp WHERE Dept = (SELECT Dept FROM emp WHERE Name = ‘Zarina Majid’ ) Admin 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 title as Nordin. SELECT Name, Title, Dept FROM emp WHERE Title = ( SELECT Title WHERE Name like ‘Nordin%’) College of Information Technology, Universiti Tenaga Nasional

11 Example of Subquery in HAVING
Display all the departments that have an average salary bill greater than the average of Admin deparment College of Information Technology, Universiti Tenaga Nasional

12 Example of Subquery in HAVING – cont.
SELECT Dept, AVG(salary) FROM emp GROUP BY Dept HAVING AVG(salary) < (SELECT AVG(salary) FROM emp WHERE Dept=‘Admin’) 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 KL, Perak and Johor SELECT CompName, State FROM cust WHERE State = ‘KL’ OR State = ‘Perak’ OR State = ‘Johor’ State IN (‘KL’, ‘Perak, ‘Johor’ College of Information Technology, Universiti Tenaga Nasional


Download ppt "Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010"

Similar presentations


Ads by Google