SQL interview questions

SQL Advanced level Question 1

Q. Write an SQL query to find the users who have logged in more than 3 times in the last 30 days, along with the total number of logins, the number of unique actions they performed (excluding 'logged_in' and 'logged_out'), and the most recent action they performed, ordered by the number of logins in descending order.

Sample Data in the activity_logs Table:

 

log_iduser_idactiontarget_tabletarget_idtimestamp
11logged_inNULLNULL2025-01-14 08:30:00
21vieweddashboardNULL2025-01-14 08:35:00
31createdreport1022025-01-14 09:00:00
41logged_outNULLNULL2025-01-14 09:15:00
52logged_inNULLNULL2025-01-14 09:30:00
62editedreport1032025-01-14 09:45:00
72logged_outNULLNULL2025-01-14 10:00:00

Explanation:

  • user_id represents the user who performed the action.
  • action specifies what action was taken, like logging in, creating, or editing.
  • target_table and target_id capture which part of the system the action relates to (optional fields).
  • timestamp records the exact time of the activity.
SELECT 
    u.user_id, 
    COUNT(*) AS total_logins,
    COUNT(DISTINCT CASE 
                    WHEN a.action NOT IN ('logged_in', 'logged_out') THEN a.action 
                    ELSE NULL 
                  END) AS unique_actions_count,
    MAX(a.timestamp) AS most_recent_action_time,
    (SELECT a.action 
     FROM activity_logs a 
     WHERE a.user_id = u.user_id
     ORDER BY a.timestamp DESC
     LIMIT 1) AS most_recent_action
FROM 
    activity_logs a
JOIN 
    (SELECT DISTINCT user_id 
     FROM activity_logs 
     WHERE action = 'logged_in' 
     AND timestamp >= NOW() - INTERVAL 30 DAY) u ON a.user_id = u.user_id
WHERE 
    a.action = 'logged_in'
    AND a.timestamp >= NOW() - INTERVAL 30 DAY
GROUP BY 
    u.user_id
HAVING 
    total_logins > 3
ORDER BY 
    total_logins DESC;

Explanation of the query:

  1. Subquery for distinct user_id:

    • The subquery inside the JOIN (SELECT DISTINCT user_id FROM activity_logs WHERE action = 'logged_in' AND timestamp >= NOW() - INTERVAL 30 DAY) fetches the users who have logged in during the last 30 days.
  2. Counting total logins:

    • COUNT(*) AS total_logins: Counts the total number of ‘logged_in’ actions for each user in the last 30 days.
  3. Counting unique actions:

    • COUNT(DISTINCT CASE WHEN a.action NOT IN ('logged_in', 'logged_out') THEN a.action ELSE NULL END) AS unique_actions_count: Counts the number of unique actions performed by the user, excluding ‘logged_in’ and ‘logged_out’.
  4. Most recent action time:

    • MAX(a.timestamp) AS most_recent_action_time: Finds the most recent timestamp of a ‘logged_in’ action for each user.
  5. Most recent action performed:

    • The subquery (SELECT a.action FROM activity_logs a WHERE a.user_id = u.user_id ORDER BY a.timestamp DESC LIMIT 1) retrieves the most recent action performed by the user.
  6. Filtering users who logged in more than 3 times:

    • HAVING total_logins > 3: Filters the result set to include only users who have logged in more than 3 times in the last 30 days.
  7. Ordering the result:

    • ORDER BY total_logins DESC: Orders the results in descending order of the total number of logins.
Comment form