A subquery is a query (SELECT statement) inside a query, normally expressed inside parentheses; it is also called a nested query. The first query in the SQL statement is the outer query, the one inside is the inner query. For simple non-correlated queries, the inner query is executed first, and the output of inner query is input to outer query. The subquery is executed only once for the whole statement. The correlated subquery is dependent on the outer query. It is executed once per every row considered in the outer query.
There are 3 basic types of subqueries in SQL:
<aside> 🛎️ Ensure that you re-run the SQL Script below.
</aside>
SELECT customerCode, lastName, firstName FROM Customer
WHERE customerCode IN (SELECT customerCode FROM Invoice);
SELECT productCode, description, price FROM Product
WHERE price >= ALL (SELECT price FROM Product);
SELECT productCode, description, price, vendorCode FROM Product WHERE price < ANY (SELECT price FROM Product WHERE vendorCode = 21344)AND vendorCode <> 21344;
What does it output?