πŸŽ‰ 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?

DfWHERE Clause

A SQL clause that filters rows based on a condition. Only rows that evaluate to TRUE are included in the result set.

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

Use % at the start of a pattern (like '%son') to find suffixes. This prevents index usage and causes full table scans. Prefer patterns that start with a fixed character for better performance.

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;

Always use range comparisons on dates instead of functions like MONTH() or YEAR(). Functions on columns prevent the database from using indexes, causing slow full table scans.

WHERE with NULL

DfNULL

NULL represents an unknown or missing value. It is not equal to 0, an empty string, or any other value. You cannot compare NULL using = or !=.

-- 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

NULL is not the same as 0 or empty string. Always use IS NULL and IS NOT NULL to check for NULL values β€” never use = NULL.

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

When combining AND and OR, always use parentheses to make your intent explicit. Without them, AND is evaluated before OR, which can produce unexpected results.

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

The WHERE clause is evaluated after FROM but before SELECT. This means you cannot reference column aliases defined in the SELECT clause within a WHERE condition.

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 10and10 and100, excluding the 'Clearance' category.

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

Key Takeaways

  1. WHERE filters rows based on conditions β€” only matching rows are returned
  2. Use comparison operators for numbers, text, and dates
  3. Use IS NULL / IS NOT NULL for NULL checks β€” never use = NULL
  4. Combine conditions with AND and OR β€” use parentheses for clarity
  5. WHERE is evaluated after FROM but before SELECT β€” aliases aren't available
⭐

Premium Content

WHERE Clause

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement