Skip to content

SQL Basics (SELECT, WHERE, ORDER BY, LIMIT)

SELECT

Choose the columns you need.

Select columns
SELECT user_id, country, created_at
FROM users;
Select columns
SELECT user_id, country, created_at
FROM users;

WHERE

Filter rows.

Filter by country
SELECT user_id, country
FROM users
WHERE country = 'IN';
Filter by country
SELECT user_id, country
FROM users
WHERE country = 'IN';

Common operators

  • ==, !=!=, >>, <<, >=>=, <=<=
  • IN (...)IN (...)
  • BETWEEN a AND bBETWEEN a AND b
  • LIKE '%text%'LIKE '%text%'
  • IS NULLIS NULL / IS NOT NULLIS NOT NULL
Multiple filters
SELECT user_id, created_at, plan
FROM users
WHERE country IN ('IN', 'US')
  AND plan != 'free'
  AND created_at >= '2025-01-01';
Multiple filters
SELECT user_id, created_at, plan
FROM users
WHERE country IN ('IN', 'US')
  AND plan != 'free'
  AND created_at >= '2025-01-01';

ORDER BY

Sort newest users
SELECT user_id, created_at
FROM users
ORDER BY created_at DESC
LIMIT 20;
Sort newest users
SELECT user_id, created_at
FROM users
ORDER BY created_at DESC
LIMIT 20;

LIMIT

Limits are great for exploration.

Inspect sample rows
SELECT *
FROM orders
LIMIT 50;
Inspect sample rows
SELECT *
FROM orders
LIMIT 50;

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.

diagram Query execution order mermaid
SQL runs FROM/WHERE first, then picks columns, and sorts/limits last — not top-to-bottom as written.

That’s why you can filter on a column in WHEREWHERE that you never mention in SELECTSELECT — the engine already resolved WHEREWHERE against the full table before it trimmed the column list.

🧪 Try It Yourself

Exercise 1 – SELECT specific columns

Exercise 2 – WHERE with multiple conditions

Exercise 3 – ORDER BY and LIMIT together

Next

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.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did