Support Ticket Status by User
For every user who has raised a support ticket, show their total ticket count and how many are still open.
- Joins
- Aggregation
- CASE expressions
- Sorting
Challenge brief
Understand the request
Customer Health Team is building a customer health dashboard that flags users with unresolved support issues alongside their total ticket history.
Join users to support_tickets and use conditional SUM(CASE WHEN) to split Open vs total tickets.
Return
- user_id
- country
- total_tickets
- open_tickets
Constraints
- Return users who have raised at least one ticket
- Count open tickets conditionally within each user total
- Resolve equal totals by user ID
- Show higher total ticket counts first; resolve ties by user ID
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
subscriptions
subscription_idINTEGERuser_idINTEGERproduct_idINTEGERstart_dateDATEend_dateDATEpriceINTEGER
users
user_idINTEGERcountryTEXTsignup_dateDATE
support_tickets
ticket_idINTEGERuser_idINTEGERproduct_idINTEGERcreated_dateDATEstatusTEXT
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
This needs user country from users and ticket metrics from support_tickets. JOIN them, GROUP BY user. Use SUM(CASE WHEN status = 'Open' THEN 1 ELSE 0 END) to count only Open tickets per user.
Hint 2
FROM users u JOIN support_tickets st ON u.user_id = st.user_id. GROUP BY u.user_id, u.country. COUNT(st.ticket_id) AS total_tickets. SUM(CASE WHEN st.status = 'Open' THEN 1 ELSE 0 END) AS open_tickets.
Hint 3
Build question 24 from the required result grain: choose the driving table, add only the joins and filters needed for that grain, then apply aggregation and deterministic ordering.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT u.user_id, u.country, COUNT(st.ticket_id) AS total_tickets, SUM(CASE WHEN st.status = 'Open' THEN 1 ELSE 0 END) AS open_tickets FROM users u INNER JOIN support_tickets st ON u.user_id = st.user_id GROUP BY u.user_id, u.country ORDER BY total_tickets DESC, u.user_id;Why this works
SUM(CASE WHEN) is the conditional counting pattern — it sums 1 for Open rows and 0 for Closed rows within each user's group. Every user has exactly 1 ticket in this dataset. Users 2, 4, 7, 9, 11 have Open tickets.
Success check
12 users — all have 1 total ticket; 5 have 1 open ticket, 7 have 0
Expected result
Use this output to verify values, aliases, ordering, and row count.
| user_id | country | total_tickets | open_tickets |
|---|---|---|---|
| 1 | US | 1 | 0 |
| 2 | IN | 1 | 1 |
| 3 | US | 1 | 0 |
| 4 | UK | 1 | 1 |
| 5 | CA | 1 | 0 |
| 6 | US | 1 | 0 |
| 7 | IN | 1 | 1 |
| 8 | US | 1 | 0 |
| 9 | DE | 1 | 1 |
| 10 | FR | 1 | 0 |
Previewing 10 of 12 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Apple
Independent Apple-style product, retail, services, support, workforce, and device-usage SQL practice.
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Amazon
Independent Amazon-style e-commerce, warehouse, inventory, and customer analytics SQL practice.
Return to the complete interview preparation experience.