You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Query the top 10 products with the highest sales volume
SELECTp.prod_name, SUM(s.quantity_sold) AS total_quantity
FROMsh.sales s
JOINsh.products p ONs.prod_id=p.prod_idGROUP BYp.prod_nameORDER BY total_quantity DESC
FETCH FIRST 10 ROWS ONLY;
Customer Analysis
Query the total consumption amount of each customer
SELECTc.cust_last_name, c.cust_first_name, SUM(s.amount_sold) AS total_spent
FROMsh.customers c
JOINsh.sales s ONc.cust_id=s.cust_idGROUP BYc.cust_last_name, c.cust_first_nameORDER BY total_spent DESC;
Query the geographical distribution of customers
SELECTc.country_id, COUNT(*) AS num_customers
FROMsh.customers c
GROUP BYc.country_idORDER BY num_customers DESC;
Channel Analysis
Query the average order amount for each channel
SELECTch.channel_desc, AVG(s.amount_sold) AS avg_order_amount
FROMsh.sales s
JOINsh.channels ch ONs.channel_id=ch.channel_idGROUP BYch.channel_descORDER BY avg_order_amount DESC;
Query the order quantity of each channel
SELECTch.channel_desc, COUNT(s.amount_sold) AS num_orders
FROMsh.sales s
JOINsh.channels ch ONs.channel_id=ch.channel_idGROUP BYch.channel_descORDER BY num_orders DESC;
Promotion Analysis
Query the total sales for each promotion
SELECTp.promo_name, SUM(s.amount_sold) AS total_sales
FROMsh.sales s
JOINsh.promotions p ONs.promo_id=p.promo_idGROUP BYp.promo_nameORDER BY total_sales DESC;
Check the sales of products participating in a specific promotion (TV program sponsorship)
SELECTp.prod_name, SUM(s.amount_sold) AS total_sales
FROMsh.sales s
JOINsh.products p ONs.prod_id=p.prod_idWHEREs.promo_idIN (SELECT promo_id FROMsh.promotionsWHERE promo_subcategory ='TV program sponsorship')
GROUP BYp.prod_nameORDER BY total_sales DESC;
Time analysis
Query the sales volume for each month
SELECTt.calendar_month_desc, SUM(s.amount_sold) AS total_sales
FROMsh.sales s
JOINsh.times t ONs.time_id=t.time_idGROUP BYt.calendar_month_descORDER BYt.calendar_month_desc;
Year-over-year (YoY) growth query
Query annual sales and its year-on-year growth rate
SELECTt.calendar_year,
SUM(s.amount_sold) AS total_sales,
LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_year) AS prev_year_sales,
ROUND((SUM(s.amount_sold) - LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_year)) / LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_year) *100, 2) AS yoy_growth_rate
FROMsh.sales s
JOINsh.times t ONs.time_id=t.time_idGROUP BYt.calendar_yearORDER BYt.calendar_year;
Month-on-month (quarter-on-quarter growth) query
Query the sales volume and month-on-month growth rate for each quarter
SELECTt.calendar_quarter_desc,
SUM(s.amount_sold) AS total_sales,
LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_quarter_desc) AS prev_quarter_sales,
ROUND((SUM(s.amount_sold) - LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_quarter_desc)) / LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_quarter_desc) *100, 2) AS qoq_growth_rate
FROMsh.sales s
JOINsh.times t ONs.time_id=t.time_idGROUP BYt.calendar_quarter_descORDER BYt.calendar_quarter_desc;
Query the sales volume and month-on-month growth rate of each month
SELECTt.calendar_month_desc,
SUM(s.amount_sold) AS total_sales,
LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_month_desc) AS prev_month_sales,
ROUND((SUM(s.amount_sold) - LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_month_desc)) / LAG(SUM(s.amount_sold)) OVER (ORDER BYt.calendar_month_desc) *100, 2) AS mom_growth_rate
FROMsh.sales s
JOINsh.times t ONs.time_id=t.time_idGROUP BYt.calendar_month_descORDER BYt.calendar_month_desc;
Comprehensive Example
Check the impact of price, promotion and channel on sales
SELECTp.prod_name, ch.channel_desc, pr.promo_name, SUM(s.amount_sold) AS total_sales
FROMsh.sales s
JOINsh.products p ONs.prod_id=p.prod_idJOINsh.channels ch ONs.channel_id=ch.channel_idJOINsh.promotions pr ONs.promo_id=pr.promo_idGROUP BYp.prod_name, ch.channel_desc, pr.promo_nameORDER BY total_sales DESC;
Query the top 10 products with the highest sales and their year-on-year growth
WITH product_sales AS (
SELECTp.prod_name, t.calendar_year, SUM(s.amount_sold) AS total_sales
FROMsh.sales s
JOINsh.products p ONs.prod_id=p.prod_idJOINsh.times t ONs.time_id=t.time_idGROUP BYp.prod_name, t.calendar_year
)
SELECT prod_name, calendar_year, total_sales,
LAG(total_sales) OVER (PARTITION BY prod_name ORDER BY calendar_year) AS prev_year_sales,
ROUND((total_sales - LAG(total_sales) OVER (PARTITION BY prod_name ORDER BY calendar_year)) / LAG(total_sales) OVER (PARTITION BY prod_name ORDER BY calendar_year) *100, 2) AS yoy_growth_rate
FROM product_sales
WHERE prod_name IN (
SELECT prod_name
FROM product_sales
WHERE calendar_year = (SELECTMAX(calendar_year) FROM product_sales)
ORDER BY total_sales DESC
FETCH FIRST 10 ROWS ONLY
)
ORDER BY prod_name, calendar_year;
Query the quarterly sales and month-on-month growth of each channel
SELECTch.channel_desc, t.calendar_quarter_desc,
SUM(s.amount_sold) AS total_sales,
LAG(SUM(s.amount_sold)) OVER (PARTITION BY ch.channel_descORDER BYt.calendar_quarter_desc) AS prev_quarter_sales,
ROUND((SUM(s.amount_sold) - LAG(SUM(s.amount_sold)) OVER (PARTITION BY ch.channel_descORDER BYt.calendar_quarter_desc)) / LAG(SUM(s.amount_sold)) OVER (PARTITION BY ch.channel_descORDER BYt.calendar_quarter_desc) *100, 2) AS qoq_growth_rate
FROMsh.sales s
JOINsh.channels ch ONs.channel_id=ch.channel_idJOINsh.times t ONs.time_id=t.time_idGROUP BYch.channel_desc, t.calendar_quarter_descORDER BYch.channel_desc, t.calendar_quarter_desc;