SQL Query to Find Total Number of Orders per Customer

To find the total number of orders for each customer, you can use a SQL query as follows: SELECT customer_id, COUNT(order_id) AS total_orders FROM customer_orders GROUP BY customer_id;

Problem

Given a table of data representing customer orders, write a SQL query to find the total number of orders for each customer.
Input:
customer_orders
| customer_id | order_id |
| ------------|----------|
| 1           | 1001     |
| 1           | 1002     |
| 2           | 1003     |
| 2           | 1004     |
| 3           | 1005     |
Output:
| customer_id | total_orders |
| ------------|--------------|
| 1           | 2            |
| 2           | 2            |
| 3           | 1            |

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT customer_id, COUNT(order_id) AS total_orders
FROM customer_orders
GROUP BY customer_id;

/*

The solution above is optimal because it uses the GROUP BY clause to group the customer_id column and then uses the COUNT function to count the number of orders for each customer.

*/

A.I. Evaluation of the Solution

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

Evaluated at: 2022-11-18 00:16:13