0
0
MySQLquery~5 mins

IN and NOT IN operators in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the IN operator do in SQL?
The IN operator checks if a value matches any value in a list or subquery. It helps filter rows where a column's value is in a specified set.
Click to reveal answer
beginner
How does the NOT IN operator work?
The NOT IN operator filters rows where a column's value is NOT in the specified list or subquery. It excludes those values.
Click to reveal answer
beginner
Write a simple SQL query using IN to find employees in departments 1, 2, or 3.
SELECT * FROM employees WHERE department_id IN (1, 2, 3);
Click to reveal answer
intermediate
What happens if the list in NOT IN contains NULL?
If the list contains NULL, NOT IN may return no rows because comparisons with NULL are unknown. It's safer to avoid NULL in NOT IN lists.
Click to reveal answer
intermediate
Can IN and NOT IN be used with subqueries? Give an example.
Yes. Example: SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE active = 1);
Click to reveal answer
Which SQL operator checks if a value is inside a list of values?
ABETWEEN
BNOT IN
CIN
DLIKE
What does NOT IN do in a SQL query?
AIncludes rows with values in the list
BExcludes rows with values in the list
CChecks for NULL values only
DSorts the results
What is the result if the list in NOT IN contains NULL?
AAll rows are returned
BOnly rows with NULL values are returned
CIt causes a syntax error
DNo rows are returned
Which of these is a valid use of IN with a subquery?
ASELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE active = 1);
BSELECT * FROM employees WHERE department_id NOT IN (SELECT id FROM departments WHERE active = 1);
CSELECT * FROM employees WHERE department_id = IN (1, 2, 3);
DSELECT * FROM employees WHERE department_id IN 1, 2, 3;
Which operator would you use to find rows where a column's value is NOT in a list?
ANOT IN
BIN
CBETWEEN
DLIKE
Explain how the IN operator works and give a simple example.
Think about filtering rows by matching values.
You got /3 concepts.
    Describe a potential issue when using NOT IN with NULL values in the list.
    Consider how SQL treats NULL in comparisons.
    You got /3 concepts.