Manuals / SQL / Ch 2

A · QueryBeginner30 min read

2. SELECT basics — reading rows

SQL · 48 pages source format

Every query starts with SELECT. Name columns explicitly in production. DISTINCT removes duplicates. Aliases rename columns or tables.

What you'll learn

  • SELECT columns
  • DISTINCT
  • Column aliases
  • FROM and table names

Basic SELECT

SELECT col1, col2 FROM table. Avoid SELECT * in production — it hides schema changes and fetches unnecessary data.

SELECT id, name, email
FROM users;

Do this now

SQLBolt lesson 1. Save query as queries/01_select.sql.

Clear?

DISTINCT and aliases

SELECT DISTINCT country FROM customers. SELECT total AS order_total — aliases clarify output headers.

Do this now

List unique product categories. Alias a computed column: quantity * price AS line_total.

Clear?

Explore schema first

Before querying: .schema in SQLite, \d in psql, or DESCRIBE in MySQL. Know column names and types.

Do this now

Document your sandbox schema in sql-notes/SCHEMA.md — tables, keys, sample row counts.

Pro tip. QA tip: schema docs become test oracle references.

Clear?

Checklist