Region Revenue Share

MediumSQLDatabaseSorting

Description

Write a SQL query to return each region with its share of total revenue as a percentage rounded to 2 decimal places, ordered by region ascending. **Output columns (in order):** region, pct

Table:sales
ColumnType
idINT
regionTEXT
amountINT

Examples

Input:CREATE TABLE sales (id INT, region TEXT, amount INT); INSERT INTO sales VALUES (1,'East',25),(2,'West',75);
Output:East|25.0 West|75.0
Explanation:

Each region’s revenue is divided by the overall revenue and scaled to a percentage, by region.

Input:CREATE TABLE sales (id INT, region TEXT, amount INT); INSERT INTO sales VALUES (1,'Solo',40);
Output:Solo|100.0
Explanation:

Each region’s revenue is divided by the overall revenue and scaled to a percentage, by region.

Input:CREATE TABLE sales (id INT, region TEXT, amount INT); INSERT INTO sales VALUES (1,'A',10),(2,'B',10),(3,'C',10),(4,'D',10);
Output:A|25.0 B|25.0 C|25.0 D|25.0
Explanation:

Each region’s revenue is divided by the overall revenue and scaled to a percentage, by region.

Constraints

  • Use standard SQL (SQLite).

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.