Total number of transactions per user

The goal is to find the total number of transactions for each user using data from two tables, "users" and "transactions". The "users" table has columns for id and name , while the "transactions" table has columns for id, user_id, and amount. The expected output is a table with columns for id, name, and total_transactions.

Problem

Given two tables, "users" and "transactions", write a SQL query to find the total number of transactions for each user.
users:
id | name
1 | John
2 | Jane
3 | Joe
4 | Jill
transactions:
id | user_id | amount
1 | 1 | 100
2 | 1 | 50
3 | 2 | 75
4 | 3 | 25
5 | 3 | 50
6 | 4 | 150
Example output:
id | name | total_transactions
1 | John | 3
2 | Jane | 1
3 | Joe | 2
4 | Jill | 1

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT users.id, users.name, COUNT(transactions.id) AS total_transactions
FROM users
LEFT JOIN transactions ON users.id = transactions.user_id
GROUP BY users.id

/*

The solution above is optimal because it uses a LEFT JOIN to ensure that all users are included in the result, even if they have no transactions.

*/

A.I. Evaluation of the Solution

The candidate's solution is correct and uses a LEFT JOIN to ensure that all users are included in the result, even if they have no transactions.

Evaluated at: 2022-11-17 23:47:26