WHERE Clause — Complete Guide
WHERE Clause — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 12 of 100
WHERE Clause
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — SQL & Queries
What is this?
WHERE filters rows before they reach SELECT, GROUP BY, or ORDER BY. You combine conditions with AND, OR, NOT, IN, BETWEEN, and IS NULL. Only matching rows continue through the query pipeline.
Why should you care?
Swiggy riders need orders WHERE status='out_for_delivery' AND city='Bangalore' — not every order in India.
See it live — copy this example
Run in pgAdmin or psql.
SELECT order_id, total_amount, status
FROM orders
WHERE status IN ('pending', 'confirmed')
AND total_amount BETWEEN 100 AND 2000
AND created_at >= now() - interval '7 days';
What happened?
- IN limits statuses.
- BETWEEN sets amount bounds inclusive.
- The interval filter keeps last week only.
- Rows failing any condition are dropped.
Practice next
- Add created_at timestamptz DEFAULT now() to orders if missing.
- Insert orders with varied status, amount, and dates.
- Run the WHERE query and count results.
- Filter with ILIKE '%express%' on a text column for case-insensitive search.
- Add OR customer_id = 42 wrapped in parentheses.
Remember
WHERE runs before grouping and sorting. Use IS NULL for missing values. Parenthesize OR groups when mixing with AND.
PostgresVerse ops dashboard
Support filters recent medium-value pending orders during a payment gateway outage.
Outcome: They callback 200 customers instead of exporting the full orders table.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!