🔥🔥🔥 3 Solutions in MySQL | Beats 94.21% 🔥🔥🔥
1. LEFT JOIN with Runtime 532ms (Beats 94.21%):\n- Time complexity: O(NlogN)+O(N)\u2248O(NlogN),assuming indexing on player_id\n- Space complexity: O(M), where 
# 1. LEFT JOIN with Runtime 532ms (Beats 94.21%):\n- **Time complexity:** O(NlogN)+O(N)\u2248O(NlogN),assuming indexing on player_id\n- **Space complexity:** O(M), where M is the number of unique player_ids, for the CTE storage.\n\n# Code\n```mysql []\nWITH min_event_date_cte AS (\n    SELECT \n        player_id,\n        MIN(event_date) AS min_date\n    FROM  activity\n    GROUP BY player_id\n)\nSELECT \n    ROUND(\n        COALESCE(COUNT(b.player_id), 0) / COUNT(DISTINCT a.player_id), \n        2\n    ) AS fraction\nFROM activity a\nLEFT JOIN activity b \n\tON a.player_id = b.player_id \n\t\tAND DATEDIFF(b.event_date, a.event_date) = 1\nJOIN min_event_date_cte m \n\tON a.player_id = m.player_id \n\t\tAND a.event_date = m.min_date;\n```\n\n# 2. JOIN with Runtime 1061ms (Beats 35.78%):\n- **Time complexity:** O(NlogN)+O(N)\u2248O(NlogN)\n- **Space complexity:** O(M)\n\n# Code\n```mysql []\nWITH Min_Date_CTE AS\n(\n\tSELECT \n\t\tplayer_id,\n        MIN(event_date) AS event_date\n\tFROM activity\n\tGROUP BY player_id\n)\nSELECT \n\tROUND(COUNT(*) / (SELECT COUNT(*) FROM Min_Date_CTE), 2) AS fraction\nFROM \n\tMin_Date_CTE a JOIN Activity b \nON a.player_id = b.player_id\nWHERE DATEDIFF(b.event_date, a.event_date) = 1;\n```\n\n# 3. AGG() & Subquery with Runtime 583ms (Beats 76.89%):\n- **Time complexity:**  O(NlogG)\n- **Space complexity:** O(N)\n\n# Code\n```mysql []\nSELECT \n    ROUND(\n        SUM(CASE WHEN DATEDIFF(event_date, min_event_date) = 1 THEN 1 ELSE 0 END) \n        / COUNT(DISTINCT player_id),\n        2\n    ) AS fraction\nFROM (\n    SELECT \n        player_id,\n        event_date,\n        MIN(event_date) OVER (PARTITION BY player_id) AS min_event_date\n    FROM activity\n) AS activity_with_min_date\n```
['Database', 'MySQL']