Lecture - Subqueries
Lecture - Subqueries
WEEK 9 LECTURE 1
SUBQUERIES
2
SUBQUERY
PROBLEM
4
SOLUTION- SUBQUERY
5
SUBQUERY
6
RULES FOR SUBQUERY
7
TYPES OF SUBQUERIES
8
SINGLE ROW SUBQUERY
SINGLE ROW SUBQUERY
10
EXECUTION OF SINGLE ROW QUERY
Schema:
Employees(employee_id, first_name, last_name,
department_id, job_id, salary)
Departments(department_id, dname)
11
EXECUTION OF SINGLE ROW QUERY
12
EXECUTION OF SINGLE ROW QUERY
13
GROUP FUNCTION IN SUBQUERY
14
HAVING CLAUSE IN SUBQUERY
15
EXAMPLE: OUTPUT OF QUERY
16
SOLUTION: OUTPUT
17
MULTIPLE ROW SUBQUERY
MULTIPLE ROW SUBQUERY
19
MULTIPLE ROW OPERATORS
20
PRACTICE QUESTIONS
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, DepartmentID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
22
EXAMPLE
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
SELECT ProductName
FROM Products
WHERE Price > ANY (SELECT Price
FROM Products
WHERE Category = 'Electronics');
23
EXAMPLE
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
SELECT ProductName
FROM Products
WHERE Price > ALL (SELECT Price
FROM Products
WHERE Category = 'Furniture');
24
EXAMPLE
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
25
EXAMPLE
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
26
ANY AND ALL RULES
<ANY means less than the maximum.
>ANY means more than the minimum.
=ANY is equivalent to IN.
27
ANY AND ALL RULES
Find employees who are not IT programmers and whose salary is
less than that of any IT programmer.
28
ANY AND ALL RULES
Find employees who are not IT programmers and whose salary is
less than all of the IT programmers.
29
ANY OPERATOR WITH SUBQUERY
30
ALL OPERATOR WITH SUBQUERY
31
EXISTS OPERATOR WITH SUBQUERY
32
NULL WITH SUBQUERY
NULL IN SUBQUERY
34
NOT EXISTS
37
Revisit!
38
Revisit!
39
Example-1
WHERE clause-IN
Operator
40
Example-2
WHERE clause-
NOT IN Operator
41
Example-3
WHERE clause
-Multiple Column
42
Example-4
HAVING clause
43
Example-5
FROM clause
44
SUBQUERY FOR UPDATING DATA
• Product(ProductID, ProductName, Price, Category)
• Department(DepartmentID, DepartmentName, Location)
• Customer(CustomerID, CustomerName, Email)
• Order(OrderID, OrderDate, CustomerID, ProductID)
UPDATE Product
SET Price = Price * 1.10
46
SUBQUERY FOR CREATING TABLES
Create a new table for high-value customers who placed
orders for products over $1000.
FROM ...
WHERE ...;
In SQL, parentheses are not required around a subquery when it's part of 48
a CREATE TABLE AS statement or a SELECT ... INTO clause (in some RDBMS).
CORRELATED SUBQUERIES
Students
Id Name Class Marks
1 Ali 10 85
2 Sara 10 78
3 Ahmed 9 92
4 Zoya 9 70
5 Bilal 10 90
52
CORRELATED SUBQUERIES
53
CORRELATED SUBQUERIES
56
CORRELATED SUBQUERIES
57
CORRELATED SUBQUERIES
58
CORRELATED SUBQUERIES
Correlated subqueries
A correlated subquery depends on the outer query.
It runs once for each row processed by the outer query.
References a column from the outer query.