NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

Description

Table: products

+--------------+--------------+
| Column Name  | Type         |
+--------------+--------------+
| product_id   | int          |
| product_name | varchar(255) |
| product_line | varchar(255) |
| price        | decimal      |
+--------------+--------------+

product_id is the primary key for this table.

Table: orders

+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id    | int  |
| product_id  | int  |
| quantity    | int  |
+-------------+------+

order_id is the primary key for this table. product_id is a foreign key referencing products.

Write a solution to calculate the total revenue for each product line, where revenue is defined as price * quantity. Return product_line and total_revenue, sorted by total_revenue in descending order.

The result format is shown below.

Example

Input:

products table:
+------------+--------------+--------------+---------+
| product_id | product_name | product_line | price   |
+------------+--------------+--------------+---------+
| 1          | RTX 4090     | GeForce      | 1599.00 |
| 2          | RTX 4080     | GeForce      | 1199.00 |
| 3          | A100         | Data Center  | 10000.00|
| 4          | Quadro RTX   | Professional | 2500.00 |
+------------+--------------+--------------+---------+

orders table:
+----------+------------+----------+
| order_id | product_id | quantity |
+----------+------------+----------+
| 1        | 1          | 2        |
| 2        | 2          | 3        |
| 3        | 3          | 1        |
| 4        | 4          | 2        |
| 5        | 1          | 1        |
+----------+------------+----------+

Output:

+--------------+---------------+
| product_line | total_revenue |
+--------------+---------------+
| Data Center  | 10000.00      |
| GeForce      | 8394.00       |
| Professional | 5000.00       |
+--------------+---------------+

Explanation:
GeForce: RTX 4090 sold 2 + 1 = 3 units at 1599, giving 4797; RTX 4080 sold 3 units at 1199, giving 3597. Total = 8394. Data Center: A100 sold 1 at 10000, giving 10000. Professional: Quadro RTX sold 2 at 2500, giving 5000.


Accepted
7/7
Acceptance Rate100.0%
Test Cases
Input
-
Output
-
Expected
-