📖 Scenario: You are managing a small bookstore database. You want to find books by certain authors and exclude others.
🎯 Goal: Build SQL queries using IN and NOT IN operators to filter books by author names.
📋 What You'll Learn
Create a table called books with columns id, title, and author
Insert specific book records into the books table
Write a query using IN to select books by given authors
Write a query using NOT IN to select books excluding certain authors
💡 Why This Matters
🌍 Real World
Filtering data by multiple values is common in databases for reports, searches, and data analysis.
💼 Career
Knowing how to use IN and NOT IN helps database developers and analysts write efficient queries to get precise data.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with columns id as integer primary key, title as text, and author as text.
MySQL
Hint
Use CREATE TABLE books and define three columns with correct types and primary key.
2
Insert book records
Insert these exact records into the books table: (1, 'The Great Gatsby', 'F. Scott Fitzgerald'), (2, '1984', 'George Orwell'), (3, 'To Kill a Mockingbird', 'Harper Lee'), (4, 'Animal Farm', 'George Orwell'), (5, 'The Catcher in the Rye', 'J.D. Salinger').
MySQL
Hint
Use a single INSERT INTO books statement with multiple value tuples.
3
Select books by specific authors using IN
Write a SQL query to select all columns from books where the author is either 'George Orwell' or 'Harper Lee' using the IN operator.
MySQL
Hint
Use WHERE author IN ('George Orwell', 'Harper Lee') to filter authors.
4
Select books excluding specific authors using NOT IN
Write a SQL query to select all columns from books where the author is not 'F. Scott Fitzgerald' and not 'J.D. Salinger' using the NOT IN operator.
MySQL
Hint
Use WHERE author NOT IN ('F. Scott Fitzgerald', 'J.D. Salinger') to exclude authors.
Practice
(1/5)
1. What does the IN operator do in a MySQL query?
easy
A. Checks if a value is greater than all values in a list
B. Checks if a value is less than all values in a list
C. Checks if a value matches any value in a list
D. Checks if a value is equal to a single value only
Solution
Step 1: Understand the purpose of IN operator
The IN operator is used to test if a value exists within a set of values.
Step 2: Compare with other options
The options describing comparisons with all values (greater or less than all) are incorrect, as IN checks if a value matches any in the list. The option about equality to a single value is also wrong, since IN handles multiple values.
Final Answer:
Checks if a value matches any value in a list -> Option C
Quick Check:
IN operator = check value in list [OK]
Hint: IN means "is in this list"; NOT IN means "not in this list" [OK]
Common Mistakes:
Confusing IN with equality (=) operator
Thinking IN checks all values instead of any
Mixing IN with greater than or less than comparisons
2. Which of the following is the correct syntax to select rows where the column status is either 'active', 'pending', or 'closed'?
easy
A. SELECT * FROM table WHERE status IN ('active', 'pending', 'closed');
B. SELECT * FROM table WHERE status = IN ('active', 'pending', 'closed');
C. SELECT * FROM table WHERE status NOT IN ('active', 'pending', 'closed');
D. SELECT * FROM table WHERE status IN 'active', 'pending', 'closed';
Solution
Step 1: Check correct IN syntax
The correct syntax uses IN followed by parentheses containing a comma-separated list of values.
Step 2: Identify errors in other options
SELECT * FROM table WHERE status = IN ('active', 'pending', 'closed'); has an extra '=' which is invalid. SELECT * FROM table WHERE status NOT IN ('active', 'pending', 'closed'); uses NOT IN which selects rows not matching the list. SELECT * FROM table WHERE status IN 'active', 'pending', 'closed'; misses parentheses around the list.
Final Answer:
SELECT * FROM table WHERE status IN ('active', 'pending', 'closed'); -> Option A
Quick Check:
Correct IN syntax uses parentheses and no '=' [OK]
Hint: IN must be followed by parentheses with values inside [OK]
Common Mistakes:
Using '=' before IN
Omitting parentheses around the list
Confusing IN with NOT IN
3. Given the table employees with a column department, what will this query return?
SELECT name FROM employees WHERE department NOT IN ('HR', 'Finance');
medium
A. Names of employees in HR or Finance departments
B. Names of employees not in HR or Finance departments
C. Names of employees only in HR department
D. Syntax error due to NOT IN usage
Solution
Step 1: Understand NOT IN operator
NOT IN selects rows where the column value is not in the given list.
Step 2: Apply to the query context
The query returns employee names whose department is not 'HR' or 'Finance'.
Final Answer:
Names of employees not in HR or Finance departments -> Option B
Quick Check:
NOT IN excludes listed values [OK]
Hint: NOT IN excludes listed values from results [OK]
Common Mistakes:
Thinking NOT IN returns only listed values
Confusing NOT IN with IN
Assuming syntax error with NOT IN
4. Identify the error in this query:
SELECT * FROM orders WHERE order_status NOT IN 'shipped', 'delivered';
medium
A. Missing parentheses around the list after NOT IN
B. Incorrect use of NOT IN instead of IN
C. order_status should be in quotes
D. No error, query is correct
Solution
Step 1: Check syntax of NOT IN usage
NOT IN must be followed by parentheses containing the list of values.
Step 2: Identify missing parentheses
The query lists values without parentheses, causing syntax error.
Final Answer:
Missing parentheses around the list after NOT IN -> Option A
Quick Check:
NOT IN requires parentheses around values [OK]
Hint: Always use parentheses after IN or NOT IN [OK]
Common Mistakes:
Omitting parentheses around value list
Using quotes incorrectly around column names
Confusing IN and NOT IN usage
5. You want to find customers who have placed orders but exclude those from countries 'USA', 'Canada', and 'Mexico'. Which query correctly uses NOT IN to achieve this?
hard
A. SELECT customer_id FROM orders WHERE country != ('USA', 'Canada', 'Mexico');
B. SELECT customer_id FROM orders WHERE country IN ('USA', 'Canada', 'Mexico');
C. SELECT customer_id FROM orders WHERE country NOT IN 'USA', 'Canada', 'Mexico';
D. SELECT customer_id FROM orders WHERE country NOT IN ('USA', 'Canada', 'Mexico');
Solution
Step 1: Understand the goal
We want customers excluding those from USA, Canada, and Mexico.
Step 2: Choose correct NOT IN syntax
SELECT customer_id FROM orders WHERE country NOT IN ('USA', 'Canada', 'Mexico'); uses NOT IN with parentheses and a list, correctly excluding those countries.
Step 3: Identify errors in other options
SELECT customer_id FROM orders WHERE country IN ('USA', 'Canada', 'Mexico'); includes those countries (wrong). SELECT customer_id FROM orders WHERE country NOT IN 'USA', 'Canada', 'Mexico'; misses parentheses (syntax error). SELECT customer_id FROM orders WHERE country != ('USA', 'Canada', 'Mexico'); uses != with a list, which is invalid syntax.
Final Answer:
SELECT customer_id FROM orders WHERE country NOT IN ('USA', 'Canada', 'Mexico'); -> Option D
Quick Check:
NOT IN with parentheses excludes listed values [OK]
Hint: Use NOT IN with parentheses to exclude multiple values [OK]
Common Mistakes:
Using != with multiple values
Omitting parentheses after NOT IN
Confusing IN and NOT IN
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.