0% found this document useful (0 votes)
28 views

Case Study: Tyler Pernes

This document discusses reporting in SQL. It provides an overview of SQL and reporting, introduces a case study using an Olympics dataset, and explores the dataset through example queries. The goals are to create real-world reports using real data and to overcome challenges. Tables and fields in the Olympics dataset are described. Examples are provided of exploring the data through the console and writing queries to better understand the data distributions and relationships. The importance of validating queries is also discussed.

Uploaded by

hiếu phan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Case Study: Tyler Pernes

This document discusses reporting in SQL. It provides an overview of SQL and reporting, introduces a case study using an Olympics dataset, and explores the dataset through example queries. The goals are to create real-world reports using real data and to overcome challenges. Tables and fields in the Olympics dataset are described. Examples are provided of exploring the data through the console and writing queries to better understand the data distributions and relationships. The importance of validating queries is also discussed.

Uploaded by

hiếu phan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Case study

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

Use real-world data

Overcome real-world obstacles

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

Lays out tables and elds

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

preview of table: summer_games


+-------------+-----------------------------------------+--------+
| sport | event | bronze |
|-------------|-----------------------------------------|--------|
| Gymnastics | Gymnastics Men's Individual All-Around | null |
| Gymnastics | Gymnastics Men's Floor Exercise | null |
| Gymnastics | Gymnastics Men's parallel Bars | null |
+-------------+-----------------------------------------+--------+

REPORTING IN SQL
Exploring with the console
No insight into distributions

preview of table: clients


+--------------------+
| country_of_client |
|--------------------|
| United States |
| United States |
| Mexico |
| United States |
| Canada |
| Canada |
+--------------------+

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

You might also like