// case studies · real world impact
Explore how companies solve business‑critical problems with SQL – full schema, solution, and insight.
// SELECT A CASE STUDY
Calculate total revenue by month to track sales trends and identify seasonal patterns.
Google's finance team is putting together the quarterly board deck and needs a clean month-by-month revenue trend: total revenue, transaction count, and average transaction value for each calendar month, so they can spot seasonal patterns (like a Q4 spike) and flag any month that looks anomalous before the numbers go in front of leadership. The query needs to handle months with very few transactions gracefully, since a couple of early months in the dataset are sparse.
Transactions with transaction_id, transaction_amount, transaction_date.
CREATE TABLE transactions (
transaction_id INTEGER PRIMARY KEY,
transaction_amount DECIMAL(10,2),
transaction_date DATE
);SELECT
STRFTIME('%Y-%m', transaction_date) as month,
COUNT(transaction_id) as transaction_count,
ROUND(SUM(transaction_amount), 2) as total_revenue,
ROUND(AVG(transaction_amount), 2) as average_transaction_value,
MIN(transaction_amount) as min_transaction,
MAX(transaction_amount) as max_transaction
FROM transactions
GROUP BY STRFTIME('%Y-%m', transaction_date)
ORDER BY month ASC;Step-by-step Solution:
Use STRFTIME('%Y-%m', transaction_date) to extract year-month from the transaction_date. This groups dates into months (e.g., 2024-01, 2024-02).
COUNT(transaction_id) counts the number of transactions per month.
SUM(transaction_amount) calculates total revenue for each month.
AVG(transaction_amount) shows average transaction value per month.
MIN(transaction_amount) and MAX(transaction_amount) show the smallest and largest transaction each month.
GROUP BY STRFTIME('%Y-%m', transaction_date) aggregates all transactions by month.
ORDER BY month ASC to show months in chronological order. This reveals revenue trends, seasonality, and helps forecast future revenue.