Joining movie ratings with customer data

Ra quyết định dựa trên dữ liệu với SQL

Irene Ortner

Data Scientist at Applied Statistics

JOIN

Ra quyết định dựa trên dữ liệu với 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.
Ra quyết định dựa trên dữ liệu với SQL

Giving a table a name

SELECT *
FROM customers AS c
WHERE c.customer_id = 1;
Ra quyết định dựa trên dữ liệu với 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 |
Ra quyết định dựa trên dữ liệu với 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` |
Ra quyết định dựa trên dữ liệu với 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;
Ra quyết định dựa trên dữ liệu với SQL

Let's practice!

Ra quyết định dựa trên dữ liệu với SQL

Preparing Video For Download...