Top SQL Questions

Anne Smith

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) from customer_orders group by customer_id, product_id

A.I. Evaluation of the Solution

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

Evaluated at: 2022-11-04 00:44:35

Community solutions:

Check these solutions from our community and artificial intelligence:
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.