SQL interview question: Combine Two Tables With UNION
Merge Active and Inactive Users
The product team needs a combined list of both active and recently deactivated users for an engagement campaign.
Combine all users from both tables. Add a 'status' column indicating 'active' or 'inactive'.
Return user_id, name, email, and status. Order by user_id.
Skills: UNION and adding literal columns
Tables
active_users 3 rows
- user_id integer PK
- name text
- email text
| user_id | name | |
|---|---|---|
| 1 | Alice | [email protected] |
| 2 | Bob | [email protected] |
| 3 | Charlie | [email protected] |
inactive_users 2 rows
- user_id integer PK
- name text
- email text
| user_id | name | |
|---|---|---|
| 4 | Carol | [email protected] |
| 5 | Dave | [email protected] |