sql where
The SQL WHERE clause lets you filter rows so your query only touches the records that match a condition.
What WHERE does
- It limits which rows a statement sees or changes (SELECT, UPDATE, DELETE).
- Without WHERE, the statement applies to all rows in the table.
- The condition returns TRUE/FALSE; only TRUE rows are included or affected.
Basic syntax
sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
This same pattern applies to UPDATE and DELETE.
sql
UPDATE table_name
SET column1 = value1
WHERE condition;
DELETE FROM table_name
WHERE condition;
Common operators in WHERE
You can combine comparison, range, pattern, and set operators.
=equal to>greater than<less than>=greater than or equal<=less than or equal<>or!=not equal (varies by SQL dialect)
BETWEENvalue is within a range (inclusive)
LIKEmatches a text pattern (with%and_)
INvalue is one of several options
AND,OR,NOTto combine conditions
Example:
sql
SELECT first_name, last_name, salary
FROM employees
WHERE salary >= 50000;
This returns only employees making at least 50,000.
Practical examples
1. Filter by single condition
sql
SELECT *
FROM reviews
WHERE stars < 4;
Finds all low-star reviews (less than 4).
2. Multiple conditions with AND / OR
sql
SELECT *
FROM reviews
WHERE stars < 4
AND user_id = 362;
Low-star reviews for a specific user.
sql
SELECT *
FROM customers
WHERE ContactTitle = 'CEO'
OR ContactTitle = 'Owner'
OR ContactTitle = 'President';
Matches several possible titles.
3. Using IN and BETWEEN
sql
SELECT *
FROM orders
WHERE status IN ('NEW', 'PENDING', 'ON_HOLD');
Same idea as multiple ORs but shorter.
sql
SELECT *
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';
Gets January 2026 orders (both endpoints included).
4. LIKE for patterns
sql
SELECT *
FROM products
WHERE name LIKE 'iPhone%';
Matches any product with a name starting with “iPhone”.
Mini “story” example
Imagine you’re a data analyst at an online shop and your manager asks:
“Show me all employees who earn at least 55,000 so we can review their bonuses.”
Your table employees looks like:
text
first_name | last_name | salary
-----------+-----------+-------
John | Doe | 60000
Jane | Smith | 55000
Bob | Johnson | 48000
You write:
sql
SELECT first_name, last_name, salary
FROM employees
WHERE salary >= 55000;
The result will show John and Jane, but not Bob, because Bob’s salary does not satisfy the condition. That single WHERE line transforms a huge table into just the rows your manager needs.
Handy patterns to remember
- “Only these rows”: use
WHEREwith comparisons (=, <, >, <=, >=, <>).
- “Within a range”: use
BETWEEN start AND end.
- “One of this list”: use
IN (val1, val2, ...).
- “Matches this text pattern”: use
LIKE 'text%'orLIKE '%text%'.
- “Combine rules”: glue conditions with
AND,OR, andNOT.
If you share the exact query or table you’re working with, I can help you write a concrete WHERE clause for it.