Aral Code

Filtering & Sorting Lesson 4 of 21

SQL WHERE — Filtering Rows

Choose which rows to return with WHERE and comparison operators =, <>, >, <, >= and <=, using numbers and text.

SELECT chooses the columns. WHERE chooses the rows. It's like telling the database: "show me only the products that…".

Syntax

SELECT columns
FROM table
WHERE condition;

Filtering by a number

SQL Products that cost more than ₱40
SELECT name, price
FROM products
WHERE price > 40;

Comparison operators

OperatorMeaningExample
=Equalcategory = 'Drinks'
<> or !=Not equalstatus <> 'paid'
>Greater thanstock > 100
<Less thanprice < 10
>=Greater than or equalgrade >= 9
<=Less than or equalscore <= 80

Note that SQL uses a single = to compare, unlike JavaScript and Python which use ==.

Filtering by text

Text values go inside single quotes:

SQL All drinks
SELECT name, price
FROM products
WHERE category = 'Drinks';
SQL Customers from San Isidro
SELECT name, phone
FROM customers
WHERE barangay = 'San Isidro';

Finding problems: out of stock

SQL Products with no stock left
SELECT name, stock
FROM products
WHERE stock = 0;
SQL Products running low (fewer than 30)
SELECT name, stock
FROM products
WHERE stock < 30;

Queries like these are why stores use databases: Aling Nena can see what to restock in one second.

Not equal

SQL Orders that are not yet paid
SELECT id, customer_id, order_date, status
FROM orders
WHERE status <> 'paid';

Filtering with calculations

The condition can include math:

SQL Items with more than ₱2,000 worth of stock
SELECT name, price, stock, price * stock AS stock_value
FROM products
WHERE price * stock > 2000;

Filtering dates

Dates stored as YYYY-MM-DD text can be compared like text, and the order works correctly:

SQL Orders from November 15 onward
SELECT *
FROM orders
WHERE order_date >= '2026-11-15';

Case sensitivity

In SQLite, = on text is case-sensitive: 'drinks' won't match 'Drinks'. In MySQL it usually isn't. To be safe, compare lowercase versions:

SQL Case-insensitive match
SELECT name FROM products WHERE LOWER(category) = 'drinks';

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which query finds products that cost exactly ₱15?
2. What does <> mean?
3. What's wrong with WHERE barangay = Mabini?
4. Which keyword filters rows?

By Sir Reyel · Last updated 2026-12-04