Case Study: Tyler Pernes
Case Study: Tyler Pernes
REP ORTIN G IN S QL
Tyler Pernes
Learning & Development Consultant
What is reporting?
REPORTING IN SQL
What is reporting?
REPORTING IN SQL
What is reporting?
REPORTING IN SQL
What is reporting?
REPORTING IN SQL
What is reporting?
REPORTING IN SQL
SQL and reporting
REPORTING IN SQL
Course goals
Create real-world reports
REPORTING IN SQL
Case study
REPORTING IN SQL
Case study
REPORTING IN SQL
Case study
REPORTING IN SQL
What is a dashboard?
REPORTING IN SQL
What is a base report?
SELECT
country,
SUM(wins) AS wins
FROM game_log
GROUP BY country
ORDER BY wins DESC
LIMIT 3;
REPORTING IN SQL
Your dashboard
REPORTING IN SQL
Querying time!
REP ORTIN G IN S QL
The Olympics
dataset
REP ORTIN G IN S QL
Tyler Pernes
Learning & Development Consultant
Entity-Relationship (E:R) diagram
Visual representation of database structure
Identi es relationships
REPORTING IN SQL
Olympics dataset E:R diagram
REPORTING IN SQL
Olympics dataset E:R diagram
REPORTING IN SQL
Olympics dataset E:R diagram
SELECT *
FROM countries AS c1
JOIN country_stats AS c2
ON c1.id = c2.country_id;
REPORTING IN SQL
Tables and elds
REPORTING IN SQL
Tables and elds
REPORTING IN SQL
Tables and elds
REPORTING IN SQL
Tables and elds
REPORTING IN SQL
Reference for queries
REPORTING IN SQL
Reference for queries
REPORTING IN SQL
Reference for queries
SELECT
region,
SUM(pop_in_millions) AS pop_in_millions
FROM countries AS c1
JOIN country_stats AS c2
ON c1.id = c2.country_id
GROUP BY region;
REPORTING IN SQL
Practice time!
REP ORTIN G IN S QL
Exploring our data
REP ORTIN G IN S QL
Tyler Pernes
Learning & Development Consultant
Exploring with the console
REPORTING IN SQL
Exploring with the console
May show an inaccurate picture
REPORTING IN SQL
Exploring with the console
No insight into distributions
REPORTING IN SQL
Exploring with queries
SELECT DISTINCT region
FROM countries;
+-------------------------+
| region |
|-------------------------|
| WESTERN EUROPE |
| null |
| C.W. IF IND. STATES |
| OCEANIA |
| NEAR EAST |
| SUB-SAHARAN AFRICA |
+-------------------------+
REPORTING IN SQL
Exploring with queries
SELECT region
FROM countries
GROUP BY region;
+-------------------------+
| region |
|-------------------------|
| WESTERN EUROPE |
| null |
| C.W. IF IND. STATES |
| OCEANIA |
| NEAR EAST |
| SUB-SAHARAN AFRICA |
+-------------------------+
REPORTING IN SQL
Field-level aggregations
SELECT region, COUNT(*) AS row_num
FROM countries
GROUP BY region
ORDER BY row_num DESC;
+-------------------------+---------+
| region | row_num |
|-------------------------|---------|
| SUB-SAHARAN AFRICA | 49 |
| LATIN AMER. & CARIB | 38 |
| ASIA (EX. NEAR EAST) | 26 |
| WESTERN EUROPE | 23 |
| OCEANIA | 15 |
| EASTERN EUROPE | 15 |
+-------------------------+---------+
REPORTING IN SQL
Field-level aggregations
SELECT revenue_source, SUM(revenue) AS revenue
FROM orders
GROUP BY revenue_source
ORDER BY revenue DESC;
+----------------+----------+
| revenue_source | revenue |
|----------------|----------|
| Olympics | 122000 |
| NFL | 80500 |
| MLB | 300 |
| NBA | 220 |
| NCAAF | 120 |
| NCAAB | 90 |
+----------------+----------+
REPORTING IN SQL
Table-level aggregations
SELECT COUNT(*)
FROM country_stats;
+---------+
| count |
|---------|
| 3451 |
+---------+
REPORTING IN SQL
Query validation
REPORTING IN SQL
Query validation
QUERY: ORIGINAL TABLE:
SELECT SUM(rev) AS revenue SELECT SUM(rev) AS revenue
FROM FROM orders;
(SELECT country, SUM(rev) AS rev
FROM orders AS o
+------------+
JOIN countries AS c
| revenue |
ON o.country_id = c.id
|------------|
GROUP BY country);
| 500.00 |
+------------+
+------------+
| revenue | Our Query (Left) = $50
|------------|
| 50.00 | Original Table (Right) = $500
+------------+
Lost 90% of revenue from JOIN
REPORTING IN SQL
Query validation
QUERY: ORIGINAL TABLE:
SELECT SUM(rev) AS revenue SELECT SUM(rev) AS revenue
FROM FROM orders;
(SELECT country, SUM(rev) AS rev
FROM orders AS o
+------------+
JOIN country_stats AS cs
| revenue |
ON o.country_id = cs.country_id
|------------|
GROUP BY country);
| 500.00 |
+------------+
+------------+
| revenue | Our Query (Left) = $5,000
|------------|
| 5,000.00 | Original Table (Right) = $500
+------------+
10x duplication from JOIN
REPORTING IN SQL
Let's explore!
REP ORTIN G IN S QL