0
0
SQLquery~5 mins

Subquery with IN operator in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the IN operator do in SQL when used with a subquery?
The IN operator checks if a value matches any value in the list returned by the subquery. It helps filter rows based on whether a column's value exists in the subquery result.
Click to reveal answer
beginner
Write a simple example of a subquery with IN operator to find employees who work in departments located in 'New York'.
SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'New York');
Click to reveal answer
intermediate
Can the subquery used with IN operator return multiple columns?
No, the subquery used with IN operator must return only one column. This column's values are compared against the outer query's column values.
Click to reveal answer
intermediate
What happens if the subquery with IN operator returns no rows?
If the subquery returns no rows, the IN condition evaluates to false for all rows in the outer query, so no rows are returned.
Click to reveal answer
intermediate
Why might you use a subquery with IN operator instead of a JOIN?
Using IN with a subquery can be simpler and more readable when you only need to check existence of values. JOINs are better for combining columns from multiple tables.
Click to reveal answer
What must a subquery used with the IN operator return?
ANo rows
BMultiple columns of values
CA single row with multiple columns
DA single column of values
If the subquery returns no rows, what will the IN operator do?
AReturn all rows from the outer query
BCause an error
CReturn no rows from the outer query
DReturn NULL values
Which SQL keyword is used to check if a value exists in a list returned by a subquery?
AIN
BEXISTS
CJOIN
DLIKE
Which of these is a valid use of IN with a subquery?
ASELECT * FROM employees WHERE department_id IN (SELECT department_id, location FROM departments);
BSELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments);
CSELECT * FROM employees WHERE department_id IN (SELECT location FROM departments);
DSELECT * FROM employees WHERE department_id IN ();
Why might you choose IN with a subquery over a JOIN?
AIN is simpler when only checking existence of values
BIN returns more columns
CJOIN cannot combine tables
DIN is faster for all queries
Explain how the IN operator works with a subquery in SQL.
Think about checking if a value is inside a list of values.
You got /4 concepts.
    Describe a scenario where using a subquery with IN operator is helpful.
    Imagine you want to find employees working in certain departments.
    You got /4 concepts.