0
0
SQLquery~20 mins

Operator precedence in WHERE in SQL - Practice Problems & Coding Challenges

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

SELECT * FROM Employees WHERE department = 'Sales' OR department = 'HR' AND salary > 50000;
SQL
CREATE TABLE Employees (id INT, department VARCHAR(20), salary INT);
INSERT INTO Employees VALUES
(1, 'Sales', 40000),
(2, 'HR', 60000),
(3, 'HR', 45000),
(4, 'Sales', 70000),
(5, 'IT', 55000);
ARows with id 1, 2, and 4
BRows with id 1, 3, and 4
CRows with id 2 and 4 only
DRows with id 1, 2, 3, and 4
Attempts:
2 left
💡 Hint
Remember that AND has higher precedence than OR in SQL WHERE clauses.
query_result
intermediate
2:00remaining
Effect of parentheses on operator precedence
Using the same Employees table, what rows will this query return?

SELECT * FROM Employees WHERE (department = 'Sales' OR department = 'HR') AND salary > 50000;
SQL
CREATE TABLE Employees (id INT, department VARCHAR(20), salary INT);
INSERT INTO Employees VALUES
(1, 'Sales', 40000),
(2, 'HR', 60000),
(3, 'HR', 45000),
(4, 'Sales', 70000),
(5, 'IT', 55000);
ARows with id 1, 2, and 4
BRows with id 2 and 4 only
CRows with id 1, 3, and 4
DRows with id 2, 3, and 5
Attempts:
2 left
💡 Hint
Parentheses change the order in which conditions are evaluated.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in WHERE clause
Which option contains a syntax error in the WHERE clause due to incorrect operator precedence or missing parentheses?
ASELECT * FROM Employees WHERE department = 'Sales' AND salary > 50000 OR department = 'HR';
BSELECT * FROM Employees WHERE (department = 'Sales' AND salary > 50000) OR department = 'HR';
CSELECT * FROM Employees WHERE department = 'Sales' AND (salary > 50000 OR department = 'HR');
DSELECT * FROM Employees WHERE department = 'Sales' AND salary > 50000 OR;
Attempts:
2 left
💡 Hint
Look for incomplete or misplaced operators.
optimization
advanced
2:00remaining
Optimizing WHERE clause with mixed AND and OR
Which query is logically equivalent but optimized for clarity and performance when filtering employees from 'Sales' or 'HR' departments with salary over 50000?
ASELECT * FROM Employees WHERE salary > 50000 AND (department = 'Sales' OR department = 'HR');
BSELECT * FROM Employees WHERE (department = 'Sales' OR department = 'HR') AND salary > 50000;
CSELECT * FROM Employees WHERE department = 'Sales' OR department = 'HR' AND salary > 50000;
DSELECT * FROM Employees WHERE salary > 50000 OR (department = 'Sales' AND department = 'HR');
Attempts:
2 left
💡 Hint
AND has higher precedence than OR, but grouping conditions can improve readability.
🧠 Conceptual
expert
2:00remaining
Operator precedence impact on query results
Consider the query:

SELECT * FROM Employees WHERE department = 'Sales' OR department = 'HR' AND salary > 50000 OR department = 'IT' AND salary < 60000;

Which of the following best describes how SQL evaluates this WHERE clause?
ASQL evaluates all conditions simultaneously without precedence rules.
BSQL evaluates OR operators first, then AND operators, ignoring parentheses.
CSQL evaluates AND operators first, then OR operators from left to right, grouping conditions accordingly.
DSQL evaluates all operators strictly left to right without precedence.
Attempts:
2 left
💡 Hint
Recall the standard operator precedence rules in SQL WHERE clauses.