Bird
Raised Fist0
MySQLquery~20 mins

AND, OR, NOT logical operators in MySQL - Practice Problems & Coding Challenges

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

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

SELECT * FROM Employees WHERE NOT (department = 'IT' OR salary < 50000);
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(20), salary INT);
INSERT INTO Employees VALUES
(1, 'Alice', 'Sales', 60000),
(2, 'Bob', 'Sales', 45000),
(3, 'Charlie', 'HR', 40000),
(4, 'Diana', 'HR', 55000),
(5, 'Eve', 'IT', 70000);
ARows with id 2 and 3 only
BRows with id 1, 4, and 5
CRows with id 1 and 4 only
DRows with id 1, 2, 3, 4, and 5
Attempts:
2 left
💡 Hint
NOT reverses the condition inside the parentheses.
🧠 Conceptual
advanced
1:30remaining
Understanding operator precedence in complex conditions
Which of the following statements about SQL logical operator precedence is correct?
AAND has higher precedence than OR, so AND conditions are evaluated before OR.
BAND and OR have the same precedence and are evaluated left to right.
COR has higher precedence than AND, so it is evaluated first.
DNOT has the lowest precedence among AND and OR.
Attempts:
2 left
💡 Hint
Think about how SQL evaluates multiple logical operators without parentheses.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in logical operators usage
Which option contains a syntax error in the WHERE clause using logical operators?
ASELECT * FROM Employees WHERE NOT department = 'Sales' AND salary > 50000;
BSELECT * FROM Employees WHERE department = 'Sales' AND OR salary > 50000;
CSELECT * FROM Employees WHERE NOT (department = 'Sales' OR salary < 50000);
DSELECT * FROM Employees WHERE department = 'Sales' OR salary > 50000;
Attempts:
2 left
💡 Hint
Look for misplaced or extra logical operators.
❓ optimization
expert
2:30remaining
Optimizing a query with multiple logical operators
You want to select employees who are either in the 'Marketing' department or have a salary greater than 70000, but not those in 'Marketing' with salary less than or equal to 70000. Which query is the most efficient and correct?
ASELECT * FROM Employees WHERE NOT (department = 'Marketing' AND salary <= 70000) AND (department = 'Marketing' OR salary > 70000);
BSELECT * FROM Employees WHERE department = 'Marketing' OR salary > 70000 AND NOT (department = 'Marketing' AND salary <= 70000);
CSELECT * FROM Employees WHERE department = 'Marketing' OR salary > 70000;
DSELECT * FROM Employees WHERE (department = 'Marketing' AND salary > 70000) OR (salary > 70000);
Attempts:
2 left
💡 Hint
Use De Morgan's laws and operator precedence to simplify conditions.

Practice

(1/5)
1. Which logical operator in MySQL requires all conditions to be true for the whole expression to be true?
easy
A. XOR
B. OR
C. NOT
D. AND

Solution

  1. Step 1: Understand AND operator behavior

    The AND operator returns true only if every condition joined by it is true.
  2. Step 2: Compare with other operators

    OR requires at least one true condition, NOT reverses truth, XOR is exclusive or.
  3. Final Answer:

    AND -> Option D
  4. Quick Check:

    AND means all true = A [OK]
Hint: AND means all conditions must be true [OK]
Common Mistakes:
  • Confusing AND with OR
  • Thinking NOT requires all true
  • Mixing XOR with AND
2. Which of the following is the correct syntax to select rows where age is greater than 18 and status is 'active'?
easy
A. SELECT * FROM users WHERE age > 18 AND status = 'active';
B. SELECT * FROM users WHERE NOT age > 18 AND status = 'active';
C. SELECT * FROM users WHERE age > 18 OR status = 'active';
D. SELECT * FROM users WHERE age > 18 NOT status = 'active';

