SQL Quest › SQL Interview Questions › Aggregation & Grouping

Pivot: Order Status by Country

MediumFreeQuerying BasicsAggregation & GroupingConditional Logic

Amazon's Global Operations team needs an at-a-glance view of how marketplace orders break down by status across shipping countries. Build a pivot table from the orders table: for each country, show columns completed, pending, cancelled, shipped (count of orders in each status), and total_orders. Sort by total_orders descending, then country ascending as a tie-breaker. Pivoting with conditional aggregation shows up in Amazon BIE and Data Analyst interviews constantly — standard SQL has no native PIVOT, so you build it with CASE.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect". Free accounts get 10 free challenge solves, and this can be one of them.

Schema

orders

order_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

Expected output: USA | 5 | 3 | 2 | 1 | 11

Hint

SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed for each status value. GROUP BY country.

Concepts

SELECT GROUP BY CASE Aggregation SUM Pivot / Conditional Aggregation

Practise the topic: SQL practice questions · GROUP BY exercises · CASE WHEN practice

Read the concept: How to find duplicates in SQL

In these company practice sets

Amazon · Databricks · Ramp · Shopify · Snowflake · Wise · Microsoft

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Genre Financial ReportMedium · FreeDepartment Compensation ReportMedium · FreeConsistent Director AnalysisMedium · FreeGenre Box Office ReportMedium · FreeHighest Total Salary Budget DepartmentMedium · FreeTitle Social Survival AnalysisMedium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test