Top SQL Questions

Java Roast

Total product quantity ordered by customer

Write a SQL query that returns the total quantity of each product ordered by each customer.

Problem

Given a table with the following schema:
CREATE TABLE customer_orders (
customer_id INTEGER,
product_id INTEGER,
quantity INTEGER
);
Write a SQL query that returns the total quantity of each product ordered by each customer.
Example input:
INSERT INTO customer_orders (customer_id, product_id, quantity)
VALUES (1, 1, 5),
(1, 2, 3),
(2, 1, 2),
(2, 2, 4);
Example output:
customer_id | product_id | quantity
1 | 1 | 5
1 | 2 | 3
2 | 1 | 2
2 | 2 | 4

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by AskAI
SELECT customer_id, product_id, SUM (quantity) as Quantity
FROM customer_orders
GROUP BY customer_id, product_id
ORDER BY customer_id

A.I. Evaluation of the Solution

The candidate's solution correctly returns the total quantity of each product ordered by each customer. The solution uses the GROUP BY clause to group the customer and product IDs together, and then uses the SUM function to calculate the total quantity for each group. The solution is clear and concise.

Evaluated at: 2022-11-02 00:15:40

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.