SELECT, the polite way to ask questions
SELECT is the first word of SQL most people learn, and also the most misunderstood. People think it just "gets data." It does more than that. It picks, it renames, it removes duplicates, it does arithmetic.
Today we keep it small. By the end of this lesson, you will write four kinds of SELECT and know when to reach for each one.
The shape of a SELECT
Every SELECT has the same skeleton.
SELECT column_list
FROM table_name;That is the whole sentence. column_list is what you want. table_name is where it lives. The semicolon ends the query.
- All columns: use
*. Quick for exploring, lazy for production. - Specific columns: list them, comma separated.
- Computed columns: SQL can do math right inside SELECT.
SELECT name, price, price * 1.18 AS price_with_gst
FROM menu_items;That AS gives the new column a name. Without it, your output column would be called something ugly like ?column?.
Read it backwards: FROM, then SELECT
Here is the trick that unlocks SQL for most people. Even though you write SELECT first, the database reads FROM first. It walks to the table, picks up the rows, and only then decides which columns to show you.
Mentally read every query as: "Go to this table. Then pick these columns." FROM, then SELECT. This one habit fixes half the bugs beginners hit.
Once you internalise this, JOINs and WHERE will feel obvious later. Promise.
DISTINCT, the duplicate killer
Your orders table has a row per order. You want to know which cities have ever ordered from us. If you write SELECT city FROM orders, you will see Bengaluru 4,000 times.
SELECT DISTINCT city FROM orders;That returns each city once. DISTINCT is cheap, readable, and the right tool when you genuinely want unique values. Do not reach for GROUP BY just to dedupe. DISTINCT exists for exactly this.
Picking columns is a kindness
Two reasons to avoid SELECT * in real code:
- Performance. Wider rows mean more bytes shipped over the network. On a busy Swiggy-scale table, this adds up fast.
- Stability. If someone adds a column tomorrow, your code suddenly sees it. Better to ask for exactly what you need.
SELECT * is fine in the playground. Once you ship code, name your columns.
Try it on the right
Three assignments are waiting in the right panel. Start with the easiest one. Read each query out loud before you hit run. If it reads like English, you are on the right path.
Next lesson: WHERE, ORDER BY, and LIMIT. The three clauses that turn "give me data" into "give me the right data."
Free tools you can use while you learn
Watching quietly. Tap me if you want a tip.
Try this (0 of 3 done)
- 1
Show every restaurant's name and cuisine (just those two columns).
hint
Two column names, comma separated, after SELECT.
show answer
SELECT name, cuisine FROM restaurants; - 2
List all the distinct cuisines we serve (no duplicates).
hint
SELECT DISTINCT cuisine ...
show answer
SELECT DISTINCT cuisine FROM restaurants; - 3
Give each restaurant a label "Eatery" alongside its name.
hint
SELECT name, 'Eatery' AS label FROM ...
show answer
SELECT name, 'Eatery' AS label FROM restaurants;