0
0
SQLquery~5 mins

Subquery in WHERE clause in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a subquery in the WHERE clause?
A subquery in the WHERE clause is a query inside another query that helps filter results based on values returned by the inner query.
Click to reveal answer
beginner
How does a subquery in the WHERE clause work?
The outer query uses the result of the subquery to decide which rows to include. The subquery runs first and returns values that the outer query compares against.
Click to reveal answer
beginner
Example: SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'New York');<br>What does this query do?
It finds all employees who work in departments located in New York. The subquery finds department IDs in New York, and the outer query selects employees in those departments.
Click to reveal answer
intermediate
Can a subquery in the WHERE clause return multiple values?
Yes, subqueries can return multiple values when used with operators like IN, ANY, or ALL to compare against multiple results.
Click to reveal answer
intermediate
What is the difference between using '=' and 'IN' with a subquery in the WHERE clause?
'=' expects the subquery to return a single value, while 'IN' allows the subquery to return multiple values to match any of them.
Click to reveal answer
What does a subquery in the WHERE clause do?
ACreates a new table
BFilters rows based on values returned by the inner query
CUpdates data in the database
DDeletes rows from a table
Which operator allows a subquery to return multiple values in the WHERE clause?
A=
BBETWEEN
CIN
DLIKE
What happens if a subquery in the WHERE clause returns no rows?
AThe outer query returns no rows
BThe outer query returns all rows
CThe database throws an error
DThe subquery runs again
Which of these is a valid use of a subquery in the WHERE clause?
ASELECT * FROM products WHERE price IN 'SELECT price FROM products';
BSELECT * FROM products WHERE price = 'SELECT AVG(price) FROM products';
CSELECT * FROM products WHERE price > ALL 'SELECT price FROM products';
DSELECT * FROM products WHERE price > (SELECT AVG(price) FROM products);
Can a subquery in the WHERE clause reference columns from the outer query?
AYes, this is called a correlated subquery
BNo, subqueries cannot reference outer query columns
COnly if the subquery is in the SELECT clause
DOnly if the subquery uses JOIN
Explain how a subquery in the WHERE clause helps filter data with an example.
Think about how the inner query returns values that the outer query uses to decide which rows to keep.
You got /3 concepts.
    Describe the difference between using '=' and 'IN' with subqueries in the WHERE clause.
    Consider what happens if the subquery returns one value or many values.
    You got /3 concepts.