Nested Queries in SQL
Nested Queries in SQL
1. What is a Nested Query?
A nested query (subquery) is a complete SELECT–FROM–WHERE query that is written inside another SQL query.
-
The outer query is called the outer query
-
The inner query is called the nested query or subquery
Nested queries are useful when:
-
Values must first be retrieved from the database
-
These values are then used for comparison in another query
2. Where Can Nested Queries Appear?
A nested query can appear in:
-
WHEREclause (most common) -
FROMclause -
SELECTclause -
HAVINGclause
3. Nested Queries with IN Operator
IN Operator
The operator IN compares a value with a set (or multiset) of values.
-
v IN Vis TRUE ifvis one of the elements inV -
Vis typically the result of a nested query
Example
Retrieve project numbers of projects where an employee with last name ‘Smith’ is either:
-
the manager, or
-
a worker
Explanation:
-
First nested query finds projects managed by Smith
-
Second nested query finds projects worked on by Smith
-
The outer query selects projects appearing in either result
4. Scalar Nested Queries
If a nested query returns:
-
One attribute
-
One tuple
Then the result is a single (scalar) value
In this case:
-
=can be used instead ofIN
Example:
5. Tuple Comparisons in Nested Queries
SQL allows tuple-valued comparisons using parentheses.
Example: Tuple Comparison with IN
Explanation:
-
The nested query returns
(Pno, Hours)pairs for employee John Smith -
The outer query finds employees who work on the same project with the same hours
-
Comparison is done tuple-by-tuple, not attribute-by-attribute
6. ANY (or SOME) and ALL Operators
ANY / SOME
-
= ANYor= SOME→ equivalent toIN -
Returns TRUE if the condition holds for at least one value
Example:
✔ Salary is greater than at least one employee in department 5
ALL Operator
-
Condition must hold for every value in the set
Example (Navathe):
Explanation:
-
Returns employees whose salary is greater than all employees in department 5
-
Equivalent to comparing with
MAX(Salary)
7. Attribute Name Ambiguity and Scope Rules
When nested queries are used:
-
SQL applies scope rules, similar to programming languages
-
An unqualified attribute name refers to:
the relation in the innermost query
To avoid ambiguity:
-
Use table aliases (tuple variables)
Example
Retrieve employees who have a dependent with the same first name and sex
Explanation:
-
E.FnameandE.Sexrefer to the outer query -
D.Dependent_nameandD.Sexbelong to DEPENDENT -
Aliases
EandDremove ambiguity
8. Correlated Nested Queries
A correlated nested query is one where:
-
The nested query references attributes of the outer query
👉 Such a nested query:
-
Is evaluated once for each tuple of the outer query
Understanding Correlated Query
For each EMPLOYEE tuple:
-
Execute the nested query
-
Check dependents with:
-
Same name
-
Same sex
-
-
If employee’s
Ssnappears in the result → select employee
9. Equivalent Non-Nested Query
Many nested queries can be rewritten as single-block queries using joins.
Example :
10. Summary
-
Nested queries are queries inside other queries
-
IN compares a value with a set of values
-
Scalar subqueries return single values
-
Tuple comparisons use
(A, B) IN (subquery) -
ANY / SOME → at least one match
-
ALL → must satisfy condition for all values
-
Correlated queries reference outer query attributes
-
Aliases help avoid ambiguity and improve clarity
Comments
Post a Comment