Challenge - 5 Problems
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember that AND has higher precedence than OR in SQL.
✗ Incorrect
The condition is evaluated as (department = 'Sales' AND salary > 50000) OR (department = 'HR'). So, employees in Sales with salary > 50000 (id 1) and all employees in HR (id 3 and 4) are returned.
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
NOT reverses the condition inside the parentheses.
✗ Incorrect
The condition inside NOT is (department = 'IT' OR salary < 50000). Rows where this is true are excluded. So, only employees not in IT and with salary >= 50000 remain: Alice (id 1) and Diana (id 4).
🧠 Conceptual
advanced1:30remaining
Understanding operator precedence in complex conditions
Which of the following statements about SQL logical operator precedence is correct?
Attempts:
2 left
💡 Hint
Think about how SQL evaluates multiple logical operators without parentheses.
✗ Incorrect
In SQL, NOT has the highest precedence, then AND, then OR. So AND conditions are evaluated before OR conditions.
📝 Syntax
advanced1:30remaining
Identify the syntax error in logical operators usage
Which option contains a syntax error in the WHERE clause using logical operators?
Attempts:
2 left
💡 Hint
Look for misplaced or extra logical operators.
✗ Incorrect
Option B has 'AND OR' together without a condition between them, which is invalid syntax.
❓ optimization
expert2: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?
Attempts:
2 left
💡 Hint
Use De Morgan's laws and operator precedence to simplify conditions.
✗ Incorrect
Option A correctly excludes employees in Marketing with salary <= 70000 while including others in Marketing with salary > 70000 or those with salary > 70000 in other departments.