Module F-3·22 min read

WHERE, ORDER BY, LIMIT, OFFSET, string functions, and date arithmetic — the tools you will use on every single query.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

F-3 — Filtering, Sorting, and Finding What You Need

Who this module is for: You completed F-2, can create tables and perform basic INSERT/SELECT/UPDATE/DELETE. Now you need to find specific rows, sort results, and use the built-in functions that every SQL developer relies on daily. This module covers the tools you will use in almost every query you ever write.


The WHERE Clause — Your Primary Filter

WHERE filters rows before they are returned. Only rows where the condition is TRUE are included.

sql

BETWEEN — Range Check

sql

BETWEEN is inclusive — it includes the boundary values.

IN — Match Any Value in a List

sql

Trap: if the list (or a subquery feeding it) contains even one NULL, NOT IN silently returns zero rows — not an error, just nothing, because comparing anything to NULL with <> yields NULL, not true. NOT IN (SELECT category FROM other_table) is a classic way to hit this if that column allows NULLs. NOT EXISTS is the safer equivalent when NULLs might be involved.

LIKE and ILIKE — Pattern Matching

LIKE matches a pattern where % means "any sequence of characters" and _ means "any single character".

sql

LIKE is case-sensitive. ILIKE is the PostgreSQL extension for case-insensitive matching:

sql

IS NULL and IS NOT NULL

sql

Combining Conditions — AND, OR, NOT

sql

Parentheses Are Critical

Without parentheses, AND has higher precedence than OR — this catches many beginners:

sql

Rule: when mixing AND and OR, always use parentheses to make intent explicit.


ORDER BY — Sorting Results

Without ORDER BY, PostgreSQL returns rows in no guaranteed order. Do not assume the order will be consistent between queries.

sql

NULLS FIRST and NULLS LAST

When sorting a column that contains NULL, you control where nulls appear:

sql

By default in PostgreSQL: NULL sorts last with ASC, first with DESC.


LIMIT and OFFSET — Pagination

sql

⚠️ The performance trap with large OFFSET:

OFFSET 1000 means PostgreSQL reads 1000 rows and discards them before returning your 5. At large offsets, this gets slow. For pagination in real applications, use cursor-based pagination instead (covered in later modules).

Always use ORDER BY with LIMIT — without it, the rows you get are unpredictable and vary between runs.


DISTINCT — Remove Duplicate Rows

sql

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.