Joining movie ratings with customer data

Data-Driven Decision Making in SQL

Irene Ortner

Data Scientist at Applied Statistics

JOIN

Data-Driven Decision Making in SQL

LEFT JOIN

  • LEFT JOIN is an outer join.
  • Keep all rows of the left table, match with rows in the right table.
  • Use identifier to define which rows of two tables can be matched.
Data-Driven Decision Making in SQL

Giving a table a name

SELECT *
FROM customers AS c
WHERE c.customer_id = 1;
Data-Driven Decision Making in SQL

Tables for LEFT JOIN

Left table: renting_selected

| renting_id | customer_id | rating |
|------------|-------------|--------|
| 518        | 1           | `null` |
| 203        | 2           | 6      |
| 478        | 4           | 7      |
| 292        | 4           | 8      |
| 477        | 5           | `null` |
| 400        | 6           | `null` |

Right table: customers_selected

| customer_id | name               | gender |
|-------------|--------------------|--------|
| 1           | Robert Bohm        | male   |
| 2           | Wolfgang Ackermann | male   |
| 3           | Daniela Herzog     | female |
| 4           | Julia Jung         | female |
Data-Driven Decision Making in SQL

LEFT JOIN example

SELECT *
FROM renting_selected AS r
LEFT JOIN customers_selected AS c
ON r.customer_id = c.customer_id;
| renting_id | customer_id | rating | customer_id | name               | gender |
|------------|-------------|--------|-------------|--------------------|--------|
| 518        | 1           | `null` | 1           | Robert Bohm        | male   |
| 203        | 2           | 6      | 2           | Wolfgang Ackermann | male   |
| 478        | 4           | `null` | 4           | Julia Jung         | female |
| 292        | 4           | 8      | 4           | Julia Jung         | female |
| 477        | 5           | 7      | `null`      | `null`             | `null` |
Data-Driven Decision Making in SQL

More than one JOIN

SELECT m.title, 
       c.name
FROM renting AS r
LEFT JOIN movies AS m
ON r.movie_id = m.movie_id
LEFT JOIN customers AS c
ON r.customer_id = c.customer_id;
Data-Driven Decision Making in SQL

Let's practice!

Data-Driven Decision Making in SQL

Preparing Video For Download...