📖 Scenario: You are managing a small bookstore database. You want to find books that meet certain conditions, like books by a specific author or books priced below a certain amount.
🎯 Goal: Build a MySQL query step-by-step that filters books using the WHERE clause to find specific records based on conditions.
📋 What You'll Learn
Create a table called books with columns id, title, author, and price
Insert specific book records into the books table
Write a SELECT query with a WHERE clause filtering by author
Modify the WHERE clause to filter books priced less than a certain value
💡 Why This Matters
🌍 Real World
Filtering data is essential in databases to find only the records you need, like searching for books by a certain author or price range.
💼 Career
Database filtering skills are fundamental for roles like data analyst, backend developer, and database administrator to efficiently query and manage data.
Progress0 / 4 steps
1
Create the books table
Write a MySQL statement to create a table called books with these columns: id as an integer primary key, title as a text, author as a text, and price as a decimal(5,2).
MySQL
Hint
Use CREATE TABLE with the specified columns and data types.
2
Insert book records
Insert these three books into the books table: (1, 'The Great Gatsby', 'F. Scott Fitzgerald', 10.99), (2, '1984', 'George Orwell', 8.99), and (3, 'To Kill a Mockingbird', 'Harper Lee', 12.50).
MySQL
Hint
Use a single INSERT INTO statement with multiple rows.
3
Select books by author using WHERE
Write a SELECT query to get all columns from books where the author is exactly 'George Orwell'. Use the WHERE clause with author = 'George Orwell'.
MySQL
Hint
Use SELECT * FROM books WHERE author = 'George Orwell' to filter by author.
4
Select books priced below 11.00
Modify the previous SELECT query to get all columns from books where the price is less than 11.00. Use the WHERE clause with price < 11.00.
MySQL
Hint
Use WHERE price < 11.00 to filter books cheaper than 11 dollars.
Practice
(1/5)
1. What does the WHERE clause do in a SQL query?
easy
A. Filters rows based on a specified condition
B. Sorts the rows in ascending order
C. Joins two tables together
D. Creates a new table in the database
Solution
Step 1: Understand the purpose of WHERE clause
The WHERE clause is used to filter rows that meet a specific condition in a SELECT query.
Step 2: Differentiate from other SQL clauses
Sorting is done by ORDER BY, joining by JOIN, and creating tables by CREATE TABLE, so these are not correct.
Final Answer:
Filters rows based on a specified condition -> Option A
Quick Check:
WHERE clause = filters rows [OK]
Hint: WHERE filters rows by condition, not sorting or joining [OK]
Common Mistakes:
Confusing WHERE with ORDER BY
Thinking WHERE creates or joins tables
Assuming WHERE changes table structure
2. Which of the following is the correct syntax to select all columns from a table named employees where the age is greater than 30?
easy
A. SELECT * FROM employees WHERE age => 30;
B. SELECT * FROM employees WHERE age > 30;
C. SELECT * employees WHERE age > 30;
D. SELECT * FROM employees WHERE age < 30;
Solution
Step 1: Check the basic SELECT syntax
The correct syntax to select all columns is SELECT * FROM table_name.
Step 2: Verify the WHERE condition syntax
The comparison operator for greater than is >, not =>. Also, the condition should match the question (age > 30).
Final Answer:
SELECT * FROM employees WHERE age > 30; -> Option B
Quick Check:
Correct syntax with > operator [OK]
Hint: Use > for greater than, syntax: SELECT * FROM table WHERE condition [OK]
Common Mistakes:
Using => instead of >
Omitting FROM keyword
Using wrong comparison operator
3. Given the table products with columns id, name, and price, what will the query SELECT name FROM products WHERE price <= 50; return?
medium
A. All product names with price less than or equal to 50
B. All product names with price greater than 50
C. All product names regardless of price
D. Syntax error due to <= operator
Solution
Step 1: Understand the WHERE condition
The condition price <= 50 means selecting rows where price is less than or equal to 50.
Step 2: Identify the selected columns
The query selects only the name column from those filtered rows.
Final Answer:
All product names with price less than or equal to 50 -> Option A
Quick Check:
price <= 50 filters products [OK]
Hint: <= means less than or equal, filters rows accordingly [OK]
Common Mistakes:
Confusing <= with >= operator
Assuming it returns all products
Thinking <= causes syntax error
4. Identify the error in the following query: SELECT * FROM orders WHERE status = 'shipped' OR;
medium
A. Incorrect use of single quotes around 'shipped'
B. Using * instead of column names
C. Missing semicolon at the end
D. Missing condition after OR operator
Solution
Step 1: Analyze the WHERE clause
The WHERE clause ends with OR but has no condition after it, which is invalid syntax.
Step 2: Check other parts of the query
Single quotes around 'shipped' are correct, semicolon is optional, and * is valid to select all columns.
Final Answer:
Missing condition after OR operator -> Option D
Quick Check:
OR must be followed by a condition [OK]
Hint: OR must always have conditions on both sides [OK]
Common Mistakes:
Leaving OR without a following condition
Thinking quotes around strings cause error
Assuming semicolon is mandatory
5. You have a table students with columns id, name, and score. You want to select students who scored between 70 and 90 inclusive. Which query correctly uses the WHERE clause to achieve this?
hard
A. SELECT * FROM students WHERE score > 70 AND score < 90;
B. SELECT * FROM students WHERE score > 70 OR score < 90;
C. SELECT * FROM students WHERE score BETWEEN 70 AND 90;
D. SELECT * FROM students WHERE score = 70 AND score = 90;
Solution
Step 1: Understand the condition for scores between 70 and 90 inclusive
We want scores greater than or equal to 70 and less than or equal to 90.
Step 2: Evaluate each option
SELECT * FROM students WHERE score > 70 AND score < 90; uses > and < which excludes the boundary values 70 and 90, incorrect. SELECT * FROM students WHERE score > 70 OR score < 90; uses OR which is incorrect because it includes scores outside the range. SELECT * FROM students WHERE score BETWEEN 70 AND 90; uses BETWEEN which is a concise way to express the inclusive range. SELECT * FROM students WHERE score = 70 AND score = 90; checks if score equals both 70 and 90 simultaneously, which is impossible.
Step 3: Choose the best correct option
BETWEEN is the simplest and most readable way to filter inclusive ranges.
Final Answer:
SELECT * FROM students WHERE score BETWEEN 70 AND 90; -> Option C
Quick Check:
BETWEEN includes boundaries, perfect for range filtering [OK]
Hint: Use BETWEEN for inclusive ranges, AND for combined conditions [OK]
Common Mistakes:
Using OR instead of AND for range conditions
Confusing BETWEEN boundaries as exclusive
Using impossible conditions with AND equals
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.