All 40 lessons

Lesson 5 of 40 · SQL Basics

SQL WHERE Clause

By Skillancy Editorial Team

Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.

The WHERE clause filters rows, keeping only those that meet a condition. Without it you get every row in the table; with it you get exactly the rows your question is about.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

Comparison operators

OperatorMeaning
=equal to
<> or !=not equal to
> / <greater / less than
>= / <=greater / less than or equal to

Examples

1. Filter on text

All delivered orders:

SELECT order_id, customer_id, order_date
FROM orders
WHERE status = 'Delivered'
LIMIT 5;
Try it in the SQL Compiler

Result:

order_idcustomer_idorder_date
100112025-01-05
100222025-01-18
100332025-02-02
100542025-03-11
100762025-04-15

(5 rows)

Text values go in single quotes, and in PostgreSQL the match is case-sensitive.

2. Filter on numbers

Products priced above ₹5,000:

SELECT product_name, price
FROM products
WHERE price > 5000;
Try it in the SQL Compiler

Result:

product_nameprice
Redmi Note 13 Pro23999.00
Samsung 43" Smart TV31990.00
Women's Silk Saree8499.00

(3 rows)

3. Filter on dates

Orders placed on or after 1 January 2026:

SELECT order_id, order_date, status
FROM orders
WHERE order_date >= '2026-01-01';
Try it in the SQL Compiler

Result:

order_idorder_datestatus
10222026-01-08Shipped
10232026-01-26Pending
10242026-02-10Pending

(3 rows)

Write dates as 'YYYY-MM-DD'. This format works in every major database.

4. Combine conditions with AND

AND keeps rows where both conditions are true. Prime customers in Karnataka:

SELECT customer_name, city
FROM customers
WHERE is_prime = TRUE
  AND state = 'Karnataka';
Try it in the SQL Compiler

Result:

customer_namecity
Aarav SharmaBengaluru

(1 row)

5. Either condition with OR

OR keeps rows where at least one condition is true. Orders that were cancelled or returned:

SELECT order_id, status
FROM orders
WHERE status = 'Cancelled'
   OR status = 'Returned';
Try it in the SQL Compiler

Result:

order_idstatus
1004Cancelled
1006Returned
1012Cancelled
1015Returned

(4 rows)

6. Exclude rows with NOT

Every order that is not delivered:

SELECT order_id, status
FROM orders
WHERE NOT status = 'Delivered';
Try it in the SQL Compiler

Result:

order_idstatus
1004Cancelled
1006Returned
1012Cancelled
1015Returned
1020Shipped
1022Shipped
1023Pending
1024Pending

(8 rows)

WHERE status <> 'Delivered' gives the same result.

AND, OR and brackets

AND is evaluated before OR, just as multiplication comes before addition. This catches a lot of people out. Suppose you want UPI or Card orders that were delivered:

-- Wrong: AND binds first, so this means UPI orders (any status) OR delivered Card orders
SELECT COUNT(*) AS orders
FROM orders
WHERE payment_method = 'UPI' OR payment_method = 'Card'
  AND status = 'Delivered';
Try it in the SQL Compiler

Result:

orders
15

(1 row)

-- Right: brackets make the intent explicit
SELECT COUNT(*) AS orders
FROM orders
WHERE (payment_method = 'UPI' OR payment_method = 'Card')
  AND status = 'Delivered';
Try it in the SQL Compiler

Result:

orders
12

(1 row)

Different answers from almost the same query. Always use brackets when you mix AND and OR.

Common mistakes

  • Double quotes around text. WHERE city = "Delhi" makes PostgreSQL look for a column named Delhi.
  • Comparing with NULL using =. WHERE phone = NULL never matches anything. Use IS NULL (see the NULL lesson).
  • Mixing AND/OR without brackets, as shown above.

Practice question

Find all products in category 1 (Electronics) that cost less than ₹30,000 and have more than 30 units in stock.

Show solution
SELECT product_name, price, stock_qty
FROM products
WHERE category_id = 1
  AND price < 30000
  AND stock_qty > 30;
Try it in the SQL Compiler

Result:

product_namepricestock_qty
Redmi Note 13 Pro23999.0040
boAt Airdopes 1411299.00250

(2 rows)

Key takeaways

  • WHERE filters rows before anything else is calculated.
  • Use comparison operators for numbers, text and dates.
  • AND needs every condition true; OR needs at least one.
  • AND runs before OR, so add brackets when mixing them.

Frequently asked questions

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY. Use WHERE whenever the condition does not depend on an aggregate like SUM or COUNT.

Is SQL WHERE case-sensitive?

In PostgreSQL, text comparisons are case-sensitive: 'delhi' does not equal 'Delhi'. Use ILIKE or LOWER() for case-insensitive matching. MySQL is usually case-insensitive by default.

What is the difference between != and <>?

Nothing. Both mean not equal. <> is the SQL standard; != is supported by almost every database.

Newsletter

Keep learning SQL

One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.

Prefer a one-page summary? Download the free SQL cheat sheet (PDF)