SELECT, FROM & Column Aliases
Return all columns with SELECT *, pick specific columns, add calculated columns and rename them with AS.
The most basic question you can ask a database is "show me the data". The SELECT statement does that. You decide which columns you want, and the FROM clause tells SQL which table to read them from.
Definition
SELECT is the DQL statement that retrieves data from one or more tables. The SELECT clause lists the columns (or expressions) to return, and the FROM clause names the table that holds the data.
A column alias is a temporary name given to a column or expression in the result, written with
AS. It changes only the heading of the result — not the table.
Syntax
sql
SELECT column1, column2, expression AS alias_name
FROM table_name;
SELECT * -- * = every column
FROM table_name;| Part | Meaning |
|---|---|
SELECT | Which columns to show (comma-separated) |
* | Shortcut for "all columns", in table order |
expression | A calculation such as runs * 2 or LOWER(team) |
AS alias_name | New heading for that column in the result |
FROM table_name | Where to find the data |
Example — SELECT * (all columns)
sql
SELECT *
FROM players;| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | DC | Australia | 700 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Execution order: 1️⃣ FROM players — find the table → 2️⃣ SELECT * — keep all columns.
Example — SELECT a few columns
sql
SELECT name, team
FROM players;| name | team |
|---|---|
| Virat Kohli | RCB |
| Rohit Sharma | MI |
| Jos Buttler | RR |
| MS Dhoni | CSK |
| David Warner | DC |
| Jasprit Bumrah | MI |
How it works: FROM loads the table, then SELECT name, team keeps only those two columns, in the order you listed them. Every row is still returned — SELECT chooses columns, not rows (WHERE chooses rows).
Example — aliases and a calculated column
sql
SELECT name AS player_name,
runs AS season_runs,
runs * 2 AS double_runs
FROM players;| player_name | season_runs | double_runs |
|---|---|---|
| Virat Kohli | 750 | 1500 |
| Rohit Sharma | 500 | 1000 |
| Jos Buttler | 850 | 1700 |
| MS Dhoni | 350 | 700 |
| David Warner | 700 | 1400 |
| Jasprit Bumrah | 20 | 40 |
How it works: double_runs doesn't exist in the table — SQL computes it for each row. AS gives every result column a friendly heading. The players table itself is not changed.
Tip: If an alias contains a space, wrap it:
AS [Season Runs](SQL Server) orAS "Season Runs"(standard SQL). The wordASis optional (runs season_runsalso works), but writing it makes queries clearer.
Why avoid SELECT * in real projects?
SELECT * is great for exploring a table. In production code, list the columns you need:
- Performance — fewer columns means less data read from disk and sent over the network.
- Stability — if someone adds a column to the table later, your app suddenly receives extra data.
- Readability — the reader knows exactly what the query returns.
Common mistakes
- ❌ Missing comma:
SELECT name team FROM players— SQL treatsteamas an alias forname! ✅SELECT name, team. - ❌ Trailing comma:
SELECT name, team, FROM players→ syntax error near FROM. ✅ No comma beforeFROM. - ❌ Misspelt column → Invalid column name 'nmae'. ✅ Check spelling against the table.
Interview tip
Q: Does a column alias change the table?
A: No. An alias only renames the column in that query's result. To rename a real column you need DDL (sp_rename in SQL Server, ALTER TABLE … RENAME COLUMN elsewhere).
Key points to remember
- SELECT retrieves data; the SELECT clause picks columns and FROM names the table.
- SELECT * returns every column — handy for exploring, but list columns explicitly in real code.
- List the columns you need separated by commas; they appear in the order you write them.
- Execution order: FROM runs first (find the table), then SELECT (keep the columns).
- Expressions like runs * 2 create calculated columns that don't exist in the table.
- AS gives a column a temporary alias in the result; the table itself never changes.
Try it yourself
SELECT * — every column
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Only the columns you need
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Aliases and a calculated column
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: build your own column list
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
