Get Total Revenue for Each Customer

This technical problem involves writing a SQL query that returns the total revenue for each customer. The table schema is provided, and an example input and output are also given.

Problem

Given a table with the following schema:
CREATE TABLE orders (
id INT NOT NULL,
customer_id INT,
product_id INT,
quantity INT,
price DECIMAL(10,2),
PRIMARY KEY (id)
);
Write a SQL query that returns the total revenue for each customer.
Example input:
id | customer_id | product_id | quantity | price
1 | 1 | 2 | 2 | 10.00
2 | 1 | 4 | 1 | 20.00
3 | 2 | 3 | 1 | 15.00
4 | 3 | 1 | 3 | 5.00
Example output:
customer_id | revenue
1 | 30.00
2 | 15.00
3 | 15.00

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by frankf
select customer_id, sum(price) as revenue
from orders
group by customer_id
order by customer_id asc

A.I. Evaluation of the Solution

This solution is complete and solves the problem. The approach is straightforward and easy to follow.

Evaluated at: 2023-01-21 05:20:22

Community solutions:

Check these solutions from our community and artificial intelligence:
The solution is optimal because it uses the GROUP BY clause to group the rows by customer_id and then uses the SUM function to sum the revenue for each customer.