Number of items locked by each user

This technical problem involves writing a query to find the number of items that are locked by each user in a given table. The output should show the user ID and the number of items locked for each user.

Problem

Given a table of locked items, write a query to find the number of items that are locked by each user.
Input:
Table: locked_items
+----+------------+-------------+
| id | user_id    | item_id     |
+----+------------+-------------+
| 1  | 1          | 10          |
| 2  | 2          | 20          |
| 3  | 3          | 30          |
| 4  | 1          | 40          |
| 5  | 2          | 50          |
| 6  | 3          | 60          |
+----+------------+-------------+
Output:
+------------+-------------+
| user_id    | num_locked  |
+------------+-------------+
| 1          | 2           |
| 2          | 2           |
| 3          | 2           |
+------------+-------------+

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT user_id, COUNT(*) AS num_locked
FROM locked_items
GROUP BY user_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-20 06:16:15