0% found this document useful (0 votes)
229 views15 pages

SQL Subqueries for Developers

A subquery is a query embedded within another SQL query. The subquery executes first and its results are used by the main query. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements and comparison operators. They must be enclosed in parentheses and can return single or multiple rows/columns. Correlated subqueries use values from the outer query and may be slow.

Uploaded by

Jai Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views15 pages

SQL Subqueries for Developers

A subquery is a query embedded within another SQL query. The subquery executes first and its results are used by the main query. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements and comparison operators. They must be enclosed in parentheses and can return single or multiple rows/columns. Correlated subqueries use values from the outer query and may be slow.

Uploaded by

Jai Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SQL – SUBQUERIES

SUBQUERIES

• A Subquery can be defined as a query within another query.


• In other words we can say that a Subquery is a query that is embedded in
WHERE/HAVING/FROM clause of another SQL query.
SUBQUERIES - USAGE
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression
operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator.
• A subquery is a query within another query. The outer query is called as main query and inner query is called
as subquery.
• The subquery generally executes first, and its output is used to complete the query condition for the main or
outer query.
• Subquery must be enclosed in parentheses.
• Subqueries are on the right side of the comparison operator.
• ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to perform same
function as ORDER BY command.
• Use single-row operators with singlerow Subqueries. Use multiple-row operators with multiple-row
Subqueries.
SUBQUERY - SYNTAX

SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
EXAMPLES
QUERY 1

SUBQUERY USING IN / NOT IN:


QUESTION: Find all employees that belong to the location 1700
SOLUTION:
SELECT employee_id, first_name, last_name FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id =
1700) ORDER BY first_name , last_name;
QUERY 2

SUBQUERY USING OPERATORS (=,>,<, >=,<=,<>):


QUESTION: Finds the employee who have the least salary
SOLUTION:
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees) ORDER BY first_name ,
last_name;
QUERY 3 (EXISTS / NOT EXISTS)

The EXISTS operator checks for the existence of rows returned from the subquery.
It returns true if the subquery contains any rows. Otherwise, it returns false.

EXISTS (subquery )

NOT EXISTS (subquery )


QUERY 3 (EXISTS / NOT EXISTS)

QUESTION: Find all departments which have at least one employee with the salary is greater than 10,000.

SOLUTION:
SELECT department_name
FROM departments d
WHERE EXISTS( SELECT 1 FROM employees e WHERE salary > 10000 AND e.department_id =
d.department_id)
ORDER BY department_name;
QUERY 4 SQL SUBQUERY WITH ALL / ANY
OPERATOR

• comparison_operator ALL (subquery)


• comparison_operator ANY (subquery)

• Eg:
• x > ALL (subquery)
• x > ANY(subquery)
QUERY 4 SQL SUBQUERY WITH ALL OPERATOR

• Find all employees whose salaries are greater than the lowest salary of every department:
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary >= ALL (SELECT MIN(salary) FROM employees GROUP BY department_id)
ORDER BY first_name , last_name;
SQL SUBQUERY IN THE FROM CLAUSE

• Syntax:
SELECT * FROM (subquery) AS table_name

QUESTION: Find the round value of the average salary of departments:

Solution:
SELECT ROUND(average_salary, 0)
FROM (SELECT AVG(salary) average_salary FROM employees
GROUP BY department_id) department_salary;
SQL SUBQUERY IN THE SELECT CLAUSE
SELECT

employee_id, first_name, last_name, salary,

(SELECT

ROUND(AVG(salary), 0)

FROM

employees) average_salary,

salary - (SELECT

ROUND(AVG(salary), 0)

FROM

employees) difference

FROM

employees

ORDER BY first_name , last_name;


SUB QUERY TYPES

• Single row subquery : Returns zero or one row.


• Multiple row subquery : Returns one or more rows.
• Multiple column subqueries : Returns one or more columns.
• Correlated subqueries : A correlated subquery is a subquery that uses the values from the outer
query. Also, a correlated subquery may be evaluated once for each row selected by the outer
query. Because of this, a query that uses a correlated subquery may be slow.
• Nested subqueries : Subqueries are placed within another subquery.
CORRELATED SUBQUERIES

SELECT
employee_id, first_name, last_name, salary, department_id FROM employees e
WHERE
salary > (SELECT AVG(salary) FROM employees WHERE department_id =
e.department_id) ORDER BY department_id , first_name , last_name;
• For each employee, the database system has to execute the correlated subquery once to calculate
the average salary of the employees in the department of the current employee.

You might also like