0
← Exercises/
Window Functions•2 of 14
Window FunctionsAdvanced

Top 2 Sales Reps per Region with DENSE_RANK()

Scenario

Sales VP is allocating president club bonuses. They need to find the top 2 sales reps by total bookings in each geographic territory, properly handling ties.

Task
Rank sales representatives within each `region` based on `total_bookings` in descending order. Return only the reps who rank in the top 2 (`regional_rank <= 2`). Columns to return: 1. `rep_name` 2. `region` 3. `total_bookings` 4. `regional_rank` Order results by `region` ascending, then by `regional_rank` ascending, then by `rep_name` ascending.
Expected Output Format

Filtered table with rep_name, region, total_bookings, and regional_rank containing only rank 1 and 2 reps per region.

Input Tables & Sample Data0 tables
Loading database schema...
Need a hint?
query.sql|Local Sandbox
Loading SQL editor...
Click "Run" or press ⌘ + Enter to execute your query

About the “Top 2 Sales Reps per Region with DENSE_RANK()” SQL Problem

Business Scenario

Sales VP is allocating president club bonuses. They need to find the top 2 sales reps by total bookings in each geographic territory, properly handling ties.

Expected Output & Criteria

Filtered table with rep_name, region, total_bookings, and regional_rank containing only rank 1 and 2 reps per region.

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

This challenge executes directly against the following relational schema in your browser:

CREATE TABLE sales_reps (
        rep_id INT PRIMARY KEY,
        rep_name VARCHAR(80) NOT NULL,
        region VARCHAR(30) NOT NULL,
        total_bookings NUMERIC(10, 2) NOT NULL
      );

Target SQL Clauses & Operators

DENSE_RANKOVERPARTITION BY

Want more Window Functions practice?

Explore the full syllabus of 14 exercises, syntax guides, and interview pitfalls.

View Window Functions Track→