Free SQL Course / Lesson 1 of 10

SQL Course: Learn Database Queries with Examples

Learn SQL from reading tables to joins, analytics, safe data changes, schema design, and query performance. Examples use PostgreSQL syntax unless noted.

Lesson 1: Read data with SELECT

SQL describes the rows you want from a relational database. The examples use a table named orders with columns order_id, customer_id, order_date, and total. Put each clause on its own line so queries are easy to inspect.

Start by returning every column for a small number of rows, then select only the fields the task needs.

SELECT order_id, customer_id, total
FROM orders
ORDER BY order_date DESC
LIMIT 10;

Query clause reference

ClausePurposeExample
SELECTChoose columns or expressions returned.SELECT order_id, total
FROMChoose the source table or view.FROM orders
ASGive a column or expression a readable alias.total * 1.1 AS total_with_tax
DISTINCTReturn distinct selected rows.SELECT DISTINCT customer_id FROM orders
ORDER BYSort result rows; ASC is default, DESC is descending.ORDER BY order_date DESC
LIMITCap result rows in PostgreSQL and several other systems.LIMIT 10
OFFSETSkip rows; use stable ordering for paging.LIMIT 20 OFFSET 40
FETCH FIRSTStandard-style row limit supported by PostgreSQL.FETCH FIRST 10 ROWS ONLY
CommentsDocument intent without changing the result.-- latest ten orders

Copy a starter query: SELECT order_id, total FROM orders ORDER BY order_date DESC LIMIT 10;

Logical query order

Although SELECT appears first, a useful mental model is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, then LIMIT. This explains why a SELECT alias is not always available in WHERE.

Progress stays in this browser.