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.
01Business problem
Verify sales, profit, assortment and data quality.
02My role
Independent case: analytical questions and verifiable criteria; prepared with AI assistance.
03Analysis
JOIN, CTE, SUM OVER, DENSE_RANK, CASE, LAG/LEAD.
04Solution
Eight reproducible SQL examples on existing data.
05Result
Revenue RUB 1,715,700 reconciled independently; cumulative total matched; negative checks detected defects.
06Evidence
SQL, source CSVs, output CSVs and execution record below.
Execution results JSON · Schema SQL · Methodology · Source data
JOINSales → 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;
Analytical conclusion: Dimension joins use unique keys, avoiding row multiplication.
SQL · Result CSVCTEAuditable 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;
Analytical conclusion: Gross profit is distinct from operating profit; the fixture has no overhead, tax or returns.
SQL · Result CSVWindow SUMCumulative 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;
Analytical conclusion: The last running total equals total revenue, validating aggregation completeness.
SQL · Result CSVDENSE_RANKProduct 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;
Analytical conclusion: Category and overall rankings answer different management questions.
SQL · Result CSVGROUP BYCategory 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;
Analytical conclusion: Compare categories in matching periods with consistent revenue recognition.
SQL · Result CSVCASE WHEN + windowsABC 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;
Analytical conclusion: ABC alone does not determine replenishment; demand, lead time and safety stock are also needed.
SQL · Result CSVLAG / LEADMonth-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;
Analytical conclusion: The first month has no predecessor; a zero denominator yields NULL, not infinite growth.
SQL · Result CSVPostgreSQL-compatible SQLCompleteness 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;
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