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:

  • WHERE clause (most common)

  • FROM clause

  • SELECT clause

  • HAVING clause


3. Nested Queries with IN Operator

IN Operator

The operator IN compares a value with a set (or multiset) of values.

  • v IN V is TRUE if v is one of the elements in V

  • V is 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

SELECT DISTINCT Pnumber FROM PROJECT WHERE Pnumber IN ( SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith' ) OR Pnumber IN ( SELECT Pno FROM WORKS_ON, EMPLOYEE WHERE Essn = Ssn AND Lname = 'Smith' );

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 of IN

Example:

WHERE Salary = ( SELECT MAX(Salary) FROM EMPLOYEE );

5. Tuple Comparisons in Nested Queries

SQL allows tuple-valued comparisons using parentheses.

Example: Tuple Comparison with IN

SELECT DISTINCT Essn FROM WORKS_ON WHERE (Pno, Hours) IN ( SELECT Pno, Hours FROM WORKS_ON WHERE Essn = '123456789' );

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

  • = ANY or = SOME → equivalent to IN

  • Returns TRUE if the condition holds for at least one value

Example:

Salary > ANY (SELECT Salary FROM EMPLOYEE WHERE Dno = 5);

✔ Salary is greater than at least one employee in department 5


ALL Operator

  • Condition must hold for every value in the set

Example (Navathe):

SELECT Lname, Fname FROM EMPLOYEE WHERE Salary > ALL ( SELECT Salary FROM EMPLOYEE WHERE Dno = 5 );

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

SELECT E.Fname, E.Lname FROM EMPLOYEE AS E WHERE E.Ssn IN ( SELECT D.Essn FROM DEPENDENT AS D WHERE E.Fname = D.Dependent_name AND E.Sex = D.Sex );

Explanation:

  • E.Fname and E.Sex refer to the outer query

  • D.Dependent_name and D.Sex belong to DEPENDENT

  • Aliases E and D remove 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:

  1. Execute the nested query

  2. Check dependents with:

    • Same name

    • Same sex

  3. If employee’s Ssn appears in the result → select employee


9. Equivalent Non-Nested Query

Many nested queries can be rewritten as single-block queries using joins.

Example :

SELECT E.Fname, E.Lname FROM EMPLOYEE AS E, DEPENDENT AS D WHERE E.Ssn = D.Essn AND E.Sex = D.Sex AND E.Fname = D.Dependent_name;

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

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS