Challenge - 5 Problems
AND Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Filter employees by department and salary
Given a table Employees with columns
id, name, department, and salary, what is the output of this query?SELECT name FROM Employees WHERE department = 'Sales' AND salary > 50000;SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 'Sales', 60000), (2, 'Bob', 'Sales', 45000), (3, 'Charlie', 'HR', 55000), (4, 'Diana', 'Sales', 70000);
Attempts:
2 left
💡 Hint
Remember both conditions in the WHERE clause must be true for a row to be included.
✗ Incorrect
The query selects employees in the Sales department with salary greater than 50000. Alice (60000) and Diana (70000) meet both conditions.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in the WHERE clause
Which option contains a syntax error in the WHERE clause using AND operator?
SQL
SELECT * FROM Products WHERE price > 100 AND category = 'Electronics';
Attempts:
2 left
💡 Hint
Look for repeated keywords or misplaced operators.
✗ Incorrect
Option A has two AND operators in a row, which is invalid syntax.
❓ optimization
advanced2:00remaining
Optimize query with multiple AND conditions
You want to find customers from 'USA' who have made purchases over $1000 and have a loyalty status of 'Gold'. Which query is more efficient?
SQL
SELECT * FROM Customers WHERE country = 'USA' AND purchase_amount > 1000 AND loyalty_status = 'Gold';
Attempts:
2 left
💡 Hint
The order of AND conditions does not affect query results or efficiency in standard SQL.
✗ Incorrect
AND conditions are commutative; the database engine evaluates all conditions regardless of order.
🧠 Conceptual
advanced2:00remaining
Understanding AND operator with NULL values
Consider a table
Will rows with
Orders with a column shipped_date that can be NULL. What is the result of this query?SELECT * FROM Orders WHERE shipped_date > '2023-01-01' AND shipped_date < '2023-12-31';Will rows with
shipped_date as NULL be included?Attempts:
2 left
💡 Hint
Think about how SQL treats NULL in logical conditions.
✗ Incorrect
In SQL, any comparison with NULL results in unknown, so the AND condition fails and those rows are excluded.
🔧 Debug
expert2:30remaining
Debug the unexpected output with AND operator
A developer wrote this query:
They expect to get employees in IT with salary over 70000 and also all managers regardless of department or salary. But the output includes employees not matching these rules. What is the cause?
SELECT * FROM Employees WHERE department = 'IT' AND salary > 70000 OR position = 'Manager';They expect to get employees in IT with salary over 70000 and also all managers regardless of department or salary. But the output includes employees not matching these rules. What is the cause?
Attempts:
2 left
💡 Hint
Consider operator precedence and how SQL evaluates AND and OR.
✗ Incorrect
AND has higher precedence than OR, so the query is evaluated as (department = 'IT' AND salary > 70000) OR position = 'Manager'. To get the expected result, parentheses are needed to group conditions correctly.