Chapter 02 · basic · 6 min
SELECT: reading data
Every SQL query you'll ever write starts with SELECT. It answers one question: which columns, from which table, do I want to see? Master this and the rest of SQL is just adding clauses onto it.
SELECT column1, column2
FROM table_name;We'll use a small rides table from a ride-hailing app. Run each example, then change it and run again: nothing you do here can break anything.
The dataset
A `rides` table with 6 rows: each ride has a city, a fare in ringgit, and a status.
Select everything
SELECT * returns every column, for every row. The * means "all columns". It's handy for exploring a table you've never seen, but in real queries you usually name the columns you actually want.
All columns, all rows
Pick specific columns
List the columns you want, separated by commas. The result keeps them in the order you asked for; here we flip city in front of fare:
| city | fare |
|---|---|
| Kuala Lumpur | 18.50 |
| Kuala Lumpur | 12.00 |
| Penang | 9.75 |
| Johor Bahru | 22.30 |
| Penang | 15.00 |
| Johor Bahru | 8.40 |
Just two columns
Why not just always SELECT *?
It's tempting to always grab everything and sort it out later. But on a real table, with millions of rows and dozens of columns, SELECT * pulls far more data across the network than you need, which is slower and, on cloud warehouses billed by data scanned, more expensive. It also breaks quietly: if someone adds a new column to the table later, every SELECT * query downstream now returns an extra column it didn't ask for, which can silently break a report or an app that assumed a fixed shape. Naming columns explicitly avoids both problems.
Rename a column with AS
AS gives a column a friendlier name in the output, an alias. This doesn't change the table; it only relabels the result:
| city | fare_myr |
|---|---|
| Kuala Lumpur | 18.50 |
| Kuala Lumpur | 12.00 |
| Penang | 9.75 |
| Johor Bahru | 22.30 |
| Penang | 15.00 |
| Johor Bahru | 8.40 |
Aliases matter later when a column is a calculation (like fare * 0.8) and would otherwise have an ugly auto-generated name.
Aliasing fare -> fare_myr
Remove duplicates with DISTINCT
SELECT DISTINCT collapses repeated rows into one. Ask for the distinct city values and the three cities each appear once, even though there are six rides:
| city |
|---|
| Kuala Lumpur |
| Penang |
| Johor Bahru |
If you've used Excel or Google Sheets, this is the same job as Data → Remove duplicates, or a PivotTable's list of unique row labels. SQL just does it as part of the query instead of a separate cleanup step. Try deleting DISTINCT and running it again to see the duplicates come back.
Ready to try a full example? Grab completed rides by city in the practice set below combines everything from this chapter.
Unique cities
That's the whole shape of a read query: which columns (SELECT), from where (FROM), with optional aliases and de-duplication. Next we'll narrow down which rows come back, with WHERE.