📖 Scenario: You are managing a small online store's database. You want to find products within a specific price range to create a special discount list.
🎯 Goal: Build a SQL query that selects products priced between two values using the BETWEEN operator.
📋 What You'll Learn
Create a table called products with columns id, name, and price.
Insert exactly three products with given prices.
Define two variables for the minimum and maximum price range.
Write a SQL query using BETWEEN to select products within the price range.
💡 Why This Matters
🌍 Real World
Filtering products by price range is common in online stores to show customers affordable options or special deals.
💼 Career
Knowing how to use BETWEEN for range filtering is essential for database querying in roles like data analyst, backend developer, and database administrator.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id (integer), name (text), and price (decimal). Insert these three products exactly: (1, 'Pen', 1.20), (2, 'Notebook', 2.50), and (3, 'Backpack', 25.00).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the products.
2
Define minimum and maximum price variables
Define two variables called @min_price and @max_price and set them to 1.00 and 3.00 respectively.
MySQL
Hint
Use SET to assign values to variables in MySQL.
3
Write a query using BETWEEN to filter products
Write a SQL SELECT query to get all columns from products where the price is between @min_price and @max_price using the BETWEEN operator.
MySQL
Hint
The BETWEEN operator includes the boundary values.
4
Complete the query with ordering by price
Add an ORDER BY price ASC clause to the existing query to sort the results by price in ascending order.
MySQL
Hint
Use ORDER BY to sort query results.
Practice
(1/5)
1. What does the BETWEEN operator do in a MySQL query?
easy
A. Filters rows where a column's value is NULL.
B. Filters rows where a column's value is within a specified range, inclusive.
C. Filters rows where a column's value is outside a specified range.
D. Filters rows where a column's value is exactly equal to one value.
Solution
Step 1: Understand the purpose of BETWEEN
The BETWEEN operator is used to filter rows where a column's value falls between two values, including both ends.
Step 2: Compare options with definition
Filters rows where a column's value is within a specified range, inclusive. correctly describes this inclusive range filtering. Other options describe different filters.
Final Answer:
Filters rows where a column's value is within a specified range, inclusive. -> Option B
Quick Check:
BETWEEN means inclusive range filter = D [OK]
Hint: BETWEEN includes both start and end values in range [OK]
Common Mistakes:
Thinking BETWEEN excludes the end values
Confusing BETWEEN with equality (=)
Assuming BETWEEN works only with numbers
2. Which of the following is the correct syntax to select rows where the column age is between 18 and 30?
easy
A. SELECT * FROM users WHERE age BETWEEN 18 & 30;
B. SELECT * FROM users WHERE age BETWEEN (18, 30);
C. SELECT * FROM users WHERE age BETWEEN 18 TO 30;
D. SELECT * FROM users WHERE age BETWEEN 18 AND 30;
Solution
Step 1: Recall correct BETWEEN syntax
The correct syntax uses BETWEEN value1 AND value2 without parentheses or other connectors.
Step 2: Check each option
SELECT * FROM users WHERE age BETWEEN 18 AND 30; uses correct syntax. Options B, C, and D use invalid syntax with commas, TO, or &.
Final Answer:
SELECT * FROM users WHERE age BETWEEN 18 AND 30; -> Option D
Quick Check:
BETWEEN ... AND ... syntax = A [OK]
Hint: BETWEEN syntax always uses AND, no commas or TO [OK]
Common Mistakes:
Using commas inside BETWEEN
Replacing AND with TO or &
Adding parentheses around range values
3. Given the table products with a column price, what will this query return?
SELECT * FROM products WHERE price BETWEEN 50 AND 100;
medium
A. All products with price greater than 50 and less than 100, excluding 50 and 100.
B. All products with price exactly 50 or 100 only.
C. All products with price from 50 up to 100, including both 50 and 100.
D. All products with price less than 50 or greater than 100.
Solution
Step 1: Understand BETWEEN inclusiveness
BETWEEN includes both boundary values, so prices 50 and 100 are included.
Step 2: Analyze query result
The query returns products priced from 50 to 100 inclusive, matching All products with price from 50 up to 100, including both 50 and 100..
Final Answer:
All products with price from 50 up to 100, including both 50 and 100. -> Option C
Quick Check:
BETWEEN includes boundaries = C [OK]
Hint: BETWEEN includes start and end values in results [OK]
Common Mistakes:
Thinking BETWEEN excludes boundary values
Confusing BETWEEN with > and < operators
Assuming BETWEEN works only for numbers
4. Identify the error in this query:
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'
medium
A. No error; the query is correct.
B. BETWEEN cannot be used with date values.
C. The date format is incorrect for BETWEEN.
D. Missing semicolon at the end of the query.
Solution
Step 1: Check BETWEEN usage with dates
BETWEEN works perfectly with date values in MySQL when dates are in 'YYYY-MM-DD' format.
Step 2: Verify query syntax
The query syntax is correct; semicolon is optional in many clients and not an error here.
Final Answer:
No error; the query is correct. -> Option A
Quick Check:
BETWEEN works with dates correctly = A [OK]
Hint: BETWEEN works with numbers, dates, and text [OK]
Common Mistakes:
Thinking BETWEEN can't filter dates
Expecting semicolon is mandatory always
Using wrong date formats like MM-DD-YYYY
5. You want to find all employees hired between January 1, 2020, and December 31, 2022, but your query returns no rows:
SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31 23:59:59';
What is the most likely reason and how to fix it?
hard
A. hire_date column stores only dates without time; remove time part from end date.
B. BETWEEN does not work with datetime values; use >= and <= instead.
C. The date format is wrong; use MM-DD-YYYY format.
D. The query is correct; the table is empty.
Solution
Step 1: Understand data type of hire_date
If hire_date stores only DATE (no time), comparing with '2022-12-31 23:59:59' fails because time part is ignored or mismatched.
Step 2: Fix the query
Use '2022-12-31' without time to match date-only values correctly.
Final Answer:
hire_date column stores only dates without time; remove time part from end date. -> Option A
Quick Check:
Match data type format in BETWEEN range = B [OK]
Hint: Match data types exactly when using BETWEEN with dates [OK]
Common Mistakes:
Adding time to date-only columns in BETWEEN
Assuming BETWEEN fails with datetime
Using wrong date formats
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.