0
0
MySQLquery~5 mins

Subqueries in WHERE clause in MySQL - 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 conditions from the inner query.
Click to reveal answer
beginner
How does a subquery in the WHERE clause work?
The main query uses the result of the subquery to decide which rows to include, like checking if a value matches any value returned by the subquery.
Click to reveal answer
beginner
Example: What does this query do?<br>
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'NY');
It finds employees who work in departments located in New York by using a subquery to get department IDs in NY and filtering employees by those IDs.
Click to reveal answer
intermediate
Can subqueries in WHERE clause return multiple rows?
Yes, subqueries can return multiple rows when used with operators like IN, ANY, or EXISTS to check conditions against many values.
Click to reveal answer
intermediate
What happens if a subquery in the WHERE clause returns no rows?
If the subquery returns no rows, the condition usually evaluates to false, so the main query returns no matching rows for that condition.
Click to reveal answer
What does a subquery in the WHERE clause do?
AFilters rows based on the result of another query
BCreates a new table
CDeletes rows from a table
DUpdates values in a table
Which operator is commonly used with subqueries returning multiple rows in WHERE clause?
AIN
BLIKE
CBETWEEN
DIS NULL
What will happen if the subquery in WHERE clause returns no rows?
AThe main query returns all rows
BThe main query returns no rows for that condition
CThe main query throws an error
DThe main query ignores the condition
Which keyword can be used to check if a subquery returns any rows in WHERE clause?
AGROUP BY
BCOUNT
CORDER BY
DEXISTS
In this query, what does the subquery return?<br>SELECT * FROM orders WHERE customer_id = (SELECT id FROM customers WHERE name = 'Alice');
AThe name of the customer
BAll customers
CThe id of customer named Alice
DAll orders
Explain how a subquery in the WHERE clause helps filter data in a main query.
Think about how the inner query result guides which rows to keep.
You got /3 concepts.
    Describe what happens when a subquery in the WHERE clause returns multiple rows versus a single row.
    Consider how the main query compares values from the subquery.
    You got /3 concepts.