subqueries_flashcards
What is a subquery?
A SELECT query nested inside another query.
Where can subqueries be used?
In SELECT, FROM, and WHERE clauses.
What is a scalar subquery?
A subquery that returns a single value.
What is a correlated subquery?
A subquery that refers to columns from the outer query.
When should you use IN in a subquery?
When comparing a column to multiple values returned by a subquery.
What is the difference between = and IN in subqueries?
= is used for scalar (one result), IN is used for multiple-row subqueries.
Can you use ORDER BY inside a subquery?
No, unless it’s in a subquery used in the FROM clause.
How do subqueries and joins differ?
Subqueries are nested; joins combine tables horizontally.
Give an example of a correlated subquery.
SELECT name FROM employee e WHERE salary > (SELECT AVG(salary) FROM employee WHERE dept = e.dept);