0
← Exercises/
Window Functions•8 of 14
Window FunctionsAdvancedPRO

Driver Earnings Quartile Segmentation with NTILE(4)

Scenario

Fleet operations segments active gig couriers into earnings quartiles to benchmark hourly incentive schemes and tier bonuses.

Task
Assign each driver in `driver_earnings` to an earnings quartile (1 to 4) using `NTILE(4)` ordered by `weekly_payout` descending. Return: 1. `driver_id` 2. `driver_name` 3. `weekly_payout` 4. `earnings_quartile`: `NTILE(4) OVER (ORDER BY weekly_payout DESC)` Order results by `weekly_payout` descending, then by `driver_id` ascending.
Expected Output Format

Drivers segmented into 4 earnings quartiles (1 = top 25%, 4 = bottom 25%).

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 “Driver Earnings Quartile Segmentation with NTILE(4)” SQL Problem

Business Scenario

Fleet operations segments active gig couriers into earnings quartiles to benchmark hourly incentive schemes and tier bonuses.

Expected Output & Criteria

Drivers segmented into 4 earnings quartiles (1 = top 25%, 4 = bottom 25%).

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

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

CREATE TABLE driver_earnings (
        driver_id INT PRIMARY KEY,
        driver_name VARCHAR(80) NOT NULL,
        weekly_payout NUMERIC(10, 2) NOT NULL
      );

Target SQL Clauses & Operators

NTILEOVERORDER BY

Want more Window Functions practice?

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

View Window Functions Track→