A complete Select Query (SQL), otherwise known as a nested query, can be specified within the WHERE clause of another query (called the outer query).
There are different ways we can nest queries:
IN Clause (SQL)
The comparison operator IN compares a value v with a set (or multi-set) of values V and evaluates to TRUE if v is one of the elements in V:
SELECT NAME, ADDRESSFROM EMPLOYEEWHERE DNO IN ( SELECT DNUMBER FROM DEPARTMENT WHERE DNAME = 'Research')
The nested query selects the number of the ‘Research’ department, the other query selects an employee if its department number is in the result of the nested query.
In this example, the nested query is not correlated with the outer query.
A correlated nested query is where the condition in the WHERE clause of a nested query references an attribute of a relation declared in the outer query.
The result of a correlated nested query is different for each tuple of the relation in the outer query.
SELECT E.nameFROM EMPLOYEE AS EWHERE E.ssn IN ( SELECT essn FROM DEPENDENT WHERE essn=E.ssn AND E.name=dependent_name)
Although these can still be re-written as a single block query with a simple Join Condition (SQL).