Python Central HubSign in

Data Analytics with Python / Phase 9: SQL for Data Analytics

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

Write your first real analytics queries using selection, filtering, sorting, and limiting.

SELECT

Choose the columns you need.

SQL
SELECT user_id, country, created_at
FROM users;

WHERE

Filter rows.

SQL
SELECT user_id, country
FROM users
WHERE country = 'IN';

Common operators

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

ORDER BY

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

LIMIT

Limits are great for exploration.

SQL
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 WHERE that you never mention in SELECT — the engine already resolved WHERE against the full table before it trimmed the column list.

Check your understanding

quizCheck yourself
  1. In what order does the database logically process a query?

    Show answer

    B — FROM, WHERE, SELECT, ORDER BY, LIMIT — The engine picks the table, filters rows, chooses columns, then sorts and limits last.

  2. Which query returns the 20 newest users?

    Show answer

    A — ORDER BY created_at DESC LIMIT 20 — DESC sorts newest first, and LIMIT 20 keeps the first twenty rows.

  3. Which operator tests for missing values?

    Show answer

    B — IS NULL — NULL needs IS NULL or IS NOT NULL. A normal comparison with = never matches.

  4. Why can you filter in WHERE on a column that is not in SELECT?

    Show answer

    A — WHERE runs before SELECT, against the full table — WHERE is resolved before the column list is trimmed.

🧪 Try It Yourself

Exercise 1 – SELECT specific columns

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – WHERE with multiple conditions

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – ORDER BY and LIMIT together

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

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.

Was this page helpful?

Let us know how we did