Lecture 09
Lecture 09
Sapna Kumari
DATABASE SYSTEMS- LECTURE 09 Lecturer, CS-SZABIST
[email protected]
OUTLINE
• Sub Query
• Types of Subquery
• Single-row subquery
• Multi-row subquery
• Correlated subquery
SUB QUERY
Subqueries in SQL (also known as inner queries or nested queries) are queries
nested inside another SQL query. They are used to perform operations that require
multiple steps or layers of data filtering or aggregation.
• Subqueries can be used in SELECT, FROM, WHERE, HAVING, and sometimes even
JOIN clauses.
• The subqueries generally executes first, and its output is used to complete the query
condition for the main or the outer query.
• You can use keywords like IN, ANY, ALL, EXISTS, and NOT EXISTS with subqueries.
• Subqueries are useful when a query is based on unknown values.
IMPORTANT RULE
•A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
•A subquery is a query within another query. The outer query is known as the main
query, and the inner query is known as a subquery.
•Subqueries are on the right side of the comparison operator.
•A subquery is enclosed in parentheses. In the Subquery, ORDER BY command cannot
be used. But GROUP BY command can be used to perform the same function as
ORDER BY command.
SUB QUERY
Advantages of Subqueries:
Modularity: Nested subqueries allows queries to manage in parts means the queries
are broken down into smaller parts and which is easier to manage.
Readability: Breaking a query into smaller parts makes it easier to read and
understand, better for simple use cases.
Simplifies Logic: Nested queries sometimes allows to avoid complex joins and
filtering conditions.
Independent execution: subquery is executed independently from the outer query,
which makes it reusable in different scenarios.
SUB QUERY
Disadvantages of Subqueries:
Performance: They can be slower, especially in those cases when an inner query runs
multiple times for each row in the outer query. This creates an issue in performance.
Limited Optimization: because of complex nested queries many databases struggle
to optimize them which leads to inefficient execution plans.
Complexity in Multi-Level Nesting: As if there are multilevel or deeply nested
queries is present then it is harder to manage and debug them.
Larger Data can be less efficient: If there is larger datasets than nested queries can
become inefficient, as they may require more resources and time to process than join
operations.
TYPES OF SUBQUERY
• Single-row subquery
• Multi-row subquery
• Scalar subquery
• Correlated subquery
• Non-correlated subquery
SINGLE-ROW SUBQUERY
A single-row subquery is one that returns one row from the inner SELECT statement.
This type of subquery uses a single-row operator.
SINGLE-ROW SUBQUERY-EXAMPLE
Display the employees whose job title is the same as that of employee 7369.
SINGLE-ROW SUBQUERY-EXAMPLE
MULTIPLE-ROW SUBQUERIES
• Subqueries that return more than one row are called multiple-row subqueries.
• Use multiple-row comparison operators.
MULTIPLE-ROW SUBQUERIES-EXAMPLE
Example: Find the employees who earn the same salary as the minimum salary for
departments.
MULTIPLE-ROW SUBQUERIES-EXAMPLE (ANY)
MULTIPLE-ROW SUBQUERIES-EXAMPLE (ALL)
CORRELATED SUBQUERY
• A correlated subquery is one that is executed after the outer query is executed. So correlated
sub-queries take an approach opposite to that of normal subqueries.
• They are termed "correlated" because the subquery's execution is influenced by the outer
query's rows.
• It's essential to employ a table alias (or correlation name) to clarify the table reference
intended for use within the subquery.
The Correlated subquery execution is as follows:
• The outer query will get executed first and for every row of outer query, inner query will get
executed as many times as no of rows in result of the outer query.
• The outer query output can use the inner query output for comparison.
• The inner query and outer query dependent on each other.
CORRELATED SUBQUERY-EXAMPLE
Find employees whose salary is higher than the average salary of their respective
departments.
SELECT e.employee_id, e.name, e.salary, e.department_id
FROM employees e
WHERE e.salary > ( SELECT AVG(e2.salary) FROM employees e2
WHERE e2.department_id = e.department_id );
CORRELATED SUBQUERY-EXAMPLE
SELECT a.ord_num, a.ord_amount, a.cust_code, a.agent_code
FROM orders a
WHERE a.agent_code = (
SELECT b.agent_code
FROM agents b
WHERE b.agent_name = 'Alex' );
CORRELATED SUBQUERY-EXAMPLE(NOT EXISTS)
SELECT employee_id, manager_id, first_name, last_name
FROM employees a
WHERE NOT EXISTS (
SELECT employee_id
FROM employees b
WHERE b.manager_id = a.employee_id
);
CORRELATED SUBQUERY-EXAMPLE(EXISTS)
SELECT employee_id, manager_id, first_name, last_name
FROM employees a
WHERE EXISTS (
SELECT employee_id
FROM employees b
WHERE b.manager_id = a.employee_id
);
TASKS:
emp_id name department salary manager_id
1 Alice HR 60000 5
2 Bob Sales 55000 5
3 Charlie IT 70000 6
4 David IT 72000 6
5 Emma HR 90000 NULL
6 Frank IT 95000 NULL
7 Grace Sales 58000 5
TASKS:
1. Find employees who earn more than Alice.
2. List employees who work in the same department as Emma or Frank.
3. Find employees whose salary is greater than their department's average.
4. Find the department with the highest paid employee.
5. List names of employees who are managers (i.e., their emp_id appears in manager_id).
6. Find employees who earn more than any other employee in their department (i.e., highest in their
department).
7. Get the name of the lowest paid employee.
8. Find employees who have the same salary as someone else.
9. Show employees whose salary is less than their manager's salary.
10. List names of employees who earn equal to the average salary of their department.