0
0
SQLquery~20 mins

WHERE with OR operator in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of WHERE with OR
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of WHERE with OR filtering
Given the table Employees with columns id, name, and department, what rows will this query return?

SELECT * FROM Employees WHERE department = 'Sales' OR department = 'HR';
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(50));
INSERT INTO Employees VALUES (1, 'Alice', 'Sales'), (2, 'Bob', 'HR'), (3, 'Charlie', 'IT'), (4, 'Diana', 'Sales');
A[{id:1, name:'Alice', department:'Sales'}, {id:2, name:'Bob', department:'HR'}, {id:4, name:'Diana', department:'Sales'}]
B[{id:1, name:'Alice', department:'Sales'}, {id:3, name:'Charlie', department:'IT'}, {id:4, name:'Diana', department:'Sales'}]
C[{id:2, name:'Bob', department:'HR'}, {id:3, name:'Charlie', department:'IT'}]
D[{id:3, name:'Charlie', department:'IT'}]
Attempts:
2 left
💡 Hint
Remember OR means either condition can be true to include the row.
query_result
intermediate
2:00remaining
Rows returned with mixed OR conditions
Consider the table Products with columns product_id, category, and price. What rows will this query return?

SELECT * FROM Products WHERE category = 'Books' OR price < 20;
SQL
CREATE TABLE Products (product_id INT, category VARCHAR(50), price DECIMAL);
INSERT INTO Products VALUES (1, 'Books', 25), (2, 'Electronics', 15), (3, 'Books', 10), (4, 'Clothing', 30);
A[{product_id:3, category:'Books', price:10}]
B[{product_id:2, category:'Electronics', price:15}, {product_id:4, category:'Clothing', price:30}]
C[{product_id:1, category:'Books', price:25}, {product_id:2, category:'Electronics', price:15}, {product_id:3, category:'Books', price:10}]
D[{product_id:1, category:'Books', price:25}, {product_id:3, category:'Books', price:10}, {product_id:4, category:'Clothing', price:30}]
Attempts:
2 left
💡 Hint
Rows with category 'Books' or price less than 20 are included.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in OR condition
Which option contains a syntax error in the WHERE clause using OR operator?
ASELECT * FROM Orders WHERE status = 'Pending' OR status = 'Shipped' AND customer_id = 5;
BSELECT * FROM Orders WHERE status = 'Pending' OR;
CSELECT * FROM Orders WHERE status = 'Pending' OR status = 'Shipped';
DSELECT * FROM Orders WHERE status = 'Pending' OR status = 'Shipped'
Attempts:
2 left
💡 Hint
Look for incomplete conditions after OR.
optimization
advanced
2:00remaining
Optimizing OR conditions in WHERE clause
Which query is more efficient when filtering rows where category is 'A' or 'B' or 'C'?
ASELECT * FROM Items WHERE category = 'A' AND category = 'B' AND category = 'C';
BSELECT * FROM Items WHERE category = 'A' OR category = 'B' OR category = 'C';
CSELECT * FROM Items WHERE category = 'A' OR category = 'B' AND category = 'C';
DSELECT * FROM Items WHERE category IN ('A', 'B', 'C');
Attempts:
2 left
💡 Hint
IN clause is often optimized for multiple OR conditions.
🧠 Conceptual
expert
3:00remaining
Understanding operator precedence with OR and AND
What is the result of this query on table Tasks with columns id, status, and priority?

SELECT * FROM Tasks WHERE status = 'Open' OR status = 'In Progress' AND priority = 'High';

Assuming rows:
1: (1, 'Open', 'Low')
2: (2, 'In Progress', 'High')
3: (3, 'In Progress', 'Low')
4: (4, 'Closed', 'High')
A[{id:1, status:'Open', priority:'Low'}, {id:2, status:'In Progress', priority:'High'}]
B[{id:1, status:'Open', priority:'Low'}, {id:2, status:'In Progress', priority:'High'}, {id:3, status:'In Progress', priority:'Low'}]
C[{id:1, status:'Open', priority:'Low'}]
D[{id:2, status:'In Progress', priority:'High'}, {id:4, status:'Closed', priority:'High'}]
Attempts:
2 left
💡 Hint
AND has higher precedence than OR in SQL.