Read data from a table with SELECT, choosing the columns you want.
A music app knows thousands of songs. Asking it for just the titles takes one line.
A database stores information in tables, like a spreadsheet with strict rules. SQL is the language you use to ask a table a question. The question comes back as a new table.
Each table has columns (the kinds of thing stored, such as title) and rows (one song each). This course uses a small music library.
Make your prediction first. Help opens once you've locked it in.
SELECT title, artist
FROM songs;
SELECT chooses columns. FROM says which table.
Run it and open Tables in this database to see every column in songs.
| Part | What it does |
|---|---|
SELECT title, artist |
Picks two columns, separated by a comma |
FROM songs |
Says which table to read |
; |
Ends the query |
SELECT title, artist
FROM songs;
SELECT *
FROM songs;
* means every column.
The order of the words matters.
Run the query. How many columns does the table have?
Change the query so it shows each song's title and genre, and nothing else.
Before a hint: what kind of stuck is this? Try a strategy first
The columns you want go after SELECT, separated by commas.
You want title, then genre.
Show only the title and genre of every song.
Run your program. This ticks when your code does all of the above.
Write your own query for a playlist card. Choose three columns that make a good card and run it.
Before a hint: what kind of stuck is this? Try a strategy first
Pick three columns you would put on a playlist card.
SELECT column, column, column FROM songs;
Write a query that shows exactly three columns from songs.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
Students meet tables, rows and columns, and write SELECT with named columns and with a star. About 25 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Putting SELECT, the columns and FROM in order. Say the query aloud: select these, from that table.
Stretch: Show the columns in a different order and see that the table follows.
Support: Give the query with one column filled in and ask for the second.
Next lesson: show only the rows that match, such as one genre.