SQL Query to Find Users with Multiple Orders

This technical problem involves writing a SQL query to find users with multiple orders. The example input and output are provided.

Problem

You are given two tables:
Table 1: Users
id | name
1 | John
2 | Jane
3 | Smith
Table 2: Orders
id | user_id | order_date
1 | 1 | 2017-01-01
2 | 2 | 2017-02-01
3 | 2 | 2017-03-01
4 | 3 | 2017-04-01
Write a SQL query that returns the names of all users who have placed at least 2 orders.
Example input:
Users:
id | name
1 | John
2 | Jane
3 | Smith
Orders:
id | user_id | order_date
1 | 1 | 2017-01-01
2 | 2 | 2017-02-01
3 | 2 | 2017-03-01
4 | 3 | 2017-04-01
Example output:
id | name
2 | Jane

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT u.id, u.name
FROM Users u
JOIN Orders o ON u.id = o.user_id
GROUP BY u.id, u.name
HAVING COUNT(o.id) >= 2;

A.I. Evaluation of the Solution

The solution is correct and demonstrates a level of completeness. The approach is straightforward and easy to follow.

Evaluated at: 2022-11-21 10:16:04