Lesson 10 of 30
33%
SELECT
SELECT reads rows. It does not change them.
Star means every column. You can also name the columns you want.
All columns
sql
Result
| id | name | age | city |
|---|---|---|---|
| 1 | Ada | 12 | London |
| 2 | Sam | 11 | Kenya |
| 3 | Lin | 13 | Taipei |
| 4 | Omar | 12 | Cairo |
| 5 | Nia | 10 | Unknown |
5 rows
Some columns
Name the columns you need. The result is smaller and clearer.
sql
Result
| name | city |
|---|---|
| Ada | London |
| Sam | Kenya |
| Lin | Taipei |
| Omar | Cairo |
| Nia | Unknown |
5 rows
Rename a column in the result
AS is a nickname for the output. The table itself does not change.
sql
Result
| student_name | years_old |
|---|---|
| Ada | 12 |
| Sam | 11 |
| Lin | 13 |
| Omar | 12 |
| Nia | 10 |
5 rows
Expressions
SELECT can compute, not only copy.
sql
Result
| name | age | next_birthday |
|---|---|---|
| Ada | 12 | 13 |
| Sam | 11 | 12 |
| Lin | 13 | 14 |
| Omar | 12 | 13 |
| Nia | 10 | 11 |
5 rows
DISTINCT
DISTINCT hides duplicate values in the result.
sql
Result
| city |
|---|
| London |
| Kenya |
| Taipei |
| Cairo |
| Unknown |
5 rows
Tip: Prefer named columns over SELECT * in real apps. You see only what you asked for.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.