TECHNOLOGIESTieInn Technologies

Filtering rows with WHERE

SQL · 17 questions

Short notes

Key idea

WHERE keeps only the rows that make a condition true. It runs before grouping and before SELECT, row by row.

Syntax

SELECT * FROM orders
WHERE status = 'shipped'
  AND amount >= 500
  AND city IN ('Chennai', 'Madurai')
  AND order_date BETWEEN '2024-01-01' AND '2024-03-31'
  AND customer_name LIKE 'S%';
  • <> or != for "not equal".
  • LIKE: % is any run of characters, _ is exactly one.
  • IS NULL / IS NOT NULL for missing values.

Example

Orders over ₹1,000 that are not cancelled:

SELECT order_id, amount FROM orders
WHERE amount > 1000 AND status <> 'cancelled';

Traps

  • = NULL never matches. Use IS NULL.
  • AND binds tighter than OR. a OR b AND c means a OR (b AND c); add brackets.
  • BETWEEN includes both ends. With timestamps, BETWEEN '2024-01-01' AND '2024-01-31' misses most of 31 January.
  • NOT IN with a NULL in the list returns no rows.

Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    Filtering rows with WHERE

  1. Question 1easy

    Which condition finds customers whose city is missing?

  2. Filtering rows with WHERE

  3. Question 2easy

    Which condition finds customers in Chennai or Coimbatore?

  4. Filtering rows with WHERE

  5. Question 3medium

    How many rows does this query return?

    SELECT product_name, category, price
    FROM products
    WHERE product_name LIKE 'S%'
       OR category = 'Books' AND price > 600;
  6. Filtering rows with WHERE

  7. Question 4hard

    The orders table has 80 orders: 48 paid by UPI, 18 by Card, 9 by COD, 3 by NetBanking, and 2 with no payment method recorded. What does this query return?

    SELECT COUNT(*)
    FROM orders
    WHERE payment_method NOT IN ('UPI', 'Card');
  8. Filtering rows with WHERE

  9. Question 5medium

    The products table has 20 products. 4 are in the Books category, and 2 have no category. What does this query return?

    SELECT COUNT(*)
    FROM products
    WHERE category <> 'Books';

12 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.