🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

WHERE Clause

SQL FundamentalsDML🟢 Free Lesson

Advertisement

SQL Fundamentals

The WHERE Clause

Filter your data to find exactly what you need — no more, no less.

  • Comparison Operators — equals, greater than, less than, and more
  • Logical Operators — combine conditions with AND, OR, NOT
  • Pattern Matching — search text with LIKE and wildcards

WHERE is like a sieve for your data.

What is WHERE?

Comparison Operators= Equal!= Not Equal> Greater< Less>= <= <>BETWEEN..ANDIN (list)LIKEIS NULL
SELECT column1, column2
FROM table_name
WHERE condition;
ComponentDescriptionExample
WHEREKeywordRequired
conditionBoolean expressionsalary > 70000
Comparison operators=, <>, >, <, >=, <=WHERE price >= 100
Logical operatorsAND, OR, NOTWHERE a > 1 AND b < 10

Comparison Operators

OperatorMeaningExampleDescription
=Equal toWHERE age = 25Exact match
<> or !=Not equalWHERE age <> 25Everything except 25
>Greater thanWHERE price > 100Above 100
<Less thanWHERE price < 50Below 50
>=Greater or equalWHERE score >= 9090 or above
<=Less or equalWHERE score <= 6060 or below
-- Equal to
SELECT first_name, last_name, salary
FROM employees
WHERE salary = 75000;

-- Greater than
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 70000;
first_namelast_namesalary
AliceJohnson75000
CarolWilliams85000
EveBrown91000

WHERE with Text

-- Exact match (case-sensitive in some databases)
SELECT * FROM customers WHERE city = 'New York';

-- Case-insensitive search
SELECT * FROM customers WHERE LOWER(city) = 'new york';

-- Pattern matching with LIKE
SELECT * FROM customers WHERE name LIKE 'A%';      -- Starts with A
SELECT * FROM customers WHERE name LIKE '%son';    -- Ends with son
SELECT * FROM customers WHERE name LIKE '%john%';  -- Contains john
WildcardDescriptionExampleMatches
%Any number of characters'%son'Johnson, Smith
_Exactly one character'_lice'Alice, Ellice

WHERE with Dates

-- Specific date
SELECT * FROM orders WHERE order_date = '2024-01-15';

-- Date range
SELECT * FROM orders
WHERE order_date >= '2024-01-01'
AND order_date <= '2024-03-31';

-- Using BETWEEN for dates
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';

-- Recent orders (last 30 days)
SELECT * FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '30' DAY;

WHERE with NULL

-- Check for NULL (cannot use = NULL)
SELECT * FROM customers WHERE email IS NULL;

-- Check for NOT NULL
SELECT * FROM customers WHERE email IS NOT NULL;

-- WRONG! Returns nothing
SELECT * FROM customers WHERE email = NULL;
SELECT * FROM customers WHERE email != NULL;
ExpressionResultExplanation
NULL = NULLNULLNULL is unknown
NULL != NULLNULLNULL is unknown
NULL IS NULLTRUECorrect way to check
NULL IS NOT NULLFALSECorrect way to check

Multiple Conditions

-- AND: Both conditions must be true
SELECT * FROM employees
WHERE salary > 70000
AND department = 'Engineering';

-- OR: At least one condition must be true
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Marketing';

-- Combined AND and OR (use parentheses!)
SELECT * FROM employees
WHERE (department = 'Engineering' OR department = 'Marketing')
AND salary > 60000;
PrecedenceOperatorDescription
1 (Highest)NOTNegation
2ANDLogical AND
3 (Lowest)ORLogical OR

WHERE with IN

-- Match against a list of values
SELECT * FROM employees
WHERE department IN ('Engineering', 'Marketing', 'Sales');

-- Equivalent to multiple OR conditions
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Marketing'
OR department = 'Sales';

-- NOT IN: Exclude values
SELECT * FROM employees
WHERE department NOT IN ('HR', 'Finance');

WHERE with BETWEEN

-- Inclusive range (includes both endpoints)
SELECT * FROM employees
WHERE salary BETWEEN 60000 AND 80000;

-- Equivalent to:
SELECT * FROM employees
WHERE salary >= 60000
AND salary <= 80000;

Performance Tips

TipBad ExampleGood ExampleWhy
Avoid functions on columnsWHERE YEAR(date) = 2024WHERE date >= '2024-01-01'Can use index
Avoid leading wildcardsWHERE name LIKE '%son'WHERE name LIKE 'John%'Can use index
Use IN instead of ORWHERE x=1 OR x=2 OR x=3WHERE x IN (1,2,3)Cleaner, same performance
Use BETWEEN for rangesWHERE a>=1 AND a<=10WHERE a BETWEEN 1 AND 10More readable

Common Mistakes

-- BAD: Comparing NULL with =
SELECT * FROM customers WHERE email = NULL;

-- GOOD: Use IS NULL
SELECT * FROM customers WHERE email IS NULL;

-- BAD: Using OR chains
SELECT * FROM employees
WHERE department = 'Engineering'
OR department = 'Marketing'
OR department = 'Sales'
OR department = 'Finance';

-- GOOD: Use IN
SELECT * FROM employees
WHERE department IN ('Engineering', 'Marketing', 'Sales', 'Finance');

-- BAD: Function on column in WHERE
SELECT * FROM orders WHERE YEAR(order_date) = 2024;

-- GOOD: Range comparison
SELECT * FROM orders
WHERE order_date >= '2024-01-01'
AND order_date < '2025-01-01';

Practice Exercises

Exercise 1: Find all employees who earn more than 60000, work in Engineering, and were hired after January 1, 2023.

SELECT first_name, last_name, salary
FROM employees
WHERE salary > 60000
  AND department = 'Engineering'
  AND hire_date > '2023-01-01';

Exercise 2: Find all customers from 'New York', 'Los Angeles', or 'Chicago' with email addresses.

SELECT * FROM customers
WHERE city IN ('New York', 'Los Angeles', 'Chicago')
AND email IS NOT NULL;

Exercise 3: Find all products priced between 100, excluding the 'Clearance' category.

SELECT * FROM products
WHERE price BETWEEN 10 AND 100
AND category != 'Clearance';

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement