⚡0 XP🔥0

Filtering and Sorting (WHERE, ORDER BY, DISTINCT)

Lesson 2 of 8 ⏱ 4 min read ✎ 4 exercises ❓ 5-question quiz ▶ Runs SQL

Filter rows with WHERE using comparison operators, AND/OR/NOT, IN, BETWEEN, LIKE and IS NULL; sort with ORDER BY; remove duplicates with DISTINCT; create calculated columns with aliases.

WHERE - filtering rows#

WHERE keeps only the rows that satisfy a condition:

SELECT columns
FROM table
WHERE condition;
▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

Comparison operators#

Operator Meaning Example
= equal status = 'Delivered'
<> or != not equal status <> 'Cancelled'
> < >= <= greater/less price >= 5000
▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

AND, OR, NOT#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
Common Mistake

When mixing AND and OR, use brackets. Without them AND is evaluated first:

-- probably NOT what you meant:
WHERE status = 'Cancelled' OR status = 'Returned' AND payment_mode = 'UPI'
-- clear:
WHERE (status = 'Cancelled' OR status = 'Returned') AND payment_mode = 'UPI'

IN - match any value in a list#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

BETWEEN - ranges (inclusive)#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

LIKE - text patterns#

Pattern Matches
'A%' starts with A
'%Laptop%' contains "Laptop"
'%a' ends with a
'_a%' second letter is a (_ = exactly one character)
▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

(In SQLite and MySQL, LIKE is case-insensitive for English letters; in PostgreSQL use ILIKE for that.)

IS NULL - missing values#

NULL means "no value / unknown". It is not zero or an empty string, and = NULL never matches.

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
हिंदी में समझें

NULL का मतलब है "value मौजूद नहीं / पता नहीं" - यह 0 या खाली text नहीं है।

इसलिए WHERE manager_id = NULL कभी काम नहीं करता (क्योंकि "अज्ञात" की तुलना किसी से नहीं हो सकती)। हमेशा IS NULL या IS NOT NULL लिखें।

ORDER BY - sorting#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
  • ASC (default) = smallest/A first; DESC = largest/Z first.
  • Sort by several columns: first by category, then by price within each category.

LIMIT - top N#

SELECT name, price FROM products ORDER BY price DESC LIMIT 3;   -- 3 most expensive

(SQL Server uses SELECT TOP 3 ...; Oracle uses FETCH FIRST 3 ROWS ONLY.)

DISTINCT - unique values#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

DISTINCT applies to the whole row: SELECT DISTINCT city, state returns unique city-state pairs.

Calculated columns and aliases#

You can calculate new columns and name them with AS:

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
Tip

Write 100.0 (not 100) when dividing integers in SQLite/SQL Server - otherwise integer division drops decimals (7 / 2 = 3).

How SQL processes a query#

You write clauses in this order:

SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT

But the database executes them in this order:

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

That's why an alias created in SELECT can be used in ORDER BY, but not in WHERE (in most databases).

Summary#

  • WHERE filters rows with =, <>, >, <, AND, OR, NOT, IN, BETWEEN, LIKE and IS NULL.
  • ORDER BY ... ASC/DESC sorts; LIMIT gives top N.
  • DISTINCT returns unique values.
  • Create calculated columns and name them with AS.
  • Logical order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Customers in Madhya Pradesh

Show name and city of customers whose state is Madhya Pradesh.

Database: shop (see the tables in this lesson)
2

Expensive technology

Show name and price of Technology products priced above 10,000, most expensive first.

Database: shop (see the tables in this lesson)
3

Digital payments in 2025

Show order_id, order_date and payment_mode of orders in 2025 paid by UPI or Card, sorted by date (oldest first).

Database: shop (see the tables in this lesson)
4

Profit per unit

For every product show name, price, cost and a calculated column profit_per_unit (price - cost), sorted by profit_per_unit descending.

Database: shop (see the tables in this lesson)

Quick Quiz

Q1.

Which condition finds products priced between 1,000 and 10,000 (inclusive)?

Q2.

Which finds customers whose name starts with "A"?

Q3.

How do you find rows where manager_id has no value?

Q4.

What does ORDER BY price DESC do?

Q5.

In which order does SQL logically process these clauses?