SQL Basics (SELECT, WHERE, ORDER BY, LIMIT)
SELECT
Section titled “SELECT”Choose the columns you need.
SELECT user_id, country, created_at
FROM users;Filter rows.
SELECT user_id, country
FROM users
WHERE country = 'IN';Common operators
Section titled “Common operators”=,!=,>,<,>=,<=IN (...)BETWEEN a AND bLIKE '%text%'IS NULL/IS NOT NULL
SELECT user_id, created_at, plan
FROM users
WHERE country IN ('IN', 'US')
AND plan != 'free'
AND created_at >= '2025-01-01';ORDER BY
Section titled “ORDER BY”SELECT user_id, created_at
FROM users
ORDER BY created_at DESC
LIMIT 20;Limits are great for exploration.
SELECT *
FROM orders
LIMIT 50;The order SQL actually runs in
Section titled “The order SQL actually runs in”This trips up almost every beginner: SQL is not executed top-to-bottom in the order you type it. The database picks the table first, filters rows, then figures out which columns to show, and sorts/limits last.
flowchart TD A["FROM users"] --> B["WHERE country = 'IN'"] B --> C["SELECT user_id, country"] C --> D["ORDER BY created_at DESC"] D --> E["LIMIT 20"]
That’s why you can filter on a column in WHERE that you never mention in SELECT — the
engine already resolved WHERE against the full table before it trimmed the column list.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – SELECT specific columns
Section titled “Exercise 1 – SELECT specific columns”Exercise 2 – WHERE with multiple conditions
Section titled “Exercise 2 – WHERE with multiple conditions”Exercise 3 – ORDER BY and LIMIT together
Section titled “Exercise 3 – ORDER BY and LIMIT together”Once you’re comfortable filtering and sorting single tables, move on to Aggregations (COUNT, SUM, AVG) and GROUP BY to start computing metrics instead of just listing rows.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading