Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
β€Ήβ€Ί

1. SQL basics: ask a table a question

1.2 WHERE: keep only the rows you need

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      WHERE is a filter. The database checks every row against your condition and keeps only the rows where it is true.

      • Compare with =, <> (not equal), >, <, >=, <=
      • Join conditions with AND (both true) and OR (either true)
      • IN ('Noida', 'Pune') matches any value in the list
      • BETWEEN 1000 AND 5000 matches a range, including both ends

      Text values go inside single quotes: 'delivered'. Numbers have no quotes: 1000.

      pandas vs SQL

      • df[df["city"] == "Noida"] β†’ WHERE city = 'Noida'
      • & β†’ AND
      • | β†’ OR
      • .isin([...]) β†’ IN (...)
      Must Know:

      SQL uses one = to compare, not ==. Text is matched exactly in most databases: 'Noida' and 'noida' are different values.

      How it fits together

      1. All rows
      2. WHERE condition
      3. Only matching rows

      Watch the query run

      Grey rows are the ones the filter removes.

      SQL

      SELECT product, amountFROM ordersWHERE status = 'delivered'  AND amount > 1000;

      Rows at this step

      product  | amount
      ---------+-------
      Laptop   | 55000
      Keyboard | 1500
      Laptop   | 52000
      Monitor  | 11500

      Practice in SQL

      Show the name and city of customers who live in Noida.

      Tables you can query: customers, orders
      customers
      id | name  | city
      ---+-------+------
      1  | Asha  | Noida
      2  | Ravi  | Delhi
      3  | Meera | Pune
      4  | Kabir | Noida
      5  | Zoya  | NULL
      
      orders
      id  | customer_id | product  | amount | status    | order_date
      ----+-------------+----------+--------+-----------+-----------
      101 | 1           | Laptop   | 55000  | delivered | 2026-01-05
      102 | 2           | Mouse    | 700    | delivered | 2026-01-06
      103 | 1           | Keyboard | 1500   | delivered | 2026-01-09
      104 | 3           | Monitor  | 12000  | cancelled | 2026-01-12
      105 | 2           | Laptop   | 52000  | delivered | 2026-01-15
      106 | 4           | Mouse    | 650    | pending   | 2026-01-18
      107 | 1           | Monitor  | 11500  | delivered | 2026-01-20
      108 | 3           | Keyboard | NULL   | pending   | 2026-01-22
      Expected output
      name  | city
      ------+------
      Asha  | Noida
      Kabir | Noida

      Reference solution
      SELECT name, city
      FROM customers
      WHERE city = 'Noida';

      Question 1 Β· Easy

      What does WHERE do?

      Question 2 Β· Medium

      How many orders match WHERE product = 'Mouse' OR product = 'Keyboard'?

      Question 3 Β· Hard

      Which condition finds orders from 2026-01-10 to 2026-01-20, including both dates?

      Key takeaways

      WHERE keeps the rows where the condition is true. Combine checks with AND and OR. Put text in single quotes.

      Lesson 2 of 24

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousWHERE: keep only the rows you need
      Save your progress free for 1 month. Start free Β· Log in