K / KASKAD

SQL · business questions

Extension of the existing SQL Operations project using the same 36 sales, 6 SKUs and 6 months. Suppliers are an additional synthetic dimension. Results below were executed in SQLite; no PostgreSQL-engine execution is claimed.

01

Business problem

Verify sales, profit, assortment and data quality.

02

My role

Independent case: analytical questions and verifiable criteria; prepared with AI assistance.

03

Analysis

JOIN, CTE, SUM OVER, DENSE_RANK, CASE, LAG/LEAD.

04

Solution

Eight reproducible SQL examples on existing data.

05

Result

Revenue RUB 1,715,700 reconciled independently; cumulative total matched; negative checks detected defects.

06

Evidence

SQL, source CSVs, output CSVs and execution record below.

Execution results JSON · Schema SQL · Methodology · Source data

JOIN

Sales → product → supplier

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

SELECT sp.supplier_name, p.category, COUNT(*) AS sale_lines,
 SUM(s.quantity*s.unit_price) AS revenue,
 SUM(s.quantity*p.unit_cost) AS cost_of_sales
FROM demo_sales s
JOIN demo_products p ON p.sku=s.sku
JOIN demo_suppliers sp ON sp.supplier_id=p.supplier_id
GROUP BY sp.supplier_name,p.category
ORDER BY revenue DESC,sp.supplier_name;
supplier_namecategorysale_linesrevenuecost_of_sales
Demo Tableware SupplierTableware12779850413850
Demo Coffee SupplierCoffee12650250386550
Demo Tea SupplierTea12285600153825

Analytical conclusion: Dimension joins use unique keys, avoiding row multiplication.

SQL · Result CSV
CTE

Auditable financial calculation

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

WITH lines AS (
 SELECT s.sale_id,s.quantity*s.unit_price AS revenue,
 s.quantity*p.unit_cost AS cost
 FROM demo_sales s JOIN demo_products p ON p.sku=s.sku
), totals AS (
 SELECT SUM(revenue) AS revenue,SUM(cost) AS cost_of_sales FROM lines
)
SELECT revenue,cost_of_sales,revenue-cost_of_sales AS gross_profit,
 ROUND(100.0*(revenue-cost_of_sales)/NULLIF(revenue,0),2) AS margin_pct
FROM totals;
revenuecost_of_salesgross_profitmargin_pct
171570095422576147544.38

Analytical conclusion: Gross profit is distinct from operating profit; the fixture has no overhead, tax or returns.

SQL · Result CSV
Window SUM

Cumulative revenue

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

