Skip to content
CodeItRaw
SQL: Joins

Lesson 1/2

Data is split into tables so that nothing is repeated: books sit in one table and authors in another, and a book's row carries only the author's number (author_id). To see a book's title next to its author's name you join the two tables on that number.

SELECT b.title, a.name
FROM books AS b
JOIN authors AS a ON a.id = b.author_id;
JOIN ... ON says which row pairs with which: those where the book's author_id equals the author's id. b and a are short names given to the tables; when both tables have a column of the same name, these say which one you mean.

A plain JOIN brings only the rows that have a partner: an author with no books does not appear in the result. To see them too you write LEFT JOIN: every row of the left table stays, and those without a partner get NULL on the right side. That is how "every author and their number of books, zero included" is solved.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.

  1. 01

    Join tables

    SQL query

  2. 02

    Nobody left out

    SQL query

If you would rather not wait for the order, every problem is open without locks: Problem list