SQL WHERE Quiz: Aliases, AND/OR, IN, BETWEEN, LIKE Practice
Test yourself: 12 questions and three queries on SQL aliases, calculated columns, WHERE, AND/OR precedence, IN, BETWEEN, LIKE wildcards and DISTINCT.
- Course: SQL study plan
- Module: Selecting and filtering
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: MySQL 8 syntax on SQLite 3.49
Checkpoint — Selecting and filtering is the checkpoint that closes the Selecting and filtering module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: column and table aliases and where they may be used; calculated columns with arithmetic, decimal division and DIV, MOD, ROUND and CONCAT; the WHERE clause with comparisons on numbers, text and dates; AND, OR and NOT with precedence and three-valued logic; IN, BETWEEN and LIKE; and DISTINCT with COUNT(DISTINCT …).
How it works. Twelve questions and three queries. You need 70% on the questions and every query accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- Where may a column alias be used, and why not in
WHERE? - What do
7 / 2,7 DIV 2and7 % 2return in MySQL, and what doesNULL + 1return? - Why does
WHERE dept <> 'Sales'leave out the rows whosedeptis NULL, and how do you include them? - Which binds first,
ANDorOR, and where do the parentheses go? - Is
BETWEENinclusive, and why is it risky on aDATETIMEcolumn? - What do
%and_match inLIKE, and how do you search for a literal%? - What happens to
NOT INwhen its list contains NULL? - How do
COUNT(*),COUNT(col)andCOUNT(DISTINCT col)differ?
The three queries are a price list with renamed and calculated columns, a filtered order list that needs parentheses, a range and a NULL check, and a set of distinct values found with a LIKE pattern.
Common questions
Why does WHERE category = 'books' OR category = 'games' AND price < 500 return expensive books?
Because AND binds tighter than OR, so it reads as category = 'books' OR (category = 'games' AND price < 500): every book qualifies whatever its price. Parenthesise the OR group: (category = 'books' OR category = 'games') AND price < 500.
What does SELECT 7 / 2, 7 DIV 2 return in MySQL?
3.5000 and 3. MySQL's / is always decimal division and DIV is integer division. PostgreSQL, SQLite and SQL Server would return 3 for 7 / 2, because there / between two integers truncates.
Why can a NOT IN list containing NULL return nothing?
x NOT IN (1, NULL) expands to x <> 1 AND x <> NULL. The comparison with NULL is unknown for every row, so the AND is never true and WHERE keeps no rows. The same happens with a NOT IN subquery that returns a NULL.
Exercises
A discounted price list
The table products (discount_pct is NULL when no discount is set):
| product_id | name | price | discount_pct |
|---|---|---|---|
| 1 | Kettle | 1999 | 10 |
| 2 | Toaster | 2500 | 0 |
| 3 | Mixer | 4499 | 15 |
Return id (product_id), product (name), list_price (price) and sale_price: the price less its discount percentage, rounded to 2 places. A NULL discount gives a NULL sale_price. Order by sale_price descending (NULLs last, which is MySQL's descending order), ties by product_id.
Output: id | product | list_price | sale_price.
id | product | list_price | sale_price
3 | Mixer | 4499 | 3824.15
2 | Toaster | 2500 | 2500
1 | Kettle | 1999 | 1799.1Orders to review
The table orders:
| order_id | status | amount | country | coupon |
|---|---|---|---|---|
| 1 | pending | 12000 | IN | NULL |
| 2 | pending | 3000 | US | NULL |
| 3 | shipped | 25000 | IN | NULL |
| 4 | on_hold | 15000 | IN | SAVE10 |
| 5 | on_hold | 9999 | IN | NULL |
An order needs review when all three hold: its status is pending or on_hold; its amount is between 10000 and 50000 inclusive or it ships outside India (country is not IN); and no coupon was used (coupon is NULL). Return order_id, status and amount of those orders, ordered by order_id.
Output: order_id | status | amount.
order_id | status | amount
1 | pending | 12000
2 | pending | 3000Departments with engineers
The table employees (dept may be NULL):
| emp_id | name | job_title | dept |
|---|---|---|---|
| 1 | Asha | Software Engineer | Platform |
| 2 | Ravi | Senior Engineer | Platform |
| 3 | Meera | Data Engineer | Analytics |
| 4 | Kabir | Analyst | Analytics |
| 5 | Dev | QA Engineer | NULL |
Return each department that has at least one employee whose job_title contains the text Engineer, once each, in alphabetical order. Leave out a NULL department.
Output: one column dept.
dept
Analytics
PlatformIn this module: Selecting and filtering
- Columns and aliases — naming what a query returns
- Expressions in SELECT — arithmetic, text and computed columns
- The WHERE clause — keeping the rows you want
- AND, OR and NOT — combining conditions
- IN, BETWEEN and LIKE — lists, ranges and patterns
- DISTINCT — removing duplicate rows
- Checkpoint — Selecting and filtering (this lesson)
← DISTINCT — removing duplicate rows · ORDER BY — putting rows in order →