Prepstellar

SQL Fundamentals · Getting started

25 cards

What You Will Learn in This SQL Course

Swipe, scroll or use ← →
  1. How each lesson works

    Welcome. Before we talk about SQL itself, here is how a lesson is built, so you always know where you are.

    A lesson is a short stack of slides. Many slides show a small table, the query that runs on it, and the result that comes back. Between the slides there is a quick check with three options, so you find out straight away whether the idea landed. You can read in silence, or turn the narration on and follow the words as they light up.

    After the slides come ten questions on the same material. Most concepts then add a hands-on practice of six steps, where you complete a real query yourself and see what it returns.

    One more comfort. The whole course uses a single small data world, a music store with artists, albums, tracks, customers and invoices. The same rows come back lesson after lesson, so your attention stays on the SQL.

    1 / 25
  2. Quick check

    You open a lesson. What will you meet, and in what order?

    1. ATen questions first, and the slides only at the very end

      The questions come after the slides, once the material has been shown.

    2. BSlides with a quick check, then ten questions, then the practice where the concept has one

      Right. Teaching slides and quick checks first, then the ten questions, then the six-step practice when the concept carries one.

    3. CA different music store in each lesson, with new artists and albums

      The data world never changes: the same small music store runs through the whole course.

    2 / 25

  3. Block one: ask one table

    The first block gives you the vocabulary and your first real answer. You meet the relational model, see how a SQL statement is written, learn how values, types and expressions behave, and meet the special marker that means no value was recorded here. Then you write your first query, and you learn how to add, change and remove rows.

    Here are four rows of the tracks table:

    track_id name unit_price
    1 For Those About To Rock 0.99
    2 Balls to the Wall 0.99
    8 Walk On Water 1.29
    9 All I Really Want 1.29
    3 / 25
  4. Block one: ask one table

    By the end of the block, this is the kind of query you write:

    SELECT name, unit_price
    FROM tracks
    WHERE unit_price > 0.99
    ORDER BY name;
    
    name unit_price
    All I Really Want 1.29
    Walk On Water 1.29

    You are not expected to understand that syntax today. Read it as a promise instead: after block one, the question "which tracks cost more than 0.99, listed by name?" is one you answer yourself.

    4 / 25
  5. Quick check

    Only the tracks table is available. Which job is already inside block one?

    1. APutting the track names next to the name of the artist who recorded them

      Names from a second table arrive in block two, where you learn to join tables.

    2. BTurning the whole table into one line with the average price of a track

      One summary line for many rows is aggregation, and that is block three.

    3. CPicking the rows that match a condition and sorting them by name

      Right. Filtering and sorting the rows of one table is exactly what block one gives you.

    5 / 25

  6. Block two: join two tables

    One table rarely holds the whole answer. Titles live in albums, names live in artists, and only a shared number links them.

    artist_id name
    1 AC/DC
    2 Accept
    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

    Block two brings tables together. You will give tables short aliases and pair their rows with inner, cross and outer joins, including the rows that have no partner. You will also join a table to itself, join three tables at once, and stack one result on top of another with set operations.

    6 / 25
  7. Block two: join two tables

    So, which albums belong to AC/DC?

    SELECT albums.title
    FROM albums
    JOIN artists
      ON albums.artist_id = artists.artist_id
    WHERE artists.name = 'AC/DC'
    ORDER BY albums.title;
    
    title
    For Those About To Rock
    Let There Be Rock

    Two tables went in and one clean answer came out. That is the whole point of the block.

    7 / 25
  8. Quick check

    Why does the question about the albums of AC/DC need block two?

    1. AThe title and the artist name live in two separate tables

      Right. Joining them on the shared artist number puts the two facts side by side.

    2. BThe answer is one number calculated from many album rows

      Counting or adding many rows into a single number is block three.

    3. CThe answer rewrites the stored rows instead of reading them

      Nothing is rewritten here; the query only reads what is already stored.

    8 / 25

  9. Keep your progress in the app

    That’s 3 of 10 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
  10. Block three: summarise a group

    Sometimes you do not want the rows themselves; you want what they add up to. Block three teaches the aggregate functions that count, add and average, the grouping that applies them once per artist or once per month, the condition that filters whole groups, and window functions, which calculate across related rows without folding them into one.

    Six album rows go in:

    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
    9 / 25
  11. Block three: summarise a group

    SELECT artists.name, COUNT(*) AS n_albums
    FROM albums
    JOIN artists
      ON albums.artist_id = artists.artist_id
    GROUP BY artists.name
    ORDER BY artists.name;
    
    name n_albums
    AC/DC 2
    Accept 2
    Aerosmith 1
    Alanis Morissette 1

    Four rows come out, one per artist. Notice that block three stands on block two: the join is still there, and the counting sits on top of it.

    10 / 25
  12. Quick check

    Six album rows go in and four rows come out. What happened to them?

    1. ATwo albums were deleted from the stored table by this query

      A query that reads does not delete anything; all six albums are still stored.

    2. BA filter removed the two albums with no matching artist row

      Every album here has a matching artist, so no album was filtered out.

    3. CThe rows were grouped by artist and each group counted

      Right. Grouping puts the albums of one artist together, and the count replaces them with one row.

    11 / 25

  13. Block four: put one query inside another

    Block four lets one query use the answer of another. You learn subqueries that give back a single value, a single row or a whole table; correlated subqueries, which are worked out again for every row; the existence test, which only asks whether any matching row is there; and common table expressions, which give a subquery a name at the top of the statement and can even walk down a chain of rows step by step.

    The store has five artists, but only four of them have an album. Which one is missing?

    artist_id name
    1 AC/DC
    2 Accept
    3 Aerosmith
    4 Alanis Morissette
    5 Alice In Chains
    12 / 25
  14. Block four: put one query inside another

    SELECT name
    FROM artists
    WHERE NOT EXISTS (
      SELECT 1 FROM albums
      WHERE albums.artist_id = artists.artist_id
    )
    ORDER BY name;
    
    name
    Alice In Chains

    The inner query runs for each artist and asks one small question: is there an album for this artist? The outer query keeps only the artists for which the answer is no.

    13 / 25
  15. Quick check

    What is the inner query doing for each artist in this example?

    1. ASorting the artists table before the outer query reads it

      The sorting is done by the last line of the outer query, not by the inner one.

    2. BAsking whether that artist has any album at all

      Right. It looks for one matching album, and the artist is kept only when none is found.

    3. CReplacing the artists table with the albums table

      Both tables stay in play; the inner query only checks the albums of one artist at a time.

    14 / 25

  16. Block five: build your own tables

    Until now the tables were already there. Block five hands you the design. You create your own tables, give a column a default value, let the database work out a column from the others, and add the rules that keep the data honest: a column that may not be left empty, a value that may not repeat, and a reference that must point at a row that really exists. You also learn to change a table that is already in use, without losing what is inside it.

    CREATE TABLE label_notes (
      artist_id INTEGER PRIMARY KEY,
      note      VARCHAR(40) NOT NULL,
      active    INTEGER DEFAULT 1
    );
    INSERT INTO label_notes (artist_id, note)
    VALUES (1, 'back catalogue');
    SELECT * FROM label_notes;
    
    artist_id note active
    1 back catalogue 1

    The insert never mentions the third column, and the stored row still has a value there. That is what a default does for you.

    15 / 25
  17. Quick check

    The insert names two columns, yet the stored row shows three values. Why?

    1. AThe third column was declared with a default value of its own

      Right. When the insert leaves the column out, the table definition supplies the value.

    2. BThe database copied that value from another table

      Nothing is copied from elsewhere; the value comes from the column definition.

    3. CA second insert had already stored the same row

      Only one row was inserted, and the key would not allow a second row with the same number.

    16 / 25

  18. Block six: change several rows as one unit

    Some changes only make sense together. Block six is about the transaction: a group of statements that succeeds as one unit, or leaves the data exactly as it was. Inside a transaction you can also set a savepoint, a marker you can come back to, so you undo part of the work and keep the rest.

    The two tracks below both cost 1.29 before the change.

    track_id name unit_price
    8 Walk On Water 1.29
    9 All I Really Want 1.29
    17 / 25
  19. Block six: change several rows as one unit

    BEGIN;
    UPDATE tracks SET unit_price = 1.49
     WHERE track_id = 8;
    SAVEPOINT s1;
    UPDATE tracks SET unit_price = 1.49
     WHERE track_id = 9;
    ROLLBACK TO SAVEPOINT s1;
    COMMIT;
    
    track_id unit_price
    8 1.49
    9 1.29

    Both updates ran, but the work done after the savepoint was undone before the group was committed. Track 8 keeps its new price, and track 9 goes back to the old one.

    18 / 25
  20. Quick check

    Both updates ran, yet only one new price survived. What decided that?

    1. AThe second update was rejected because the new price was too high

      No rule was broken here; both updates ran without complaint.

    2. BEach update was saved on its own, so the last one wins

      Inside a transaction the changes are not saved one by one; they are decided together at the end.

    3. CThe work after the savepoint was undone before the commit

      Right. Returning to the savepoint cancels only what came after it, and the rest is then committed.

    19 / 25

  21. Block seven: save a query and find rows faster

    Block seven is about two comforts. A view saves a query under a name and then behaves like a table for everyone who reads it, so a long query written once can be used again without being written again. An index is a structure the database keeps beside a table so it can find matching rows without reading every one of them. The database decides when to use it, and on a small table it may read the rows instead.

    CREATE VIEW standard_tracks AS
    SELECT name, unit_price
    FROM tracks
    WHERE unit_price = 0.99;
    SELECT * FROM standard_tracks
    ORDER BY name;
    
    name unit_price
    Balls to the Wall 0.99
    For Those About To Rock 0.99

    The second statement reads standard_tracks as if it were a table, and it brings back the two tracks priced 0.99 from the four rows you saw in block one. An index changes nothing in this result. At most it changes how the rows are reached, and only when the database judges that route the cheaper one.

    20 / 25
  22. Quick check

    What does adding an index to the tracks table change?

    1. AHow matching rows may be reached, not the rows returned

      Right. An index offers a shortcut the database can take; the answer itself stays the same.

    2. BThe result of the view, which then returns fewer rows

      The view keeps its own condition, so an index does not change which rows it returns.

    3. CThe column names, which the index rewrites for the table

      An index is stored beside the table and leaves the column definitions untouched.

    21 / 25

  23. Block eight: everyday functions

    The last block collects the small tools you reach for every day. You learn the conditional expression that chooses a value row by row, the conversion that turns one kind of value into another, the text functions that cut, join and clean up strings, and the date functions that pull a year or a month out of a date.

    SELECT name,
           CASE WHEN unit_price > 0.99
                THEN 'premium' ELSE 'standard'
           END AS band
    FROM tracks
    ORDER BY name;
    
    name band
    All I Really Want premium
    Balls to the Wall standard
    For Those About To Rock standard
    Walk On Water premium

    The four tracks keep their four rows, and each one gets a label decided by its own price. These eight blocks teach SQL as the standard describes it, so the ideas carry over between database products and the skill follows you when the product changes. Syntax, data types and functions still differ in places, so read the manual of the product you land on and run a statement there before you trust it.

    22 / 25
  24. Quick check

    The four tracks keep their four rows, but a new column appears. What produced it?

    1. AA grouping that folded the four prices into one

      Grouping would have replaced the four rows with fewer rows, and all four are still here.

    2. BA choice made row by row from the price of that row

      Right. The conditional expression looks at one price and returns one label for that row.

    3. CA new column that was added to the stored table

      Nothing was added to the table; the label exists only in the result of this query.

    23 / 25

  25. Key takeaways

    • A lesson is slides with small tables and quick checks, then ten questions, and, where the concept carries one, a six-step practice on the same music store.
    • Block one gives you one table: filter its rows, sort them, and add, change or remove them.
    • Block two joins tables, so an album can be shown next to the name of its artist.
    • Block three summarises, so that counting and adding turn many rows into one row per group.
    • Block four puts one query inside another, so a query can use what a second query found.
    • Block five is where you create your own tables and the rules that protect them, and block six groups changes so they succeed or fail together.
    • Block seven saves a query as a view and speeds up the search with an index, and block eight adds the everyday choices, conversions, text tools and date tools.
    • The order matters: each block uses what the one before it gave you, and the ideas carry over between database products, with the syntax and the types checked against the manual of each one.
    24 / 25
  26. Quick check

    You can already filter and sort one table. Which block comes next in this course?

    1. ABlock five, where you design tables of your own

      Designing tables comes later; you first learn to get answers out of the tables that exist.

    2. BBlock eight, where the everyday functions live

      Those small tools come last, once you can already query, join, summarise and define.

    3. CBlock two, so an answer can use more than one table

      Right. Joining tables is the step straight after reading a single table.

    25 / 25

  27. 10 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: Relational Model and SQL

    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