WHERE Clause

SQL FundamentalsDMLFree Lesson

Advertisement

The WHERE Clause

The WHERE clause filters records — it returns only the rows that meet a specified condition.

💡 WHERE is like a sieve for your data. It keeps the rows you want and discards the rest.

Basic Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

WHERE with Numbers

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

Comparison Operators

OperatorMeaningExample
=Equal toWHERE age = 25
<> or !=Not equalWHERE age <> 25
>Greater thanWHERE price > 100
<Less thanWHERE price < 50
>=Greater or equalWHERE score >= 90
<=Less or equalWHERE score <= 60

WHERE with Text

-- Exact match
SELECT * FROM customers WHERE city = 'New York';

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

WHERE with Dates

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

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;

⚠️ NULL is not the same as 0 or empty string. Use IS NULL and IS NOT NULL to check for NULL values — never use = NULL.

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

See Solution


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

✅ Key Takeaways

  1. WHERE filters rows based on conditions
  2. Use comparison operators for numbers, text, and dates
  3. Use IS NULL / IS NOT NULL for NULL checks
  4. Combine multiple conditions with AND and OR
  5. WHERE is evaluated after FROM but before SELECT

Advertisement

Need Expert SQL Help?

Get personalized SQL training or database consulting.

Advertisement