0
0
MySQLquery~20 mins

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

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