Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In SQL, which operator is evaluated first in a WHERE clause without parentheses: AND or OR?
easy
A. OR is evaluated before AND
B. AND is evaluated before OR
C. Both are evaluated at the same time
D. It depends on the database system

Solution

  1. Step 1: Understand SQL operator precedence

    In SQL, AND has higher precedence than OR. This means AND conditions are evaluated before OR conditions when no parentheses are used.
  2. Step 2: Apply to WHERE clause evaluation

    Without parentheses, SQL evaluates all AND conditions first, then applies OR. This affects which rows match the query.
  3. Final Answer:

    AND is evaluated before OR -> Option B
  4. Quick Check:

    Operator precedence: AND > OR [OK]
Hint: Remember: AND before OR unless parentheses change it [OK]
Common Mistakes:
  • Thinking OR is evaluated before AND
  • Assuming both have equal precedence
  • Believing evaluation order depends on database
2. Which of the following WHERE clauses is syntactically correct in SQL?
easy
A. WHERE age > 30 OR AND salary < 50000 OR city = 'NY'
B. WHERE age > 30 AND OR salary < 50000 city = 'NY'
C. WHERE age > 30 salary < 50000 OR city = 'NY'
D. WHERE age > 30 AND salary < 50000 OR city = 'NY'

Solution

  1. Step 1: Check logical operator usage

    WHERE age > 30 AND salary < 50000 OR city = 'NY' uses AND and OR correctly between conditions. Options A, B, and D have misplaced or missing operators.
  2. Step 2: Validate syntax correctness

    SQL requires logical operators between conditions. WHERE age > 30 AND salary < 50000 OR city = 'NY' follows this rule, so it is syntactically valid.
  3. Final Answer:

    WHERE age > 30 AND salary < 50000 OR city = 'NY' -> Option D
  4. Quick Check:

    Logical operators must separate conditions [OK]
Hint: Operators must be between conditions, no missing or extra words [OK]
Common Mistakes:
  • Placing AND or OR without conditions
  • Missing operators between conditions
  • Using operators in wrong order
3. Given the table Employees with columns age, salary, and city, what rows will this query return?
SELECT * FROM Employees WHERE age > 30 OR salary < 50000 AND city = 'NY';
medium
A. Rows where age > 30 or (salary < 50000 and city = 'NY')
B. Rows where (age > 30 or salary < 50000) and city = 'NY'
C. Rows where age > 30 and salary < 50000 and city = 'NY'
D. Rows where age > 30 and (salary < 50000 or city = 'NY')

Solution

  1. Step 1: Apply operator precedence rules

    Since AND has higher precedence than OR, the condition is evaluated as: age > 30 OR (salary < 50000 AND city = 'NY').
  2. Step 2: Interpret the WHERE clause

    The query returns rows where either age is greater than 30, or both salary is less than 50000 and city is 'NY'.
  3. Final Answer:

    Rows where age > 30 or (salary < 50000 and city = 'NY') -> Option A
  4. Quick Check:

    AND evaluated before OR [OK]
Hint: AND groups tighter than OR without parentheses [OK]
Common Mistakes:
  • Ignoring operator precedence and grouping incorrectly
  • Assuming OR groups before AND
  • Misreading the condition order
4. Identify the error in this SQL query:
SELECT * FROM Employees WHERE age > 25 OR salary < 40000 AND;
medium
A. Missing FROM clause
B. Incorrect use of OR operator
C. Missing condition after AND operator
D. Incorrect column names

Solution

  1. Step 1: Analyze the WHERE clause syntax

    The query ends with AND; which means the AND operator is not followed by a condition. This is a syntax error.
  2. Step 2: Confirm other parts are correct

    The SELECT and FROM clauses are present and column names seem valid. The error is specifically the incomplete WHERE clause.
  3. Final Answer:

    Missing condition after AND operator -> Option C
  4. Quick Check:

    Logical operators must have conditions on both sides [OK]
Hint: Check that AND/OR always have conditions after them [OK]
Common Mistakes:
  • Leaving dangling AND or OR without conditions
  • Forgetting to complete WHERE clause
  • Assuming syntax is correct despite missing parts
5. You want to select employees who are either older than 40 or work in 'LA' with a salary less than 60000. Which WHERE clause correctly uses operator precedence to get this result?
Options:
A) WHERE age > 40 AND salary < 60000 OR city = 'LA'
B) WHERE (age > 40 OR salary < 60000) AND city = 'LA'
C) WHERE age > 40 OR (salary < 60000 AND city = 'LA')
D) WHERE (age > 40 OR city = 'LA') AND salary < 60000
hard
A. WHERE age > 40 OR (salary < 60000 AND city = 'LA')
B. WHERE (age > 40 OR salary < 60000) AND city = 'LA'
C. WHERE age > 40 AND salary < 60000 OR city = 'LA'
D. WHERE (age > 40 OR city = 'LA') AND salary < 60000

Solution

  1. Step 1: Understand the desired logic

    You want employees who are either older than 40, or who both work in 'LA' and have salary less than 60000.
  2. Step 2: Apply operator precedence and parentheses

    Since AND has higher precedence, a clause like WHERE age > 40 AND salary < 60000 OR city = 'LA' groups as (age > 40 AND salary < 60000) OR city = 'LA'. But for our desired logic age > 40 OR (salary < 60000 AND city = 'LA'), we need explicit parentheses as in WHERE age > 40 OR (salary < 60000 AND city = 'LA').
  3. Step 3: Compare options

    WHERE age > 40 AND salary < 60000 OR city = 'LA' groups as (age > 40 AND salary < 60000) OR city = 'LA' -- incorrect. WHERE (age > 40 OR salary < 60000) AND city = 'LA' requires city = 'LA' even for age > 40 -- incorrect. WHERE (age > 40 OR city = 'LA') AND salary < 60000 requires salary < 60000 always -- incorrect. WHERE age > 40 OR (salary < 60000 AND city = 'LA') correctly implements the logic.
  4. Final Answer:

    WHERE age > 40 OR (salary < 60000 AND city = 'LA') -> Option A
  5. Quick Check:

    Use parentheses to group AND conditions with OR correctly [OK]
Hint: Use parentheses to group AND conditions when mixed with OR [OK]
Common Mistakes:
  • Not using parentheses and misreading condition grouping
  • Assuming OR groups before AND
  • Changing logic by wrong parentheses placement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard