You are working as a Data Engineer for a social media platform. The product team wants to understand how popular each post is based on user engagement. One of the simplest engagement metrics is the number of likes a post receives. The data is stored in a table that records each like event. Each row represents a user liking a specific post. Your task is to aggregate this data to compute the total number of likes per post. Schema Summary: likes table: - like_id (INT) - post_id (INT) - user_id (INT) - liked_at (TIMESTAMP) Requirements: Count the total number of likes for each post. Return columns: post_id and total_likes. Group the results by post_id. Sort the output by total_likes in descending order. Hints: Use the COUNT() function to count the number of rows per post. A common mistake is grouping by unnecessary columns, which can lead to incorrect counts. Also, remember that each row represents one like, so counting rows directly gives the total likes.
Table Setup (SQL Schema)
CREATE TABLE likes (like_id INT, post_id INT, user_id INT, liked_at TIMESTAMP);
INSERT INTO likes (like_id, post_id, user_id, liked_at) VALUES
(1, 101, 1, '2023-01-01 10:00:00'),
(2, 101, 2, '2023-01-01 10:05:00'),
(3, 102, 1, '2023-01-01 11:00:00'),
(4, 101, 3, '2023-01-02 09:00:00'),
(5, 103, 2, '2023-01-02 10:00:00'),
(6, 102, 3, '2023-01-02 11:00:00'),
(7, 103, 1, '2023-01-03 12:00:00');