Defining & Implementing
Metrics in SQL
Learn to precisely define business metrics and translate them into reliable SQL queries that power dashboards and decision-making.
What Is a Metric?
A metric is a quantifiable measurement used to track and assess the status of a specific process, behavior, or outcome. In data analytics, metrics answer questions like: "How many?", "How much?", and "At what rate?"
Why Precise Definitions Matter
Two analysts asked "What is our churn rate?" can produce completely different numbers if they define "churn" differently. Precise metric definitions prevent confusion and ensure everyone trusts the data.
Anatomy of a Metric Definition
Every well-defined metric has four components. Think of it as a contract between the business and the database:
The Metric-to-SQL Translation Rule
Core Aggregation Functions
SQL provides a handful of aggregation functions that form the building blocks of every metric:
| Function | Purpose | Example Metric |
|---|---|---|
| COUNT(*) | Total rows | Total orders |
| COUNT(DISTINCT x) | Unique values | Unique customers |
| SUM(x) | Total of a column | Total revenue |
| AVG(x) | Mean of a column | Avg order value |
| MAX(x) / MIN(x) | Extremes | Peak daily sales |
COUNT vs COUNT DISTINCT
This is the most common source of metric confusion. The difference is whether you count every row or only unique values:
Step-by-Step: Metric to SQL
Example 1: Monthly Active Users
Example 2: Average Order Value
Example 3: Conversion Rate
Guarding Against Division by Zero
Ratio metrics like conversion rate require dividing one count by another. If the denominator is zero, your query will error or return unexpected results.
purchasers / total_visitors crashes when total_visitors = 0.purchasers / NULLIF(total_visitors, 0) returns NULL instead of an error.WHERE vs HAVING
Both filter data, but at different stages of query execution. Using the wrong one changes your metric's meaning.
Conditional Metrics with CASE WHEN
Many real-world metrics require conditional counting: count only rows that meet a specific condition within a broader grouping.
Common Mistakes
Metric Reference Table
| Metric | SQL Pattern | Key Clause |
|---|---|---|
| Total Count | COUNT(*) | GROUP BY |
| Unique Count | COUNT(DISTINCT col) | GROUP BY |
| Sum | SUM(col) | GROUP BY |
| Average | AVG(col) | GROUP BY |
| Ratio / Rate | \( \frac{\text{agg}}{\text{NULLIF}(\text{agg}, 0)} \) | CASE WHEN |
| Conditional Count | COUNT(CASE WHEN ...) | CASE WHEN |
| Filtered Groups | agg + HAVING | HAVING |
Question 1 of 5
You want to count the number of unique customers who placed an order. Which SQL expression is correct?
Question 2 of 5
You need to show only product categories where the average price exceeds $50. Which clause should you use?
Question 3 of 5
What does NULLIF(total_visitors, 0) return when total_visitors equals 0?
Question 4 of 5
To compute the percentage of orders paid by credit card safely in a single grouped query, which pattern is most appropriate?
SUM(*). Option D multiplies by 100 but never divides by the total, so it gives a raw count times 100, not a percentage.Question 5 of 5
Analyst A defines "Monthly Revenue" as: SUM(amount) from orders placed in the month.
Analyst B defines "Monthly Revenue" as: SUM(amount) from orders placed in the month WHERE status = 'completed'.
In January, there were 200 orders totaling $50,000. Of those, 180 had status 'completed' totaling $45,000, and 20 had status 'refunded' totaling $5,000.
What is the difference in their January revenue figures?