You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
The WHERE clause lets you filter rows based on conditions. Instead of getting every row, you get only the ones that match your criteria.
SELECT columns
FROM table
WHERE condition;
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | price = 29.99 |
<> or != | Not equal | category <> 'Office' |
> | Greater than | price > 100 |
< | Less than | stock < 50 |
>= | Greater than or equal | price >= 100 |
<= | Less than or equal | stock <= 20 |
-- Products priced over $100
SELECT name, price
FROM products
WHERE price > 100;
Result:
name | price
------------------+---------
Laptop Pro 15" | 1299.99
Standing Desk | 449.99
Office Chair | 299.99
-- Only electronics
SELECT name, price, stock
FROM products
WHERE category = 'Electronics';
Important: Text values must be in single quotes (
'Electronics'), not double quotes.
-- Electronics under $100
SELECT name, price
FROM products
WHERE category = 'Electronics'
AND price < 100;
-- Furniture OR items costing more than $1000
SELECT name, category, price
FROM products
WHERE category = 'Furniture'
OR price > 1000;
-- Everything except Office supplies
SELECT name, category
FROM products
WHERE NOT category = 'Office';
-- Equivalent shorthand
SELECT name, category
FROM products
WHERE category <> 'Office';
Instead of chaining OR conditions, use IN:
-- Products in Electronics or Furniture
SELECT name, category
FROM products
WHERE category IN ('Electronics', 'Furniture');
NOT IN excludes those values:
SELECT name, category
FROM products
WHERE category NOT IN ('Office');
-- Products priced between $50 and $500 (inclusive)
SELECT name, price
FROM products
WHERE price BETWEEN 50 AND 500;
Equivalent to price >= 50 AND price <= 500.
Use LIKE for partial text matches:
% matches any sequence of characters_ matches exactly one character-- Users whose name starts with "A"
SELECT name, email
FROM users
WHERE name LIKE 'A%';
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.