Skip to content

Commit 0ff9a30

Browse files
committed
Add group_by and join exercises
1 parent 466306e commit 0ff9a30

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

group_by.sql

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- We can use "AS" to relabel a field inside a particular query.
2+
-- This not only makes the report look nicer by displaying a more useful
3+
-- header name, but it makes it easier to reference a field elsewhere
4+
-- in the query.
5+
6+
-- "invoices" table
7+
8+
-- A list of the top 5 countries by number of invoices
9+
SELECT billing_country, COUNT(*) AS invoice_count
10+
FROM invoices
11+
GROUP BY billing_country
12+
ORDER BY invoice_count DESC
13+
LIMIT 5;
14+
15+
-- A list of the top 8 countries by gross/total invoice size
16+
SELECT billing_country, SUM(total) AS gross_sales
17+
FROM invoices
18+
GROUP BY billing_country
19+
ORDER BY gross_sales DESC
20+
LIMIT 8;
21+
22+
-- A list of the top 10 countries by average invoice size
23+
SELECT billing_country, AVG(total) AS avg_invoice_size
24+
FROM invoices
25+
GROUP BY billing_country
26+
ORDER BY avg_invoice_size DESC
27+
LIMIT 10;
28+
29+
-- Sales volume and receipts by year
30+
-- See: http://www.sqlite.org/lang_datefunc.html
31+
-- "STRFTIME" means "string format time" and is used to format the output of
32+
-- dates, times, and timestamps.
33+
-- See: http://cl.ly/image/0C1m372r233t
34+
SELECT STRFTIME('%Y', invoice_date) AS year,
35+
COUNT(*) AS invoice_count,
36+
SUM(total) AS invoice_total
37+
FROM invoices
38+
GROUP BY year
39+
ORDER BY year ASC;
40+
41+
-- Sales volume and receipts by year and month
42+
-- You can group by multiple fields
43+
-- You can order by multiple fields (here, year first then month)
44+
-- See: http://cl.ly/image/2e2H3w052H2o
45+
46+
SELECT STRFTIME('%Y', invoice_date) AS year,
47+
STRFTIME('%m', invoice_date) AS month,
48+
COUNT(*) AS invoice_count,
49+
SUM(total) AS invoice_total
50+
FROM invoices
51+
GROUP BY year, month
52+
ORDER BY year ASC, month ASC;
53+
54+
-- A list of the top 5 US states by number of invoices
55+
-- Hint: You'll need to filter the results with WHERE billing_country = 'USA'
56+
57+
-- A list of the top 5 US states by gross sales
58+
59+
-- A list of the top 5 US states by average invoice size
60+
61+
-- A list of the top 10 US cities by number of invoices
62+
63+
-- A list of the top 10 US cities by gross sales
64+
65+
-- A list of the top 10 US cities by average invoice size
66+
67+
-- A list of the top 3 cities in California by number of invoices
68+
-- Hint: You'll need a WHERE clause filtering by both billing_country and billing_state
69+
70+
-- A list of the top 3 cities in California by gross sales
71+
72+
-- A list of the top 3 cities in California by average invoice size
73+
74+
75+
-- "customers" table
76+
-- Remember: run ".schema customers" to see what fields (columns) the customers table contains.
77+
78+
-- A list of the top 3 countries by total number of customers
79+
80+
-- A list of the top 7 cities (anywhere) by total number of customers

join.sql

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)