Mastering SQL: A Comprehensive Exercise on Grouping, Filtering, and Analyzing Data in MySQL #50
akash-coded
started this conversation in
Tasks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Creating an extensive and comprThis exercise will revolve around two databases:
classicmodelsandworld. It will focus on theGROUP BYclause, theHAVINGclause, the difference betweenHAVINGandWHERE, and the complex usage of aggregate functions in conjunction withGROUP BY. Each part of the exercise will include a scenario, tasks, theoretical insights, best practices, and cautions.Database Setup
Part 1: Understanding
GROUP BYand Aggregate FunctionsScenario
You are analyzing sales data in the
classicmodelsdatabase to gain insights into the performance of various products and offices.Tasks
Theory and Best Practices
SUM(),AVG(),COUNT(),MAX(), andMIN().Cautions
Part 2: Filtering with
HAVINGClauseScenario
You've noticed some inconsistencies in the
classicmodelsdatabase and need to identify offices that have unusually high average order values, possibly due to data entry errors or high-value orders.Tasks
Theory and Best Practices
WHEREfilters rows before grouping, whileHAVINGfilters groups after theGROUP BYhas been applied.HAVINGfor conditions that involve aggregated data.Cautions
HAVINGfor conditions that can be applied before grouping (useWHEREinstead).HAVINGcan only be used withGROUP BY.Part 3: Complex Aggregations and Grouping
Scenario
The
worlddatabase needs an analysis of demographic and geographic data to aid in a global marketing strategy.Tasks
Theory and Best Practices
SELECTstatements.Cautions
SELECTlist are included in theGROUP BYclause.Part 4: Advanced Scenario - Time Series Analysis
Scenario
You are tasked with analyzing sales trends over time in the
classicmodelsdatabase to identify growth patterns and seasonal effects.Tasks
Theory and Best Practices
LAGorLEADfunctions to compare current row values with previous or subsequent rows.Cautions
Deliverables
For each part of the exercise, provide:
Let's break down each part into detailed instructions, including explicit queries to write and SQL constructs to use. This will guide you through a series of increasingly complex scenarios designed to deepen your understanding of
GROUP BY,HAVING, aggregate functions, and their nuanced usage in MySQL.Part 1: Basic Grouping and Aggregation in
classicmodelsScenario: Sales Performance Analysis
Analyze the performance of product lines in terms of total sales and quantity sold.
Task 1.1: Total Sales by Product Line
SUM()aggregate function onquantityOrdered * priceEachfrom theorderdetailstable to calculate total sales.orderdetailswithproductsto get theproductLine.productLine.SELECT,FROM,JOIN,GROUP BY,SUM()Task 1.2: Total Quantity Sold by Product Line
SUM()aggregate function onquantityOrderedfrom theorderdetailstable.orderdetailswithproductsto accessproductLine.productLine.SELECT,FROM,JOIN,GROUP BY,SUM()Part 2: Advanced Grouping and Filtering with
HAVINGinclassicmodelsScenario: High-Value Transactions Analysis
Identify product lines that have unusually high average sales amounts, which may indicate premium products or data anomalies.
Task 2.1: Average Sale Amount by Product Line with Filtering
quantityOrdered * priceEach) for each product line.HAVINGclause to filter out product lines with an average sale amount below the threshold.productLine.SELECT,FROM,JOIN,GROUP BY,AVG(),HAVINGPart 3: Complex Aggregations in
worldScenario: Continent Demographics Analysis
Perform an analysis on continents to understand population and language diversity.
Task 3.1: Average Population and Total GDP by Continent
AVG()function for average population andSUM()for total GDP from thecountrytable.Continent.SELECT,FROM,GROUP BY,AVG(),SUM()Task 3.2: Countries with Multiple Official Languages
IsOfficialis 'T' from thecountrylanguagetable.HAVINGclause to filter countries with official languages greater than the specified count.CountryCodeand then join withcountryto get country names.SELECT,FROM,JOIN,GROUP BY,COUNT(),HAVINGPart 4: Time Series Analysis in
classicmodelsScenario: Sales Trends Analysis
Examine how sales have trended over time, looking for growth patterns or seasonal effects.
Task 4.1: Month-over-Month Sales Growth
orderDateto perform a monthly sales analysis.LAG()to compare current month sales to the previous month and calculate the growth percentage.productLine.SELECT,FROM,JOIN,GROUP BY, window functions (LAG()), date functions (YEAR(),MONTH())Task 4.2: Quarterly Sales Analysis
orderDate.officesto include office information in the analysis.SELECT,FROM,JOIN, `GROUPBY
, date functions (QUARTER()`), window functions/subqueries for comparisonFor each task, ensure to:
Beta Was this translation helpful? Give feedback.
All reactions