Solution

  1. Step 1: Identify correct logical operator for both conditions

    We want rows where both age > 18 and status = 'active' are true, so use AND.
  2. Step 2: Check syntax correctness

    SELECT * FROM users WHERE age > 18 AND status = 'active'; uses AND correctly and has valid SQL syntax.
  3. Final Answer:

    SELECT * FROM users WHERE age > 18 AND status = 'active'; -> Option A
  4. Quick Check:

    AND joins conditions correctly = C [OK]
Hint: Use AND to combine multiple true conditions [OK]
Common Mistakes:
  • Using OR instead of AND
  • Incorrect NOT placement
  • Syntax errors like missing AND
3. Given the table employees with columns department and salary, what rows will this query return?
SELECT * FROM employees WHERE department = 'Sales' OR salary > 50000 AND NOT department = 'HR';
medium
A. Rows where department is 'Sales' or salary > 50000 or department is not 'HR'
B. Rows where department is 'Sales' and salary > 50000 and department is not 'HR'
C. Rows where department is 'Sales' or salary > 50000 and department is not 'HR'
D. Rows where salary > 50000 only

Solution

  1. Step 1: Understand operator precedence

    AND has higher precedence than OR, so condition is: department = 'Sales' OR (salary > 50000 AND NOT department = 'HR').
  2. Step 2: Interpret the conditions

    Rows match if department is 'Sales', or if salary > 50000 and department is not 'HR'.
  3. Final Answer:

    Rows where department is 'Sales' or salary > 50000 and department is not 'HR' -> Option C
  4. Quick Check:

    AND before OR, NOT reverses condition = B [OK]
Hint: AND binds tighter than OR; use parentheses to clarify [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Misreading NOT placement
  • Assuming OR applies to all conditions
4. Identify the error in this query:
SELECT * FROM orders WHERE NOT order_date > '2023-01-01' AND status = 'shipped' OR;
medium
A. Incorrect use of NOT operator
B. Missing condition after OR
C. AND should be OR
D. order_date comparison syntax error

Solution

  1. Step 1: Check query syntax

    The query ends with OR but no condition follows it, causing syntax error.
  2. Step 2: Verify other parts

    NOT and AND usage is correct; order_date comparison is valid.
  3. Final Answer:

    Missing condition after OR -> Option B
  4. Quick Check:

    OR must have two conditions = A [OK]
Hint: OR must always connect two conditions [OK]
Common Mistakes:
  • Leaving OR without a following condition
  • Misplacing NOT operator
  • Confusing AND and OR usage
5. You want to select all customers who are NOT in the 'VIP' group AND have made a purchase OR have a balance greater than 1000. Which query correctly expresses this logic?
hard
A. SELECT * FROM customers WHERE NOT group = 'VIP' AND (purchases > 0 OR balance > 1000);
B. SELECT * FROM customers WHERE NOT (group = 'VIP' AND purchases > 0) OR balance > 1000;
C. SELECT * FROM customers WHERE NOT group = 'VIP' AND purchases > 0 OR balance > 1000;
D. SELECT * FROM customers WHERE NOT group = 'VIP' OR purchases > 0 AND balance > 1000;

Solution

  1. Step 1: Translate the logic into conditions

    We want customers NOT in 'VIP' group AND (have purchases OR balance > 1000).
  2. Step 2: Check operator precedence and parentheses

    AND has higher precedence than OR, so parentheses around (purchases > 0 OR balance > 1000) ensure correct grouping.
  3. Step 3: Evaluate options

    SELECT * FROM customers WHERE NOT group = 'VIP' AND (purchases > 0 OR balance > 1000); correctly uses NOT on group, AND with grouped OR conditions. Others misplace parentheses or NOT.
  4. Final Answer:

    SELECT * FROM customers WHERE NOT group = 'VIP' AND (purchases > 0 OR balance > 1000); -> Option A
  5. Quick Check:

    NOT group, AND grouped OR = D [OK]
Hint: Use parentheses to group OR conditions with AND [OK]
Common Mistakes:
  • Missing parentheses causing wrong logic
  • Applying NOT to wrong part
  • Confusing AND and OR precedence
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