0% found this document useful (0 votes)
11 views7 pages

Understanding SQL Nested Queries

The document explains nested queries in SQL, detailing two main types: independent (non-correlated) and correlated nested queries. Independent queries execute the inner query independently, using its result for the outer query, while correlated queries depend on the outer query and re-execute for each row. It also provides examples and use cases for operators like IN, NOT IN, ANY, and ALL in independent queries.

Uploaded by

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

Understanding SQL Nested Queries

The document explains nested queries in SQL, detailing two main types: independent (non-correlated) and correlated nested queries. Independent queries execute the inner query independently, using its result for the outer query, while correlated queries depend on the outer query and re-execute for each row. It also provides examples and use cases for operators like IN, NOT IN, ANY, and ALL in independent queries.

Uploaded by

Jeya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Nested queries in SQL involve placing one SQL query (the inner query or

subquery) within another SQL query (the outer query). There are two main
types of nested queries based on their relationship and execution:

1. Independent Nested Queries (Non-Correlated Subqueries):


 Definition: The inner query executes completely and independently of the outer
query. Its result set is then passed to the outer query, which uses this result to
complete its own operation.
 Execution: The inner query runs once, and its output serves as input for the outer
query.
 Common Operators: IN, NOT IN, ANY, ALL.
SELECT employee_name

FROM Employees

WHERE department_id IN (SELECT department_id FROM Departments WHERE location = 'New


York');

In this example, the inner query first finds all department_ids located in
'New York', and then the outer query selects employees belonging to those
departments.

2. Correlated Nested Queries:


 Definition:
The inner query depends on the outer query for its execution, referencing one or
more columns from the outer query. The inner query is re-executed for each row
processed by the outer query.
 Execution:
The outer query selects a row, then the inner query executes using a value from
that row. This process repeats for every row in the outer query's result set.
 Common Operators:
Often used with EXISTS or comparisons involving columns from both the inner and
outer queries.
 Example:
Code
SELECT employee_name
FROM Employees e
WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.employee_id =
e.employee_id AND o.order_total > 1000);
Here, for each employee in the Employees table, the inner query checks if
there's an Order with that employee's ID and an order_total greater than
1000. This inner query is re-evaluated for every employee.

explain independent query with operators with


example and output
An independent (or non-correlated) query is a nested query where the inner query
can be executed on its own and does not depend on the outer query
. The inner query runs once and produces a result set, which is then used by the
outer query to filter its own data.

This is in contrast to a correlated subquery, where the inner query re-executes for
every row processed by the outer query and depends on data from that row.

Independent query operators

Independent subqueries are most commonly used with the following operators:
IN

The IN operator checks if a value exists within the list of values returned by the
subquery.

 Use case: Filtering a table based on a set of matching values from another table.

 Example: Find the names of all employees who work in the 'Sales' or 'Marketing'
departments.

o Setup:
Employees table:

employee_id employee_name department_id

101 Alice D1

102 Bob D2

103 Charlie D3

104 David D1

Departments table:
department_id department_name

D1 Sales

D2 Marketing

D3 Engineering

* Query:

```sql

SELECT employee_name

FROM Employees

WHERE department_id IN (

SELECT department_id

FROM Departments

WHERE department_name IN ('Sales', 'Marketing')

);

```

* Execution:

1. The inner query (SELECT department_id FROM Departments WHERE


department_name IN ('Sales', 'Marketing')) runs first.

2. It returns a result set of ('D1', 'D2') .

3. The outer query then filters the Employees table, keeping only rows
where department_id is in the list ('D1', 'D2') .

* Output:

employee_name
Alice

Bob

David

NOT IN

The NOT IN operator does the opposite of IN and excludes rows where a value
exists in the list returned by the subquery.

 Use case: Finding records that do not have a matching entry in another table.

 Example: Find the names of employees who do not work in the 'Sales' or 'Marketing'
departments.

o Query:

sql
SELECT employee_name
FROM Employees
WHERE department_id NOT IN (
SELECT department_id
FROM Departments
WHERE department_name IN ('Sales', 'Marketing')
);
Use code with caution.

o Execution:

o The inner query returns ('D1', 'D2') .

o The outer query then filters the Employees table, keeping only rows
where department_id is not in the list ('D1', 'D2') .

o Output:

employee_name

Charlie

ANY
The ANY operator returns TRUE if the comparison is TRUE for any of the values
returned by the subquery.

 Use case: Filtering based on a flexible condition, such as finding values greater than
the smallest value in another set. SOME is a synonym for ANY .

 Example: Find all products with a price higher than at least one product in the
'Beverages' category.

o Setup:
Products table:

product_id product_nam category price


e

1 Cola Beverages 1.50

2 Tea Beverages 2.00

3 Chips Snacks 1.75

4 Soda Beverages 1.25

5 Chocolate Snacks 2.50

* Query:

```sql

SELECT product_name

FROM Products

WHERE price > ANY (

SELECT price

FROM Products

WHERE category = 'Beverages'

);
```

* Execution:

1. The inner query returns the list of beverage prices (1.50, 2.00,
1.25) .

2. The outer query finds all products whose price is greater than any of those
values. For example, a product priced at 1.75 is greater than the beverage
priced at 1.50 and 1.25.

* Output:

product_name

Tea

Chips

Chocolate

ALL

The ALL operator returns TRUE if the comparison is TRUE for all of the values
returned by the subquery.

 Use case: Filtering based on a strict condition, such as finding values greater than
the highest value in another set.

 Example: Find all products with a price higher than every single product in the
'Beverages' category.

o Query:

sql
SELECT product_name
FROM Products
WHERE price > ALL (
SELECT price
FROM Products
WHERE category = 'Beverages'
);
Use code with caution.
o Execution:

o The inner query returns the list of beverage prices (1.50, 2.00, 1.25) .

o The outer query finds all products whose price is greater than all of those values.
The highest beverage price is 2.00, so only products priced higher than 2.00 will be
included.

o Output:

product_name

Chocolate

AI responses may include mistakes. Learn more



You might also like