Total items ordered for each order

This technical problem involves writing a SQL query to find the total number of items ordered for each order in a database. The database consists of two tables, "orders" and "order_items ". The "orders" table includes the order_id for each order, while the "order_items" table includes the order_id and the number of items ordered for that particular order. The expected output is a table that lists the order_id and the total number of items ordered for each order.

Problem

Given two tables, "orders" and "order_items", write a SQL query to find the total number of items ordered for each order.
orders:
+------------+
| order_id   |
+------------+
| 1          |
| 2          |
| 3          |
+------------+
order_items:
+------------+-------------+
| order_id   | item_count  |
+------------+-------------+
| 1          | 3           |
| 2          | 1           |
| 3          | 2           |
+------------+-------------+
Example output:
+------------+-------------+
| order_id   | total_items |
+------------+-------------+
| 1          | 3           |
| 2          | 1           |
| 3          | 2           |
+------------+-------------+

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT order_id, SUM(item_count) AS total_items
FROM order_items
GROUP BY order_id;

/*

The solution above is optimal because it uses the GROUP BY clause to group the rows by order_id and then uses the SUM function to sum the item_count for each group.

*/

A.I. Evaluation of the Solution

The candidate's solution is correct and uses an optimal approach.

Evaluated at: 2022-11-11 12:15:27