Subqueries Flashcards
Subquery
A subquery is a SQL query nested inside a larger query
SELECT lastName, firstName
FROM employees
WHERE officeCode IN
(SELECT officeCode
FROM offices
WHERE country = ‘USA’);
Is the “SELECT lastName, firstName” query the outer or inner query?
The outer query
SELECT lastName, firstName
FROM employees
WHERE officeCode IN
(SELECT officeCode
FROM offices
WHERE country = ‘USA’);
Is the “SELECT officeCode” query within the parenthesis the outer or inner query?
The inner query
What do we need to add to the subquery of a FROM clause in order for it to work?
An AS keyword
This will give the temporary table created by the subquery a name that the outer query can use to pull the information from.
When you have a query inside of another query, the term “subquery” refers to which query?
The inner query
Correlated Subquery
Is a subquery that contains a reference to a table that also appears in the outer query.
The correlation comes from the fact that the subquery uses information from the outer query and the subquery executes once for every row in the outer query.