SQL Query to Sum Orders by Product ID

This technical problem deals with writing a SQL query that returns the sum of all orders for each product_id. The given table has a schema with three columns: order_id, product_ id, and quantity. An example input and output is also provided.

Problem

Given a table with the following schema:
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
product_id INTEGER,
quantity INTEGER
);
Write a SQL query that returns the sum of all orders for each product_id.
Example input:
order_id | product_id | quantity
1 | 1 | 10
2 | 1 | 20
3 | 2 | 15
4 | 2 | 5
Example output:
product_id | sum
1 | 30
2 | 20

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT product_id, SUM(quantity)
FROM orders
GROUP BY 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-07 02:15:35