Prepstellar

SQL Fundamentals · Getting started

14 cards

Database Structure and Core Vocabulary

Swipe, scroll or use ← →
  1. Read one column of a table

    A database is a place that holds tables. Each table has a name, a set of named columns, and rows of data. This is the whole artists table of our small music store.

    artist_id name
    1 AC/DC
    2 Accept
    3 Aerosmith
    4 Alanis Morissette
    5 Alice In Chains

    Read across one row to see one artist. Read down one column to see the same fact for every artist. Where a row meets a column there is one box, and that box holds one value.

    The basic shape of a query that reads one column looks like this:

    SELECT column FROM table;
    

    So, to see the text of every artist and nothing else, write this:

    SELECT name
    FROM artists;
    
    name
    AC/DC
    Accept
    Aerosmith
    Alanis Morissette
    Alice In Chains

    The five rows are still there, and the column you did not ask for is gone. You name the column first and the table second.

    1 / 14
  2. Quick check

    What comes back from SELECT artist_id FROM artists;

    1. AThe five id numbers, one per row

      Right. The query keeps every row and shows only the column you named.

    2. BOne row holding the number 5

      The query does not count rows; it shows the value stored in each row.

    3. CThe five artist names

      That is the result of asking for the other column of the table.

    2 / 14

  3. Read two columns in order

    Each column also has a data type, which is the kind of value the column may store. In the albums table below, title stores text, while album_id and artist_id store whole numbers.

    album_id title artist_id
    1 For Those About To Rock 1
    2 Balls to the Wall 2
    3 Restless and Wild 2
    4 Let There Be Rock 1
    5 Big Ones 3
    6 Jagged Little Pill 4

    To read two columns, write both after SELECT with a comma between them.

    SELECT title, artist_id
    FROM albums;
    
    title artist_id
    For Those About To Rock 1
    Balls to the Wall 2
    Restless and Wild 2
    Let There Be Rock 1
    Big Ones 3
    Jagged Little Pill 4

    The columns come back in the order you wrote them. The rows are the same six albums as before, because asking for fewer columns never removes a row.

    3 / 14
  4. Quick check

    Which columns come back from SELECT artist_id, title FROM albums;

    1. AOnly the id numbers

      Both columns are named in the query, so both come back.

    2. BThe titles, then the ids

      The query names the id column first, so it is shown first.

    3. CThe ids, and then the titles

      Right. The columns are shown in the order the query wrote them.

    4 / 14

  5. Find the table that holds a fact

    Each table holds one kind of thing. The artists table holds artists, the albums table holds albums, and the tracks table holds songs. Before writing a query, find the table whose columns hold the fact you need. These are five songs of the store:

    track_id name album_id milliseconds composer
    1 For Those About To Rock 1 343719 Angus Young
    2 Balls to the Wall 2 342562 null
    3 Fast As a Shark 3 230619 F. Baltes
    7 Let There Be Rock 4 366654 Angus Young
    8 Walk On Water 5 295680 Steven Tyler

    How long a song lasts is stored in the milliseconds column, and only the tracks table has that column. The shape is the one you already know, and the table you choose goes after FROM:

    SELECT column FROM table;
    

    So this query reads the songs:

    SELECT name, milliseconds
    FROM tracks;
    
    name milliseconds
    For Those About To Rock 343719
    Balls to the Wall 342562
    Fast As a Shark 230619
    Let There Be Rock 366654
    Walk On Water 295680

    A column name only works in the table that has it. Asking the artists table for milliseconds fails, because that table has no such column.

    5 / 14
  6. Quick check

    What happens when you run SELECT name, milliseconds FROM artists;

    1. AThe song lengths of every artist come back

      Song lengths are not stored in that table at all.

    2. BIt fails: that table has no such column

      Right. A column name works only in the table that has it.

    3. CEmpty boxes come back for that column

      A name that does not exist is an error, not an empty box.

    6 / 14

  7. Keep your progress in the app

    That’s 3 of 7 quick checks. In the app they stay answered, and every lesson remembers where you left off.

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store
    Keep reading here
  8. Spot a missing value

    Sometimes a box holds no value at all. That empty box is called null, and it means the value is missing or unknown. The store never wrote down who composed the song Balls to the Wall, so its composer box is empty. These are three columns of the same five songs:

    track_id name composer
    1 For Those About To Rock Angus Young
    2 Balls to the Wall null
    3 Fast As a Shark F. Baltes
    7 Let There Be Rock Angus Young
    8 Walk On Water Steven Tyler

    The query asks for two of them in the usual way:

    SELECT name, composer
    FROM tracks;
    
    name composer
    For Those About To Rock Angus Young
    Balls to the Wall null
    Fast As a Shark F. Baltes
    Let There Be Rock Angus Young
    Walk On Water Steven Tyler

    The row is still there and the song is still shown. Only that one box is null. A null box is not the number zero and not an empty piece of text; the next block of the course works with null in full.

    7 / 14
  9. Quick check

    The composer box of Balls to the Wall is empty. What does the query return for that song?

    1. AThe row is left out of the result

      A missing value does not remove the row from the result.

    2. BThe number zero in that box

      A null box is not the number zero.

    3. CThe song, and a null box

      Right. The row comes back, and the one missing box is null.

    8 / 14

  10. Find the key column

    One column of a table is often used to point at a single row. In artists that column is artist_id: the numbers 1 to 5 are never repeated, so the value 3 belongs to one row and no other. A column used in this way is called a key.

    That table has only two columns, so naming both of them brings back all of it, with the key first:

    SELECT artist_id, name
    FROM artists;
    
    artist_id name
    1 AC/DC
    2 Accept
    3 Aerosmith
    4 Alanis Morissette
    5 Alice In Chains

    Two artists could one day share the same text in name, and then only the key value would tell the two rows apart. That is why the rest of the store points at an artist by the key number and not by the text.

    9 / 14
  11. Quick check

    Why does the store point at an artist by artist_id and not by name?

    1. ABecause a key value points at one row

      Right. The key value belongs to one row and no other.

    2. BBecause numbers are shorter to store

      Size is not the reason; two artists could still share the same text.

    3. CBecause the rows are sorted by that column

      The stored rows are not sorted by the key, and sorting would not tell two rows apart.

    10 / 14

  12. Follow a column to another table

    A table can point at another table by storing its key. In albums, the artist_id column holds the key of the artist who made that album. The shape of the query does not change:

    SELECT album_id, artist_id
    FROM albums;
    
    album_id artist_id
    1 1
    2 2
    3 2
    4 1
    5 3
    6 4

    Album 5 stores the value 3, so you look in artists for the row whose artist_id is 3, which is Aerosmith. The two tables stay separate and the shared number is the link between them. Reading both tables in one query is called a join, and it comes later in the course.

    Album 5 stores the artist key 3, and that number points at the single artists row whose key is 3.points to*1album_idartist_id2253artist_idname2Accept3Aerosmith
    The two tables stay separate. Many albums can carry the same artist key, and each key leads to exactly one artist row.
    11 / 14
  13. Quick check

    Album 6 stores artist_id 4. Which artist made that album?

    1. AAccept

      Accept is the artist whose key value is 2.

    2. BAlanis Morissette

      Right. You look in the artists table for the row with that key value.

    3. CNobody: the value is null

      The value 4 is stored, so nothing here is missing.

    12 / 14

  14. Key takeaways

    • A database holds tables; a table has named columns and rows, and each box holds one value.
    • SELECT names the columns you want, FROM names the table, and the columns come back in the order you wrote them.
    • Each column has a data type, and a column name only works in the table that has it.
    • An empty box is null: the row is still there, but that value is missing.
    • A key column points at one row, and another table can reach that row by storing the same key value.
    13 / 14
  15. Quick check

    Which query shows how long each song lasts?

    1. ASELECT milliseconds FROM albums;

      The albums table has no column with song lengths.

    2. BSELECT length FROM tracks;

      There is no column with that name in the table.

    3. CSELECT milliseconds FROM tracks;

      Right. The column is in the table that holds the songs.

    14 / 14

  16. 7 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

    Take this lesson’s test → Next lesson: A SQL Workflow from Question to Result

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store

The whole course, on your computer and your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.

Opens in your browser. Nothing to install.

Start studying

On your phone, with the same account

Get it on Google Play Download on the App Store

“This app is the ‘One Lake’ for preparing the certification exams. Really worth it.”

Abdelhak Y. Google Play review