WITH monthly AS (
 SELECT substr(sale_date,1,7) AS month,SUM(quantity*unit_price) AS revenue
 FROM demo_sales GROUP BY substr(sale_date,1,7)
)
SELECT month,revenue,SUM(revenue) OVER (
 ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_revenue FROM monthly ORDER BY month;
monthrevenuerunning_revenue
2026-01247200247200
2026-02263800511000
2026-03277100788100
2026-042957001083800
2026-053072001391000
2026-063247001715700

Analytical conclusion: The last running total equals total revenue, validating aggregation completeness.

SQL · Result CSV
DENSE_RANK

Product ranking

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

WITH sku_totals AS (
 SELECT p.sku,p.category,SUM(s.quantity*s.unit_price) AS revenue
 FROM demo_sales s JOIN demo_products p ON p.sku=s.sku
 GROUP BY p.sku,p.category
)
SELECT sku,category,revenue,
 DENSE_RANK() OVER (ORDER BY revenue DESC) AS overall_rank,
 DENSE_RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS category_rank
FROM sku_totals ORDER BY overall_rank,sku;
skucategoryrevenueoverall_rankcategory_rank
POT-01Tableware46800011
COF-01Coffee38295021
POT-02Tableware31185032
COF-02Coffee26730042
TEA-01Tea17490051
TEA-02Tea11070062

Analytical conclusion: Category and overall rankings answer different management questions.

SQL · Result CSV
GROUP BY

Category seasonality

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

SELECT substr(s.sale_date,1,7) AS month,p.category,
 SUM(s.quantity) AS units,SUM(s.quantity*s.unit_price) AS revenue
FROM demo_sales s JOIN demo_products p ON p.sku=s.sku
GROUP BY substr(s.sale_date,1,7),p.category ORDER BY month,category;
monthcategoryunitsrevenue
2026-01Coffee15093000
2026-01Tableware190111600
2026-01Tea21042600
2026-02Coffee16099150
2026-02Tableware200118950
2026-02Tea22545700
2026-03Coffee170105300
2026-03Tableware210126300
2026-03Tea22545500
2026-04Coffee180111450
2026-04Tableware220133650
2026-04Tea25050600
2026-05Coffee190117600
2026-05Tableware230141000
2026-05Tea24048600
2026-06Coffee200123750
2026-06Tableware240148350
2026-06Tea26052600

Analytical conclusion: Compare categories in matching periods with consistent revenue recognition.

SQL · Result CSV
CASE WHEN + windows

ABC and boundary handling

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

WITH sku_totals AS (
 SELECT sku,SUM(quantity*unit_price) AS revenue FROM demo_sales GROUP BY sku
), shares AS (
 SELECT sku,revenue,SUM(revenue) OVER () AS total_revenue,
 COALESCE(SUM(revenue) OVER (ORDER BY revenue DESC,sku
 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS revenue_before
 FROM sku_totals
)
SELECT sku,revenue,ROUND(100.0*revenue/NULLIF(total_revenue,0),2) AS share_pct,
 CASE WHEN total_revenue=0 THEN NULL
 WHEN revenue_before < total_revenue*0.8 THEN 'A'
 WHEN revenue_before < total_revenue*0.95 THEN 'B' ELSE 'C' END AS abc
FROM shares ORDER BY revenue DESC,sku;
skurevenueshare_pctabc
POT-0146800027.28A
COF-0138295022.32A
POT-0231185018.18A
COF-0226730015.58A
TEA-0117490010.19B
TEA-021107006.45B

Analytical conclusion: ABC alone does not determine replenishment; demand, lead time and safety stock are also needed.

SQL · Result CSV
LAG / LEAD

Month-over-month change

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

WITH monthly AS (
 SELECT substr(sale_date,1,7) AS month,SUM(quantity*unit_price) AS revenue
 FROM demo_sales GROUP BY substr(sale_date,1,7)
), neighbors AS (
 SELECT month,revenue,LAG(revenue) OVER (ORDER BY month) AS previous_revenue,
 LEAD(revenue) OVER (ORDER BY month) AS next_revenue FROM monthly
)
SELECT month,revenue,previous_revenue,next_revenue,
 revenue-previous_revenue AS change_amount,
 ROUND(100.0*(revenue-previous_revenue)/NULLIF(previous_revenue,0),2) AS change_pct
FROM neighbors ORDER BY month;
monthrevenueprevious_revenuenext_revenuechange_amountchange_pct
2026-01247200None263800NoneNone
2026-02263800247200277100166006.72
2026-03277100263800295700133005.04
2026-04295700277100307200186006.71
2026-05307200295700324700115003.89
2026-06324700307200None175005.7

Analytical conclusion: The first month has no predecessor; a zero denominator yields NULL, not infinite growth.

SQL · Result CSV
PostgreSQL-compatible SQL

Completeness and key quality

Input: existing sales and products; schema and synthetic supplier mapping are documented in the README.

SELECT 'orphan_sku' AS check_name,COUNT(*) AS issue_count
FROM demo_sales s LEFT JOIN demo_products p ON p.sku=s.sku WHERE p.sku IS NULL
UNION ALL SELECT 'invalid_amount',COUNT(*) FROM demo_sales
 WHERE quantity IS NULL OR quantity<0 OR unit_price IS NULL OR unit_price<0
UNION ALL SELECT 'missing_product_cost',COUNT(*) FROM demo_products
 WHERE unit_cost IS NULL OR unit_cost<0
UNION ALL SELECT 'duplicate_business_line',COUNT(*) FROM (
 SELECT sale_date,sku,quantity,unit_price FROM demo_sales
 GROUP BY sale_date,sku,quantity,unit_price HAVING COUNT(*)>1
) duplicates;
check_nameissue_count
orphan_sku0
invalid_amount0
missing_product_cost0
duplicate_business_line0

Analytical conclusion: Zero issues applies only to the four listed checks. A negative fixture verifies detection of orphan SKUs and negative amounts.

SQL · Result CSV