50% found this document useful (2 votes)
386 views

DBMS - Unit 3 - Notes (Subquery - Nested & Correlated)

A subquery is a query nested inside another query. Subqueries can be used in the SELECT, WHERE, HAVING clauses and return either a single value or multiple rows/columns. There are three types of subqueries - single-row, multiple-row, and multiple-column subqueries. Comparison operators like =, <, > are used with single-row subqueries while IN, ANY, ALL can be used for multiple-row subqueries. Correlated subqueries use values from the outer query in the inner query.

Uploaded by

ABC
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
386 views

DBMS - Unit 3 - Notes (Subquery - Nested & Correlated)

A subquery is a query nested inside another query. Subqueries can be used in the SELECT, WHERE, HAVING clauses and return either a single value or multiple rows/columns. There are three types of subqueries - single-row, multiple-row, and multiple-column subqueries. Comparison operators like =, <, > are used with single-row subqueries while IN, ANY, ALL can be used for multiple-row subqueries. Correlated subqueries use values from the outer query in the inner query.

Uploaded by

ABC
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL Subquery

WHAT IS SQL SUB QUERY?

A sub query is a SQL query nested


inside a larger query.
WHERE SUB QUERY OCCURS?

A subquery may occurs in:


• A SELECT clause
• A WHERE clause
• The sub query can be nested inside a SELECT,
INSERT, UPDATE, or DELETE statement or inside
another sub query.
• A sub query is usually added within the WHERE clause
of another SQL SELECT statement.
• You can use the comparison operators, such as >, <, or
=. The comparison operator can also be a multiple-row
operator, such as IN, ANY, or ALL.
SUBQUERIES

 A subquery is a SELECT statement  You can place the subquery in a number


that is embedded in a clause of of SQL clauses, including:
another SELECT statement.
- The WHERE clause
 You can build powerful statements out of - The HAVING clause
simple ones by using subqueries. - The FROM clause
 They can be very useful when you need  operator includes a comparison condition
to select rows from a table with a such as >, =, or IN
condition that depends on the data in the
table itself.
 Note: Comparison conditions fall into two

classes: single-row operators (>, =, >=,
The subquery is often referred to as a <, <>, <=) and multiple-row operators
nested SELECT, sub-SELECT, or inner (IN, ANY, ALL).
SELECT statement.
 subqueries can be placed in the
 The subquery generally executes first,
and its output is used to complete the CREATE VIEW statement, CREATE
query condition for the main or outer TABLE statement, UPDATE statement,
query. INTO clause of an INSERT statement,
and SET clause of an UPDATE
statement.
SUBQUERY SYNTAX

• The subquery (inner query) executes before the main


query(outer query).
• The result of the subquery is used by the main query.
TYPE OF SUBQUERIES

• Single row subquery : Returns zero or one


row.

• Multiple row subquery : Returns one or more


rows.

• Multiple column subqueries : Returns one or


more columns.
SINGLE ROW SUBQUERIES
• The single row subquery returns one row. The single row query uses
any operator in the query i.e. (=,<=, >=, <>, <, > ).
• EXAMPLE:

• department table

Employee table
• Suppose you want to find out the ename, job,sal of the
employees whose salaries are less than that of an employee
whose empno= 7876 from EMP table. Now you need to
perform two queries in order to get the desired result.
1. We will find out the salary of the employee whose
empno=7876. the query is as under:

2. Now in second query we will apply the condition from which


we will find the ename, job and sal. We will use the query as
under:
• The above two queries can be used as single query by
using the concept of subquery. It will combine the result
into a single query as under:
EXECUTING SINGLE-ROW SUBQUERIES
The example on the slide displays employees whose job ID is the same as that of employee 141
and whose salary is greater than that of employee 143.

The example consists of three query blocks: the outer query and two inner queries.

The inner query blocks are executed first, producing the query results ST_CLERK and
2600, respectively.

The outer query block is then processed and uses the values returned by the inner queries
to complete its search conditions.

USING GROUP FUNCTIONS IN A
SUBQUERY
 The example on the slide displays the employee last name, job ID, and
salary of all employees whose salary is equal to the minimum salary.

 The MIN group function returns a single value (2500) to the outer query.
PROBLEMS WITH SUBQUERIES
A common problem with subqueries is no rows being returned by the inner query.

 There is no employee named Haas. So the subquery returns no rows.

 The outer query takes the results of the subquery (null) and uses these results in its
WHERE clause. The outer query finds no employee with a job ID equal to null, and so
returns no rows.

 If a job existed with a value of null, the row is not returned because comparison of two null
values yields a null, therefore the WHERE condition is not true.
MULTIPLE ROW SUBQUERY

• Returns sets of rows. Query uses the set comparison


operators (IN, ALL, ANY). if u use a multirow subquery
with the equals comparison operators, the database will
return an error if more than one row is returned.

SYMBOL MEANING
IN Equal to any member in a list.
ANY Return rows that match any value on a list.
ALL Return rows that match all the values in a list.
IN OPERATOR
• The IN operator retirns true if the comparison value is
contained in the list.
• The following statement finds the employee whose
salary is the same as the minimum salary of the
employees in the department.
ANY OPERATOR IN MULTIPLE-ROW
SUBQUERIES
 The ANY operator compares a value to each value returned by a subquery.

 The slide example displays employees who are not IT programmers and whose
salary is less than that of any IT programmer. The maximum salary that a
programmer earns is $9,000.

 <ANY means less than the maximum.


 >ANY means greater than the minimum.
 =ANY is equivalent to IN.

 <ALL means less than the minimum.


 >ALL means greater than the maximum.

 When using SOME or ANY, you often use the DISTINCT keyword to prevent
rows from being selected several times.
EXAMPLE :

The NOT operator can be used with IN, ANY, and ALL operators.
ANY OPERATOR
• The ANY operator return true if the comparison value
matches any of the values in the list.
• Display the employees whose salary is more than the
minimum salary of the employees in any department.
ALL OPERATOR
• Returns true only if the comparison value matches all the
values in the list.
• Display the employee detail with salary less than those whose
job is ‘MANAGER’.
MULTIPLE COLUMN SUBQUERY

• A subquery that compares more than one column


between the parent query and subquery is called the
multiple column subqueries.
• List the employees that makes the same salary as other
employee with empno=7521 with the same job also.
CORRELATED SUBQUERIES

• A correlated subquery uses a value from


the main query as part of the inner query
– Data from each row in the main query is
“passed” to the subquery for processing
• Typically a processing-intensive operation
– Subquery must be re-run with new value(s)
for each row
EXAMPLE 1

SELECT name, street, city, state


FROM addresses
WHERE EXISTS
(SELECT *
FROM states
WHERE states.state = addresses.state);
CORRELATED SUBQUERY EXAMPLE
• finds employees
whose salary is
greater than the
average salary of all
employees:
EXAMPLE 2

• SELECT employee_id, first_name,


last_name, salary
• FROM employees
• WHERE salary > (SELECT AVG(salary)
FROM
• employees);
EXAMPLE 3
• finds all employees whose salary is higher than
the average salary of the employees in their
departments:

• 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;

You might also like