6. Combine Active and Archived Users
A platform stores user information in two separate tables:
- One for currently active users
- One for archived users
To generate a consolidated report, the company wants a unique list of all user IDs that have ever existed on the platform.
ActiveUsers
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā user_id ā int ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- user_id is the primary key.
ArchivedUsers
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā user_id ā int ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- user_id is the primary key.
- Some users may appear in both tables.
Write an SQL query to return all unique user_ids that appear in either ActiveUsers or ArchivedUsers. Return the result in any order.
Example 1:
Input:
ActiveUsers:
āāāāāāāāāāāā ā user_id ā ā āāāāāāāāāā⣠ā 1 ā āāāāāāāāāāāā ā 2 ā āāāāāāāāāāāā ā 3 ā āāāāāāāāāāāā
ArchivedUsers:
āāāāāāāāāāāā ā user_id ā ā āāāāāāāāāā⣠ā 3 ā āāāāāāāāāāāā ā 4 ā āāāāāāāāāāāā ā 5 ā āāāāāāāāāāāā
Output:
āāāāāāāāāāāā ā user_id ā ā āāāāāāāāāā⣠ā 1 ā āāāāāāāāāāāā ā 2 ā āāāāāāāāāāāā ā 3 ā āāāāāāāāāāāā ā 4 ā āāāāāāāāāāāā ā 5 ā āāāāāāāāāāāā
Explanation
User 3 appears in both tables but is shown only once in the result.
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.