SQL · Module 15 · Lesson 51 min read

Project: Answering Business Questions (Data Analysis)

The brief

The product team asks for insights from the practice database to plan new app features. Write one clean, commented query per question, and turn each result into a one-sentence insight.

The 10 questions

  1. How many users are active (have at least one expense), and what share is that of all users?
  2. What is the average monthly spending per active user?
  3. Which categories (including sub-categories rolled up to their parent) take the biggest share?
  4. Which weekday do people spend the most on?
  5. Who is close to (or over) their monthly budget in November?
  6. How fast is total spending growing month over month?
  7. What does a "typical" expense look like (median vs mean, P90)?
  8. Which tags appear together most often?
  9. Which users have unpaid bills due within 7 days of 24 Nov 2026, and can their account balances cover them?
  10. Which referrer brought in the highest-spending users?

How to work like an analyst

  • Clarify definitions first ("active", "month", "spending" = expenses only?)
  • Build queries with CTEs, step by step, checking row counts as you go
  • Watch out for NULLs, fan-out in joins, and integer division
  • Present results clearly: round numbers, label columns, sort meaningfully
  • Write the insight: "Groceries is the largest category at 24% of spending — a grocery budget template would help most users."

Deliverable

A SQL file (or a saved notebook) with each question, query and insight — plus one chart-ready query for a dashboard.

Practice with public datasets

After this, load a real CSV dataset (Kaggle, Nepal Open Data, government data portals) with COPY and answer 20 questions of your own.

Example

SQL
-- Q1. Active users and share
SELECT count(DISTINCT e.user_id)                                           AS active_users,
       (SELECT count(*) FROM users)                                        AS all_users,
       round(100.0 * count(DISTINCT e.user_id) / (SELECT count(*) FROM users), 1) AS pct_active
FROM expenses e;                                     -- 4 | 6 | 66.7

-- Q2. Average monthly spending per active user
WITH per_user_month AS (
  SELECT user_id, date_trunc('month', spent_on) AS m, sum(amount) AS total
  FROM expenses GROUP BY 1, 2
)
SELECT round(avg(total), 2) AS avg_monthly_spend FROM per_user_month;   -- 6384.17

-- Q3. Share by MAIN category (sub-categories rolled up to their parent)
SELECT COALESCE(p.name, c.name) AS main_category,
       sum(e.amount) AS total,
       round(100.0 * sum(e.amount) / sum(sum(e.amount)) OVER (), 1) AS pct
FROM expenses e
JOIN categories c ON c.id = e.category_id
LEFT JOIN categories p ON p.id = c.parent_id
GROUP BY 1 ORDER BY total DESC;
-- Food 33.1% | Transport 21.7% | Shopping 19.4% | Entertainment 15.5% | Bills 10.2%

-- Q4. Spending by weekday
SELECT to_char(spent_on, 'FMDay') AS weekday, count(*) AS n, sum(amount) AS total
FROM expenses GROUP BY 1, EXTRACT(isodow FROM spent_on)
ORDER BY total DESC LIMIT 3;

-- Q5. Budget status in November
SELECT u.name, u.monthly_budget, sum(e.amount) AS spent,
       round(100.0 * sum(e.amount) / u.monthly_budget) AS pct
FROM users u JOIN expenses e ON e.user_id = u.id
WHERE e.spent_on >= '2026-11-01' AND e.spent_on < '2026-12-01'
GROUP BY u.id ORDER BY pct DESC;

-- Q7. Typical expense
SELECT round(avg(amount)) AS mean,
       percentile_cont(0.5) WITHIN GROUP (ORDER BY amount) AS median,
       percentile_cont(0.9) WITHIN GROUP (ORDER BY amount) AS p90
FROM expenses;                                       -- mean 2471 | median 1750 | p90 5200

-- Q8. Tags that appear together (pairs)
SELECT t1 AS tag_a, t2 AS tag_b, count(*) AS together
FROM expenses e, unnest(e.tags) t1, unnest(e.tags) t2
WHERE t1 < t2
GROUP BY 1, 2 ORDER BY together DESC LIMIT 3;        -- essential + monthly 6 …

-- Q9. Bills due within 7 days (from 24 Nov 2026) vs available balance
SELECT u.name, sum(b.amount) AS due_soon,
       (SELECT COALESCE(sum(balance), 0) FROM accounts a WHERE a.user_id = u.id) AS balance,
       (SELECT COALESCE(sum(balance), 0) FROM accounts a WHERE a.user_id = u.id) >= sum(b.amount) AS can_pay
FROM bills b JOIN users u ON u.id = b.user_id
WHERE NOT b.paid AND b.due_date BETWEEN DATE '2026-11-24' AND DATE '2026-11-24' + 7
GROUP BY u.id ORDER BY u.name;

-- Q10. Which referrer brought the highest-spending users?
SELECT r.name AS referrer, count(DISTINCT u.id) AS referred_users, COALESCE(sum(e.amount), 0) AS their_spending
FROM users r
JOIN users u ON u.referred_by = r.id
LEFT JOIN expenses e ON e.user_id = u.id
GROUP BY r.name ORDER BY their_spending DESC;        -- Asha: Ravi + Sita = 46700