|
| 1 | +-- Joins (via the JOIN clause) are how we query data contained in multiple tables. |
| 2 | +-- There are two main types of relationships: |
| 3 | +-- 1. One-to-many |
| 4 | +-- 2. Many-to-many |
| 5 | + |
| 6 | +-- For example, a customer has many invoices, but each invoice belongs to one |
| 7 | +-- and only one customer. This is a one-to-many relationship. |
| 8 | + |
| 9 | +-- Think of some one-to-many relationships in every day life, e.g., |
| 10 | +-- |
| 11 | +-- 1. A car can get many parking tickets, but a parking ticket belongs to |
| 12 | +-- one and only one car. |
| 13 | +-- 2. Every state in the US has many cities, but every city in the US is in |
| 14 | +-- one and only one state. |
| 15 | +-- 3. Customers can have many orders, but an order belongs to a single customer |
| 16 | +-- 4. Customers can have multiple credit cards, but every credit card belong |
| 17 | +-- to a single customer. |
| 18 | +-- 5. Think of some of your own. |
| 19 | + |
| 20 | +-- An example of a many-to-many relationship might be events and people. |
| 21 | +-- I can attend many events and each event has many attendees. |
| 22 | +-- In our toy database, we're only dealing with one-to-many relationships. |
| 23 | + |
| 24 | +-- The 5 customers who most recently purchased something |
| 25 | +SELECT customers.id, customers.first_name, |
| 26 | + customers.last_name, customers.email, |
| 27 | + invoices.invoice_date, invoices.total |
| 28 | +FROM customers |
| 29 | +JOIN invoices |
| 30 | + ON (invoices.customer_id = customers.id) |
| 31 | +ORDER BY invoices.invoice_date DESC |
| 32 | +LIMIT 5; |
| 33 | + |
| 34 | +-- The top 10 customers by total number of orders |
| 35 | +SELECT customers.id, customers.first_name, |
| 36 | + customers.last_name, customers.email, |
| 37 | + COUNT(*) AS order_count |
| 38 | +FROM customers |
| 39 | +JOIN invoices |
| 40 | + ON (invoices.customer_id = customers.id) |
| 41 | +GROUP BY customers.id |
| 42 | +ORDER BY order_count DESC |
| 43 | +LIMIT 10; |
| 44 | + |
| 45 | +-- The top 10 customers by gross sales |
| 46 | +SELECT customers.id, customers.first_name, |
| 47 | + customers.last_name, customers.email, |
| 48 | + SUM(total) AS gross_sales |
| 49 | +FROM customers |
| 50 | +JOIN invoices |
| 51 | + ON (invoices.customer_id = customers.id) |
| 52 | +GROUP BY customers.id |
| 53 | +ORDER BY gross_sales DESC |
| 54 | +LIMIT 10; |
| 55 | + |
| 56 | +-- The top 5 genres by number of tracks (fill in the blanks) |
| 57 | +SELECT genres.id, genres.name, COUNT(*) AS track_count |
| 58 | +FROM genres |
| 59 | +JOIN tracks |
| 60 | + ON (tracks.genre_id = genres.id) |
| 61 | +GROUP BY genres.id |
| 62 | +ORDER BY track_count DESC |
| 63 | +LIMIT 5; |
| 64 | + |
| 65 | +-- The top 5 genres by total track length (in milliseconds) |
| 66 | + |
| 67 | +-- The top 5 genres by average track length (in milliseconds) |
| 68 | + |
| 69 | +-- The top 5 albums by total track length |
| 70 | +-- Hint: you'll need to JOIN the albums table and the tracks table |
| 71 | +-- Hint: the tracks table has an album_id field |
| 72 | + |
| 73 | +-- The top 5 albums by average track length |
| 74 | + |
| 75 | +-- The top 5 albums by total album price |
| 76 | +-- Hint: the "tracks" table has a unit_price field, so the "price" of an album |
| 77 | +-- is the sum of its tracks' unit_price fields. |
| 78 | + |
| 79 | +-- The 10 albums with the longest play-time |
| 80 | + |
| 81 | +-- The 10 highest-selling tracks of all time |
| 82 | +-- Hint: you'll need to join the tracks table and the invoice_lines table |
0 commit comments