SQL query to find users in more than one role

This technical problem deals with writing a SQL query to find all the users who are in more than one role. The user table, role table, and user role table are provided, and the expected output is also given.

Problem

Given a table of users and their roles, write a SQL query to find all the users who are in more than one role.
User table:
+----+-------+
| id | name  |
+----+-------+
| 1  | John  |
| 2  | Jane  |
| 3  | Dave  |
| 4  | Sarah |
+----+-------+
Role table:
+----+---------+
| id | name    |
+----+---------+
| 1  | Admin   |
| 2  | Manager |
| 3  | Analyst |
| 4  | User    |
+----+---------+
UserRole table:
+----+---------+----------+
| id | user_id | role_id  |
+----+---------+----------+
| 1  | 1       | 1        |
| 2  | 1       | 2        |
| 3  | 2       | 2        |
| 4  | 2       | 3        |
| 5  | 3       | 3        |
| 6  | 4       | 4        |
+---- +---------+----------+
Output:
+----+-------+
| id | name  |
+----+-------+
| 1  | John  |
| 2  | Jane  |
| 3  | Dave  |
+----+-------+

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT u.id, u.name
FROM User u
JOIN UserRole ur ON u.id = ur.user_id
GROUP BY u.id, u.name
HAVING COUNT(ur.role_id) > 1;

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The approach is straightforward and easy to follow.

Evaluated at: 2022-11-06 17:23:22