0
0
MySQLquery~20 mins

IN and NOT IN operators in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IN and NOT IN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of IN operator with multiple values
What rows will be returned by this query?

SELECT name FROM employees WHERE department_id IN (1, 3, 5);

Assume the employees table has these rows:
  • (name: 'Alice', department_id: 1)
  • (name: 'Bob', department_id: 2)
  • (name: 'Charlie', department_id: 3)
  • (name: 'Diana', department_id: 4)
  • (name: 'Eve', department_id: 5)
MySQL
SELECT name FROM employees WHERE department_id IN (1, 3, 5);
ABob, Diana
BAlice, Charlie, Eve
CAlice, Bob, Charlie, Diana, Eve
DNo rows returned
Attempts:
2 left
💡 Hint
IN operator returns rows where the column matches any value in the list.
query_result
intermediate
2:00remaining
Output of NOT IN operator excluding values
What rows will be returned by this query?

SELECT product_name FROM products WHERE category_id NOT IN (2, 4);

Assume the products table has these rows:
  • (product_name: 'Pen', category_id: 1)
  • (product_name: 'Notebook', category_id: 2)
  • (product_name: 'Eraser', category_id: 3)
  • (product_name: 'Marker', category_id: 4)
  • (product_name: 'Ruler', category_id: 5)
MySQL
SELECT product_name FROM products WHERE category_id NOT IN (2, 4);
ANo rows returned
BNotebook, Marker
CPen, Eraser, Ruler
DPen, Notebook, Eraser, Marker, Ruler
Attempts:
2 left
💡 Hint
NOT IN excludes rows with listed values.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in IN operator usage
Which option contains a syntax error in using the IN operator?
MySQL
SELECT * FROM orders WHERE order_id IN 1, 2, 3;
A;)3 ,2 ,1( NI di_redro EREHW sredro MORF * TCELES
BSELECT * FROM orders WHERE order_id IN (1, 2, 3);
CSELECT * FROM orders WHERE order_id IN (1 2 3);
DSELECT * FROM orders WHERE order_id IN 1, 2, 3;
Attempts:
2 left
💡 Hint
IN operator requires parentheses around the list of values.
optimization
advanced
2:00remaining
Optimizing a query using IN operator
Which query is more efficient to find customers from specific cities?

Assume customers table has an index on city.
ASELECT * FROM customers WHERE city IN ('New York', 'Los Angeles', 'Chicago');
BSELECT * FROM customers WHERE city = 'New York' OR city = 'Los Angeles' OR city = 'Chicago';
CSELECT * FROM customers WHERE city LIKE '%New York%' OR city LIKE '%Los Angeles%' OR city LIKE '%Chicago%';
DSELECT * FROM customers WHERE city NOT IN ('New York', 'Los Angeles', 'Chicago');
Attempts:
2 left
💡 Hint
IN operator is optimized for matching multiple values.
🧠 Conceptual
expert
3:00remaining
Behavior of NOT IN with NULL values
Consider this query:

SELECT * FROM employees WHERE department_id NOT IN (1, NULL, 3);

What will be the result if the department_id column has values 1, 2, 3, and NULL?
ANo rows are returned because NOT IN with NULL always yields unknown.
BRows with department_id 1 and 3 are returned.
CRows with department_id 2 only are returned.
DAll rows except those with NULL department_id are returned.
Attempts:
2 left
💡 Hint
NOT IN with NULL in the list causes the condition to evaluate to unknown for all rows.