MarzleyTech Learn

Home / Learn / SQL / WHERE and ORDER BY: filtering rows with conditions, AND/OR, IN, BETWEEN, LIKE and sorting

WHERE and ORDER BY: filtering rows with conditions, AND/OR, IN, BETWEEN, LIKE and sorting

Real tables hold thousands or millions of rows, and you rarely want all of them. A bank wants transactions from this month, a shop wants products under KSh 1,000, a school wants students in Form 4 East. WHERE filters rows to just the ones you need, and ORDER BY sorts the result. Together they answer most everyday questions asked of a database.

WHERE: keep only matching rows

SQL
SELECT columns
FROM table
WHERE condition;
SQL · runs live in the interactive lesson
SELECT * FROM Customers WHERE City = 'Nairobi';

Text values go in single quotes. Numbers don't.

SQL · runs live in the interactive lesson
SELECT Name, Price FROM Products WHERE Price > 2000;

Comparison operators

OperatorMeaningExample
=equalCity = 'Kisumu'
<> or !=not equalCategory <> 'Storage'
> / <greater / less thanPrice > 1000
>= / <=greater/less or equalQuantity >= 2
SQL · runs live in the interactive lesson
SELECT Name, Category FROM Products WHERE Category <> 'Storage';

AND, OR, NOT

  • AND: both conditions must be true.
  • OR: at least one must be true.
  • NOT: reverses a condition.
SQL · runs live in the interactive lesson
SELECT Name, Category, Price
FROM Products
WHERE Category = 'Electronics' AND Price < 1000;
SQL · runs live in the interactive lesson
SELECT Name, City FROM Customers WHERE City = 'Kisumu' OR City = 'Mombasa';

Brackets matter

AND is evaluated before OR, just like multiplication before addition. Compare:

SQL · runs live in the interactive lesson
-- Accessories of any price, plus Electronics under 1000
SELECT Name, Category, Price FROM Products
WHERE Category = 'Accessories' OR Category = 'Electronics' AND Price < 1000;
SQL · runs live in the interactive lesson
-- Accessories or Electronics, all under 1000
SELECT Name, Category, Price FROM Products
WHERE (Category = 'Accessories' OR Category = 'Electronics') AND Price < 1000;

When you mix AND and OR, always use brackets to make your meaning clear.

IN: match any value in a list

SQL · runs live in the interactive lesson
SELECT Name, City FROM Customers WHERE City IN ('Kisumu', 'Mombasa', 'Eldoret');

NOT IN excludes the list:

SQL · runs live in the interactive lesson
SELECT Name, City FROM Customers WHERE City NOT IN ('Nairobi');

BETWEEN: a range (inclusive)

SQL · runs live in the interactive lesson
SELECT Name, Price FROM Products WHERE Price BETWEEN 800 AND 2500;

Both ends are included: 800 and 2500 match. It also works with dates stored as YYYY-MM-DD text:

SQL · runs live in the interactive lesson
SELECT * FROM Orders WHERE OrderDate BETWEEN '2026-08-01' AND '2026-08-31';

LIKE: text patterns

PatternMatches
'A%'starts with A
'%a'ends with a
'%phone%'contains "phone"
'_a%'second letter is a (_ is exactly one character)
SQL · runs live in the interactive lesson
SELECT Name FROM Customers WHERE Name LIKE 'A%';
SQL · runs live in the interactive lesson
SELECT Name FROM Products WHERE Name LIKE '%USB%';
SQL · runs live in the interactive lesson
-- Safaricom-style numbers starting 0712
SELECT Name, Phone FROM Customers WHERE Phone LIKE '0712%';

In SQLite, LIKE ignores case for English letters, so '%usb%' also matches "USB".

NULL: missing values

NULL means "unknown or missing". You can't compare it with =; use IS NULL or IS NOT NULL:

SQL
SELECT * FROM Customers WHERE Phone IS NULL;

The CASE, NULL and HAVING lesson covers NULL in depth.

ORDER BY: sorting

SQL · runs live in the interactive lesson
SELECT Name, Price FROM Products ORDER BY Price;

Ascending (ASC, smallest first) is the default. Use DESC for largest first:

SQL · runs live in the interactive lesson
SELECT Name, Price FROM Products ORDER BY Price DESC;

Sorting by several columns

Sort by category A–Z, then by price high to low within each category:

SQL · runs live in the interactive lesson
SELECT Category, Name, Price FROM Products ORDER BY Category ASC, Price DESC;

Text sorts alphabetically; dates in YYYY-MM-DD format sort correctly as text, which is why that format is the standard.

You can sort by an alias or calculated column:

SQL · runs live in the interactive lesson
SELECT Name, Price * 1.16 AS WithVAT FROM Products ORDER BY WithVAT DESC;

Top-N queries

The 3 most expensive products:

SQL · runs live in the interactive lesson
SELECT Name, Price FROM Products ORDER BY Price DESC LIMIT 3;

The most recent order:

SQL · runs live in the interactive lesson
SELECT * FROM Orders ORDER BY OrderDate DESC LIMIT 1;

Putting it together

Customers outside Nairobi whose name contains "o", sorted by city then name:

SQL · runs live in the interactive lesson
SELECT Name, City
FROM Customers
WHERE City <> 'Nairobi' AND Name LIKE '%o%'
ORDER BY City, Name;

Order of clauses is fixed: SELECT → FROM → WHERE → ORDER BY → LIMIT.

Think about it: A manager asks for "orders in August with quantity 2 or more, or any order from customer 5". Write the WHERE clause with correct brackets.Show answer

WHERE (OrderDate BETWEEN '2026-08-01' AND '2026-08-31' AND Quantity >= 2) OR CustomerID = 5. The brackets group the August-and-quantity condition, so customer 5's orders are included regardless of month.

Summary

  • WHERE filters rows with =, <>, >, <, >=, <=; text in single quotes.
  • Combine conditions with AND, OR, NOT, and use brackets when mixing them.
  • IN matches a list, BETWEEN a range (inclusive), LIKE text patterns with % and _.
  • Use IS NULL for missing values.
  • ORDER BY sorts (ASC default, DESC for reverse), by one or more columns; with LIMIT it gives top-N results.

Check yourself

  1. Which keyword sorts results from largest to smallest?

    Show answer

    DESC

  2. Which wildcard in LIKE matches any number of characters?

    Show answer

    %

  3. Does BETWEEN 800 AND 2500 include 2500? (yes/no)

    Show answer

    yes

  4. Which is evaluated first without brackets: AND or OR?

    Show answer

    AND

  5. How do you check for missing values? (two words)

    Show answer

    IS NULL

  6. Which keyword matches any value in a list like ('Kisumu', 'Mombasa')?

    Show answer

    IN

Exercise

Select all customers from Nairobi.

Do this exercise in the live editor

Lesson 3 of 16 in SQL · Printable course notes