SQL Problems

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.
```SELECT customer_id, SUM(quantity * price) AS revenue
FROM orders
GROUP BY customer_id;

/*

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.

*/```

A.I. Evaluation of the Solution

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.

Evaluated at: 2022-11-04 01:35:32

Community solutions:

Check these solutions from our community and artificial intelligence:
This solution is complete and solves the problem. The approach is straightforward and easy to follow.