easy

Most Popular Post Category by Likes

Most Popular Post Category by Likes

You are a data analyst at Threadly, a social media blogging platform where creators publish posts across different content categories such as Travel, Tech, Food, and Fitness. The content team wants to understand which post categories are resonating most with the audience, measured by total likes received.

You have access to two tables:

Table: categories

Column

Type

Description

category_id

INT

Unique category identifier

category_name

VARCHAR

Name of the content category

Table: posts

Column

Type

Description

post_id

INT

Unique post identifier

title

VARCHAR

Title of the post

category_id

INT

References the category the post belongs to

likes

INT

Total number of likes the post has received

published_date

DATE

Date the post was published

Your Task:

Write a query that returns the total number of posts and total likes per category. Sort the results by total likes in descending order so the most popular category appears first.

Expected Output Columns: category_name, total_posts, total_likes

1
2
3
4
5
Preparing...
Most Popular Post Category by Likes

Most Popular Post Category by Likes You are a data analyst at Threadly , a social media blogging platform where creators publish posts across different content categories such as Travel, Tech, Food, and Fitness. The content team wants to understand which post categories are resonating most with the audience , measured by total likes received. You have access to two tables: Table: categories Column Type Description category_id INT Unique category identifier category_name VARCHAR Name of the content category Table: posts Column Type Description post_id INT Unique post identifier title VARCHAR Title of the post category_id INT References the category the post belongs to likes INT Total number of likes the post has received published_date DATE Date the post was published Your Task: Write a query that returns the total number of posts and total likes per category. Sort the results by total likes in descending order so the most popular category appears first. Expected Output Columns: category_name , total_posts , total_likes

Table Setup (SQL Schema)

-- DDL: Create tables
CREATE TABLE categories (
    category_id   INT PRIMARY KEY,
    category_name VARCHAR(100)
);

CREATE TABLE posts (
    post_id        INT PRIMARY KEY,
    title          VARCHAR(200),
    category_id    INT REFERENCES categories(category_id),
    likes          INT,
    published_date DATE
);

-- DML: Insert sample data
INSERT INTO categories (category_id, category_name) VALUES
(1, 'Travel'),
(2, 'Technology'),
(3, 'Food'),
(4, 'Fitness'),
(5, 'Finance');

INSERT INTO posts (post_id, title, category_id, likes, published_date) VALUES
(1,  '10 Hidden Gems in Southeast Asia',     1, 1820, '2024-01-10'),
(2,  'Budget Travel Tips for 2024',           1,  940, '2024-01-22'),
(3,  'Solo Travel Safety Guide',              1,  670, '2024-02-05'),
(4,  'Getting Started with AI Tools',         2, 2300, '2024-01-15'),
(5,  'The Rise of Edge Computing',            2,  880, '2024-01-28'),
(6,  'Python vs JavaScript in 2024',          2, 1540, '2024-02-10'),
(7,  'Best Street Food Cities in Asia',       3, 1100, '2024-01-18'),
(8,  'Quick 30-Minute Dinner Recipes',        3,  760, '2024-02-01'),
(9,  'Vegan Meal Prep for Beginners',         3,  430, '2024-02-14'),
(10, 'How to Build a Morning Workout',        4,  990, '2024-01-20'),
(11, 'Running Your First 5K',                 4,  620, '2024-02-03'),
(12, 'Home Gym Setup on a Budget',            4, 1350, '2024-02-18'),
(13, 'Understanding Index Funds',             5, 1750, '2024-01-12'),
(14, 'How to Build an Emergency Fund',        5,  830, '2024-01-30'),
(15, 'Credit Card Rewards Explained',         5,  510, '2024-02-20');
Home Videos Quiz Blog