basics06Chapter 6 of 6

And now: try it

You know the words now. The rest comes from writing queries.

The course

Short levels in the Byte Shop, starting with your first SELECT. Every answer is checked by its result.

To the course

The playground

The whole Byte Shop, no task and no grading. Copy one of these joins, paste it there and change it.

left-join.sqlEvery customer with their orders, also those without
SELECT c.name, o.id, o.status
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id;
n-m.sqlWho bought which product: n:m through orders
SELECT c.name, p.name
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
JOIN products AS p ON p.id = o.product_id;
Open the playground

In short

  1. 01

    A database holds tables. A row is one thing, a column one property.

  2. 02

    The primary key tells rows apart. A foreign key points at another table's primary key.

  3. 03

    Cardinality says how many belong to how many: 1:1, 1:n, n:m. For n:m you need a table in between.

  4. 04

    An ER diagram draws tables as boxes and keys as lines with crow's feet.

  5. 05

    INNER JOIN keeps matches, LEFT and RIGHT keep one side, FULL OUTER keeps both, CROSS pairs everything.