Brainlag
  1. Library
  2. Concepts
  3. SQL

SELECT, columns and aliases

SELECT picks which columns come back from a table, can work out new ones from them, and AS gives any column the name you want.

SQL8 min

Pick the columns you want

A query starts with what you want back, then where it lives:

SQL
SELECT title, year
FROM songs;

SELECT lists the columns, in the order you want them. FROM names the table. You get every row of songs, but only those two columns. SELECT * means every column, which is handy for a first look at a table and messy in anything you keep.

SQL does not care about capitals or line breaks: select title from songs works too. Writing keywords in capitals and one clause per line just makes queries easier to read.

Work things out and rename them

A column in SELECT does not have to exist in the table. You can do maths on the ones that do:

SQL
SELECT title, seconds / 60.0 AS minutes
FROM songs;

AS gives the new column a name. Without it, the heading would be the whole expression, seconds / 60.0. Two things to know: an integer divided by an integer stays a whole number in SQLite (seconds / 60 gives 2, not 2.97), and ROUND(x, 1) rounds to one decimal place.

Run it: song lengths in minutes

Run it, then change one thing and run it again.

Tables artists, songs, listeners, plays
artists 8 rows, 4 columns
idnamecountrygenre
1Nova LaneUKpop
2The TidewaterUSindie
3Kaito MoriJPelectronic
4Amara OseiGHafrobeats
5Lumen ChoirSEpop
6Rust & VelvetUKrock
7Pixel HeartsKRpop
8Juno BayAUindie
songs 30 rows, 5 columns
idtitleartist_idsecondsyear
1Neon Rain61782022
2Slow Motion11582025
3Paper Planes22332023
4Static12692020
5Golden Hour11622022
6Night Bus71572020
7Echoes22812022
8Sugar Rush12842019
9Low Tide42892019
10Glass House71522020
11Satellite12822025
12Wildfire32142022

First 12 of 30 rows. Query the table to see the rest.

listeners 10 rows, 4 columns
idusernameagecountry
1maya_k15UK
2devraj17IN
3lucia.p16ES
4tom_h14UK
5ana.s18BR
6kenji16JP
7zoe_r15US
8femi17NG
9liv13SE
10oscar.b16NULL
plays 70 rows, 5 columns
idlistener_idsong_iddayskipped
110162026-09-010
2832026-09-010
35162026-09-010
4222026-09-010
55212026-09-010
65232026-09-020
7612026-09-020
8662026-09-020
9822026-09-021
10552026-09-020
117132026-09-020
12262026-09-030

First 12 of 70 rows. Query the table to see the rest.

query.sql to run
SELECT title,
       seconds,
       seconds / 60 AS whole_minutes,
       ROUND(seconds / 60.0, 1) AS minutes
FROM songs
ORDER BY seconds DESC
LIMIT 5;

How old is each song

For every song, return its title, its year, and a column named age that says how many years old it is in 2026.

exercise_1.sql to run
SELECT title
FROM songs;

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.