Level 1: Reading a table with SELECT
Updated on
Beginner · level 1 of 20. Goal: Show the columns you want from a table, rename them and compute new ones.
Topics: SELECT * AS alias computed columns
Lesson
A database stores its information in tables. A table looks like a spreadsheet: each row is an item (a movie, a customer, a sale) and each column is a piece of information about that item (its title, its year, its duration). All the rows of a table have the same columns.
To read a table, you write a query that starts with SELECT. Right after SELECT comes the list of columns you want, separated by commas, in the order you want to see them. Then FROM gives the name of the table. SELECT title, year FROM movies shows the title and the year of each movie. The star * means “all the columns”: SELECT * FROM movies is handy to discover a table.
A SELECT query never changes the table: it builds a result, a new table shown on screen. So you can experiment without any risk.
You can rename a column of the result with AS: this is an alias. title AS film shows the column under the name film. The alias only changes the display, not the table.
You can also calculate a column with + - * /: SQL does the calculation for each row, one by one. price * 2 AS price_for_two gives the price for two people on every row. As in maths, * and / come before + and -: add parentheses to force your own order of calculation.
Watch out for division: dividing two whole numbers gives a whole number, 95 / 60 equals 1. Write 60.0 to keep the decimals.
The final semicolon marks the end of the query. Keywords such as SELECT and FROM can be written in lowercase, but they are often written in uppercase to tell them apart from column names.
Syntax
SELECT column1, column2 AS new_name, column3 * 2 AS double
FROM my_table;Worked example
SELECT title, year, duration / 60.0 AS hours
FROM movies;We read three pieces of information about each movie. The third one is computed: the duration in minutes divided by 60.0 gives hours, shown under the name hours. SQL shows every decimal; you will learn to round with ROUND in level 5.
| id | title | genre | year | duration | rating | director_id |
|---|---|---|---|---|---|---|
| 1 | Night Train | Thriller | 2015 | 118 | 7.8 | 1 |
| 2 | Blue Harbor | Drama | 2018 | 102 | 7.1 | 2 |
| 3 | Paper Moon City | Comedy | 2012 | 95 | 6.4 | 5 |
| 4 | Silent Peak | Drama | 2020 | 131 | 8.2 | 3 |
| 5 | Last Signal | Sci-Fi | 2019 | 142 | 7.5 | 1 |
| 6 | Summer Keys | Comedy | 2016 | 88 | 5.9 | 4 |
| 7 | Iron Garden | Sci-Fi | 2021 | 125 | 8 | 3 |
| 8 | Dust and Gold | Western | 2014 | 110 | 6.8 | 2 |
| 9 | The Quiet Hour | Drama | 2022 | 97 | 7.4 | 5 |
| 10 | Deep Current | Thriller | 2017 | 105 | 6.9 | NULL |
Example result
| title | year | hours |
|---|---|---|
| Night Train | 2015 | 1.9666666666666666 |
| Blue Harbor | 2018 | 1.7 |
| Paper Moon City | 2012 | 1.5833333333333333 |
| Silent Peak | 2020 | 2.183333333333333 |
| Last Signal | 2019 | 2.3666666666666667 |
| Summer Keys | 2016 | 1.4666666666666666 |
| Iron Garden | 2021 | 2.0833333333333335 |
| Dust and Gold | 2014 | 1.8333333333333333 |
| The Quiet Hour | 2022 | 1.6166666666666667 |
| Deep Current | 2017 | 1.75 |
Key points
- SELECT lists the columns, FROM names the table.
- * shows every column, in the table's order.
- AS gives a name to a result column, especially useful for computed columns.
Common pitfalls
- Forgetting the comma between two columns: SELECT title year reads title and renames it “year”!
- Dividing two whole numbers gives a whole number: 95 / 60 is 1. Write 60.0 to keep the decimals.
- price * capacity - seats_sold is not price * (capacity - seats_sold): multiplication comes before subtraction.
The level’s 5 exercises
- Show every column of the movies table.
- Show the title and genre of each movie.
- Show the title of each movie in a column named film, and its duration in a column named minutes.
- Show the name and price of each dish, plus the price for two people in a column named price_for_two.
- For each flight, show the airline, the flight's revenue (revenue: price × seats sold) and the revenue lost to empty seats (lost_revenue: price × empty seats).
In SpeedQL, every query is checked straight away: its result is compared with the expected one, then the query is run again on a hidden control database. Each exercise has written hints, to show only if you get stuck.
See also: the cheat sheet card · the SQL exercises with solutions on this topic
Do the level 1 exercises
Free, no sign-up: the lesson and the 5 exercises open right in your browser.