Python Central HubSign in

Data Analytics with Python / Phase 9: SQL for Data Analytics

SQL Mini Project (Build a KPI Dashboard Query Set)

Write a set of reusable KPI queries - DAU, revenue, cohorts, and top segments.

Goal

Create a mini “query pack” for a dashboard.

Assume tables:

  • users(user_id, created_at, country, plan)
  • orders(order_id, user_id, order_ts, amount)
  • events(user_id, event_ts, event_name)

1) Daily active users (DAU)

SQL
SELECT
  DATE(event_ts) AS day,
  COUNT(DISTINCT user_id) AS dau
FROM events
GROUP BY DATE(event_ts)
ORDER BY day;

2) Daily revenue

SQL
SELECT
  DATE(order_ts) AS day,
  SUM(amount) AS revenue
FROM orders
GROUP BY DATE(order_ts)
ORDER BY day;

3) Top countries by revenue

SQL
SELECT
  u.country,
  SUM(o.amount) AS revenue
FROM orders o
JOIN users u ON u.user_id = o.user_id
GROUP BY u.country
ORDER BY revenue DESC
LIMIT 10;

4) ARPU (avg revenue per user)

SQL
WITH revenue_by_user AS (
  SELECT user_id, SUM(amount) AS revenue
  FROM orders
  GROUP BY user_id
)
SELECT
  AVG(revenue) AS arpu
FROM revenue_by_user;

Deliverable

  • 5–10 queries you can reuse
  • Notes about assumptions (time zone, filters, returns)

How the query pack fits together

A "KPI dashboard" is really just several independent queries against the same handful of tables, each answering one question. Nothing here is new — you're combining GROUP BY, joins, DATE(), and CTEs from earlier pages into small, named building blocks.

diagram KPI query pack mermaid
Each dashboard number is its own small query against the same shared tables; a dashboard just runs all of them and displays the results side by side.

Check your understanding

quizCheck yourself
  1. How does the DAU query count active users per day?

    Show answer

    A — COUNT(DISTINCT user_id) grouped by DATE(event_ts) — A user with many events in a day should count once, so DISTINCT is needed.

  2. Which tables does the Top countries by revenue query join?

    Show answer

    A — orders and users — Revenue is in orders and country is in users, so it joins on user_id.

  3. How is ARPU computed in the lesson?

    Show answer

    A — A CTE sums revenue per user, then AVG of that revenue — revenue_by_user gives one row per user and AVG(revenue) over it is the average revenue per user.

  4. Why should each KPI query run standalone with no hidden state?

    Show answer

    A — So it can run in a fresh connection and be trusted later — A query pack that runs top to bottom with no leftover tables or assumed dates is reliable for teammates.

🧪 Try It Yourself

Exercise 1 – DAU query

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Top countries by revenue

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – ARPU via a CTE

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Next

This wraps up the SQL for Data Analytics phase. Bring these query patterns with you into the project phases, where you'll pull data from files instead of a database — the filtering, grouping, and joining logic transfers directly to pandas.

Was this page helpful?

Let us know how we did