0
0
SQLquery~20 mins

WHERE with NOT operator in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NOT Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of NOT operator in WHERE clause
Given a table Employees with columns id, name, and department, what is the output of the following query?

SELECT name FROM Employees WHERE NOT department = 'Sales';
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), department VARCHAR(50));
INSERT INTO Employees VALUES (1, 'Alice', 'Sales'), (2, 'Bob', 'HR'), (3, 'Charlie', 'Sales'), (4, 'Diana', 'IT');
ANo rows returned
BAlice, Charlie
CAlice, Bob, Charlie, Diana
DBob, Diana
Attempts:
2 left
💡 Hint
The NOT operator reverses the condition inside the WHERE clause.
🧠 Conceptual
intermediate
2:00remaining
Understanding NOT with multiple conditions
Consider the query:

SELECT * FROM Products WHERE NOT (category = 'Electronics' OR price > 1000);

Which rows will this query return?
ARows where category is 'Electronics' or price is more than 1000
BRows where category is 'Electronics' and price is more than 1000
CRows where category is not 'Electronics' and price is 1000 or less
DAll rows in the table
Attempts:
2 left
💡 Hint
NOT reverses the entire condition inside the parentheses.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with NOT operator
Which of the following SQL queries will cause a syntax error?
ASELECT * FROM Orders WHERE NOT (status = 'Shipped' AND);
BSELECT * FROM Orders WHERE NOT(status = 'Shipped');
CSELECT * FROM Orders WHERE NOT status = 'Shipped' AND date > '2023-01-01';
DSELECT * FROM Orders WHERE NOT status = 'Shipped';
Attempts:
2 left
💡 Hint
Look for incomplete conditions inside parentheses.
optimization
advanced
2:00remaining
Optimizing NOT conditions in WHERE clause
Which query is more efficient for filtering rows where status is not 'Active'?
ASELECT * FROM Users WHERE NOT status = 'Active';
BSELECT * FROM Users WHERE status <> 'Active';
CSELECT * FROM Users WHERE status != 'Active';
DSELECT * FROM Users WHERE NOT (status = 'Active');
Attempts:
2 left
💡 Hint
Consider how the database engine optimizes comparison operators.
🔧 Debug
expert
3:00remaining
Debugging unexpected results with NOT operator
A developer wrote this query:

SELECT * FROM Customers WHERE NOT city = 'New York' OR country = 'USA';

They expected to get customers who are not from New York and are from the USA. Why does this query return more rows than expected?
ABecause OR has lower precedence than NOT, so the condition is evaluated as (NOT city = 'New York') OR country = 'USA', returning rows where city is not New York or country is USA
BBecause NOT applies only to city, so it excludes only New York customers
CBecause the query syntax is invalid and returns all rows
DBecause the query should use AND instead of OR to combine conditions
Attempts:
2 left
💡 Hint
Think about operator precedence and how NOT and OR combine.