← SQL and databases

Look inside a table

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.

How this lesson works: Predict, Run, Investigate, Modify, Make
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.sql
SELECT title, artist
FROM songs;

SELECT chooses columns. FROM says which table.

The songs table has 15 songs. What will this query show?

RunRun it and compare the result with your prediction.

Run

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
lesson.sql
SELECT title, artist
FROM songs;
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
SELECT *
FROM songs;

* means every column.

The order of the words matters.

Run the query. How many columns does the table have?

You want only the year of every song. Which query does that?

ModifyChange the code on purpose and check the result.

Modify

Change the query so it shows each song's title and genre, and nothing else.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which query shows only title and plays?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

The columns you want go after SELECT, separated by commas.

Another hint

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.

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Write your own query for a playlist card. Choose three columns that make a good card and run it.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Pick three columns you would put on a playlist card.

    Another hint

    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.

      Now you can
      Next lesson2. Keep only the rows you wantFilter rows with WHERE, using = for text and > or < for numbers.
      For teachers

      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.

      Students will be able to

      Running the lesson

      1. Predict: the wrong options mix up rows and columns. Ask how many columns the answer has before anyone runs it.
      2. Run: open the table list so students see the real column names before they type any.
      3. Investigate: the star is shorthand, and the row count does not change.

      The hard part (on purpose)

      Putting SELECT, the columns and FROM in order. Say the query aloud: select these, from that table.

      When students get stuck

      Stretch and support

      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.

      Where it leads

      Next lesson: show only the rows that match, such as one genre.

      What each step is for