LAG(expr, offset, default) is a window function that returns the value offset rows before the current row. It is an essential pattern for period-over-period and year-over-year calculations.
LAG(revenue) OVER ( PARTITION BY company_name -- Do not take the preceding row across companies ORDER BY quarter -- Define the “one row earlier” position by quarter ) -- Default offset=1, default=NULL
revenue / prev_revenue causes a division-by-zero error. NULLIF(prev_revenue, 0) returns NULL when the value is 0, so division by NULL produces NULL (not an error) and is handled safely.Using quarterly revenue from three companies, calculate the previous-period revenue (prev_revenue) and period-over-period growth rate (growth_rate_pct, rounded to one decimal place). Return company_name, quarter, revenue, prev_revenue, growth_rate_pct, sorted by company_name ascending and then quarter ascending. Q1 has NULL for both prev_revenue and growth_rate_pct.
| company_name | quarter | revenue |
|---|---|---|
| AlphaTech | 2023Q1 | 300 |
| AlphaTech | 2023Q2 | 360 |
| AlphaTech | 2023Q3 | 280 |
| AlphaTech | 2023Q4 | 400 |
| BetaSoft | 2023Q1 | 250 |
| BetaSoft | 2023Q2 | 220 |
| BetaSoft | 2023Q3 | 290 |
| BetaSoft | 2023Q4 | 310 |
| GammaSys | 2023Q1 | 120 |
| GammaSys | 2023Q2 | 150 |
| GammaSys | 2023Q3 | 170 |
| GammaSys | 2023Q4 | 160 |
※ Revenue unit: JPY 100 million
Expected output (company_name ascending → quarter ascending):
| company_name | quarter | revenue | prev_revenue | growth_rate_pct |
|---|---|---|---|---|
| AlphaTech | 2023Q1 | 300 | NULL | NULL |
| AlphaTech | 2023Q2 | 360 | 300 | 20.0 |
| AlphaTech | 2023Q3 | 280 | 360 | -22.2 |
| AlphaTech | 2023Q4 | 400 | 280 | 42.9 |
| BetaSoft | 2023Q1 | 250 | NULL | NULL |
| BetaSoft | 2023Q2 | 220 | 250 | -12.0 |
| BetaSoft | 2023Q3 | 290 | 220 | 31.8 |
| BetaSoft | 2023Q4 | 310 | 290 | 6.9 |
| GammaSys | 2023Q1 | 120 | NULL | NULL |
| GammaSys | 2023Q2 | 150 | 120 | 25.0 |
| GammaSys | 2023Q3 | 170 | 150 | 13.3 |
| GammaSys | 2023Q4 | 160 | 170 | -5.9 |
AlphaTech Q3 (-22.2%) is the notable drop, followed by a sharp recovery in Q4 (+42.9%). BetaSoft’s Q2 (-12.0%) is another negative-growth warning.
- Read every model answer, explanation and table-transition visualization across all 48 advanced sets (240 questions)
- One-time purchase — no subscription. Future advanced sets are included
- Background, problem and expected output stay free
RANK(), DENSE_RANK(), and ROW_NUMBER() are all window functions that return ranks, but they handle ties differently.
| Function | When two rows tie for second | Next rank |
|---|---|---|
ROW_NUMBER() | 2, 3 (pseudo-unique assignment) | 4 |
RANK() | 2, 2 (tie) | 4 (skips a number) |
DENSE_RANK() | 2, 2 (tie) | 3 (consecutive) |
WITH ranked AS ( SELECT *, RANK() OVER (PARTITION BY category ORDER BY annual_revenue DESC) AS rnk FROM product_revenue ) SELECT * FROM ranked WHERE rnk <= 2; -- Top rank 2 or better in each category, including ties
From the competitor revenue data for each category, assign the within-category sales rank (rank_in_category) and return only companies ranked 2 or better in each category, including ties. Return category, company_name, annual_revenue, rank_in_category, sorted by category ascending and then rank_in_category ascending.
| company_name | category | annual_revenue |
|---|---|---|
| AlphaTech | Cloud | 4200 |
| AlphaTech | Security | 1800 |
| AlphaTech | AI/ML | 950 |
| BetaSoft | Cloud | 2800 |
| BetaSoft | AI/ML | 1500 |
| BetaSoft | Data Analytics | 800 |
| GammaSys | Security | 900 |
| GammaSys | AI/ML | 650 |
| GammaSys | Data Analytics | 420 |
| DeltaNet | Cloud | 1200 |
| DeltaNet | Data Analytics | 600 |
※ annual_revenue unit: JPY 100 million
Expected output (category ascending → rank_in_category ascending):
| category | company_name | annual_revenue | rank_in_category |
|---|---|---|---|
| AI/ML | BetaSoft | 1500 | 1 |
| AI/ML | AlphaTech | 950 | 2 |
| Cloud | AlphaTech | 4200 | 1 |
| Cloud | BetaSoft | 2800 | 2 |
| Security | AlphaTech | 1800 | 1 |
| Security | GammaSys | 900 | 2 |
| Data Analytics | BetaSoft | 800 | 1 |
| Data Analytics | DeltaNet | 600 | 2 |
AlphaTech ranks first in Cloud and Security. BetaSoft ranks first in AI/ML and Data Analytics, showing clear category specialization.
- Read every model answer, explanation and table-transition visualization across all 48 advanced sets (240 questions)
- One-time purchase — no subscription. Future advanced sets are included
- Background, problem and expected output stay free
Multiple CTEs can be defined as comma-separated entries in a WITH clause, and a preceding CTE can be referenced by a later CTE. This decomposes complex aggregation into named, readable stages.
WITH step1 AS ( SELECT ..., CASE WHEN col >= 30 THEN 3 ELSE 1 END AS score FROM source_table ), step2 AS ( -- step1 can be referenced here SELECT *, score_a + score_b AS total FROM step1 ) SELECT * FROM step2;
CASE WHEN ... END means ELSE NULL. In scoring logic, always specify ELSE the minimum score.From KPI data for six competitors, score revenue scale, growth rate, and NPS from 1 to 3 points, then calculate the total (maximum 9 points) and overall grade (S/A/B/C). Return company_name, revenue_score, growth_score, nps_pts, total_score, grade, sorted by total_score descending and then company_name ascending.
Scoring rules: revenue_score (revenue_bn ≥30→3, ≥10→2, else 1) | growth_score (growth_pct ≥25→3, ≥10→2, else 1) | nps_pts (nps_score ≥65→3, ≥50→2, else 1) | grade (total ≥8→S, ≥6→A, ≥4→B, else C)
| company_name | revenue_bn | growth_pct | nps_score |
|---|---|---|---|
| AlphaTech | 42.0 | 18.5 | 72 |
| BetaSoft | 28.0 | 12.3 | 58 |
| GammaSys | 15.0 | 31.2 | 65 |
| DeltaNet | 5.0 | 8.1 | 48 |
| EpsilonSys | 3.0 | -2.4 | 41 |
| ZetaCloud | 2.0 | 5.6 | 55 |
※ revenue_bn: revenue (billions of JPY) / growth_pct: year-over-year growth (%) / nps_score: customer NPS
Expected output (total_score descending → company_name ascending):
| company_name | revenue_score | growth_score | nps_pts | total_score | grade |
|---|---|---|---|---|---|
| AlphaTech | 3 | 2 | 3 | 8 | S |
| GammaSys | 2 | 3 | 3 | 8 | S |
| BetaSoft | 3 | 2 | 2 | 7 | A |
| ZetaCloud | 1 | 1 | 2 | 4 | B |
| DeltaNet | 1 | 1 | 1 | 3 | C |
| EpsilonSys | 1 | 1 | 1 | 3 | C |
GammaSys has only mid-sized revenue, but its 31.2% growth and NPS 65 give it the same top S grade as AlphaTech. BetaSoft has large revenue, but average growth and NPS leave it at A.
- Read every model answer, explanation and table-transition visualization across all 48 advanced sets (240 questions)
- One-time purchase — no subscription. Future advanced sets are included
- Background, problem and expected output stay free
Conditional aggregation uses the SUM(CASE WHEN ... END) pattern to convert long-format (row-oriented) data into wide-format (column-oriented) data. PostgreSQL has no dedicated PIVOT syntax, so this is the standard implementation.
SELECT company_name, SUM(CASE WHEN quarter = 'Q1' THEN revenue ELSE 0 END) AS q1_rev, SUM(CASE WHEN quarter = 'Q2' THEN revenue ELSE 0 END) AS q2_rev FROM quarterly_sales GROUP BY company_name;
ELSE 0 adds 0 for non-matching rows. With ELSE NULL, SUM ignores NULL, so the result is the same, but AVG and COUNT behave differently. ELSE 0 is the conventional choice for pivot aggregation.Using quarterly revenue for three companies in 2024, pivot Q1–Q4 revenue into columns, then calculate the annual total (annual_total) and the within-year Q1→Q4 growth rate (q4_vs_q1_pct, rounded to one decimal place). Return company_name, q1_rev, q2_rev, q3_rev, q4_rev, annual_total, q4_vs_q1_pct, sorted by annual_total descending.
| company_name | year | quarter | revenue |
|---|---|---|---|
| AlphaTech | 2024 | Q1 | 340 |
| AlphaTech | 2024 | Q2 | 410 |
| AlphaTech | 2024 | Q3 | 320 |
| AlphaTech | 2024 | Q4 | 450 |
| BetaSoft | 2024 | Q1 | 260 |
| BetaSoft | 2024 | Q2 | 240 |
| BetaSoft | 2024 | Q3 | 310 |
| BetaSoft | 2024 | Q4 | 340 |
| GammaSys | 2024 | Q1 | 130 |
| GammaSys | 2024 | Q2 | 165 |
| GammaSys | 2024 | Q3 | 185 |
| GammaSys | 2024 | Q4 | 170 |
※ Revenue unit: JPY 100 million / the table is assumed to contain multiple years, so filter with WHERE year = 2024
Expected output (annual_total descending):
| company_name | q1_rev | q2_rev | q3_rev | q4_rev | annual_total | q4_vs_q1_pct |
|---|---|---|---|---|---|---|
| AlphaTech | 340 | 410 | 320 | 450 | 1520 | 32.4 |
| BetaSoft | 260 | 240 | 310 | 340 | 1150 | 30.8 |
| GammaSys | 130 | 165 | 185 | 170 | 650 | 30.8 |
All three companies grow by roughly 30% from Q1 to Q4. AlphaTech has a temporary Q3 dip (320) but reaches +32.4% for the year. GammaSys has a small Q4 pullback but still grows +30.8% for the year.
- Read every model answer, explanation and table-transition visualization across all 48 advanced sets (240 questions)
- One-time purchase — no subscription. Future advanced sets are included
- Background, problem and expected output stay free
PERCENT_RANK() returns each row’s relative rank as a percentile from 0.0 to 1.0. The formula is (rank - 1) / (total_rows - 1).
| Formula | Example with 5 rows (ORDER BY revenue ASC) |
|---|---|
(rank-1) / (5-1) | Minimum revenue=0.0 / middle=0.5 / maximum revenue=1.0 |
PERCENT_RANK() OVER ( PARTITION BY year -- Independent rank space for each year ORDER BY revenue_bn -- Ascending: smallest→0.0, largest→1.0 ) * 100 -- Convert to a percentage (0–100)
From two years of revenue data for five companies, calculate relative revenue position within each year (pct_rank, percentage rounded to one decimal place) and the year-over-year position change (pct_rank_change). Return company_name, year, revenue_bn, pct_rank, prev_pct_rank, pct_rank_change, sorted by year ascending and then pct_rank descending.
| company_name | year | revenue_bn |
|---|---|---|
| AlphaTech | 2022 | 35.0 |
| BetaSoft | 2022 | 26.0 |
| GammaSys | 2022 | 18.0 |
| DeltaNet | 2022 | 12.0 |
| EpsilonSys | 2022 | 7.0 |
| AlphaTech | 2023 | 42.0 |
| BetaSoft | 2023 | 28.0 |
| GammaSys | 2023 | 15.0 |
| DeltaNet | 2023 | 16.0 |
| EpsilonSys | 2023 | 9.0 |
※ revenue_bn: revenue (billions of JPY) / from 2022 to 2023: GammaSys decreased to 15.0 and DeltaNet increased to 16.0
Expected output (year ascending → pct_rank descending):
| company_name | year | revenue_bn | pct_rank | prev_pct_rank | pct_rank_change |
|---|---|---|---|---|---|
| AlphaTech | 2022 | 35.0 | 100.0 | NULL | NULL |
| BetaSoft | 2022 | 26.0 | 75.0 | NULL | NULL |
| GammaSys | 2022 | 18.0 | 50.0 | NULL | NULL |
| DeltaNet | 2022 | 12.0 | 25.0 | NULL | NULL |
| EpsilonSys | 2022 | 7.0 | 0.0 | NULL | NULL |
| AlphaTech | 2023 | 42.0 | 100.0 | 100.0 | 0.0 |
| BetaSoft | 2023 | 28.0 | 75.0 | 75.0 | 0.0 |
| DeltaNet | 2023 | 16.0 | 50.0 | 25.0 | 25.0 |
| GammaSys | 2023 | 15.0 | 25.0 | 50.0 | -25.0 |
| EpsilonSys | 2023 | 9.0 | 0.0 | 0.0 | 0.0 |
DeltaNet rises the most, +25.0 points, as revenue grows from 12.0 to 16.0 and overtakes GammaSys. GammaSys falls the most, −25.0 points, as revenue drops from 18.0 to 15.0.
- Read every model answer, explanation and table-transition visualization across all 48 advanced sets (240 questions)
- One-time purchase — no subscription. Future advanced sets are included
- Background, problem and expected output stay free