Top 5 subreddits by post count on 2024-01-15

Quick Overview

Write a SQL query that finds the five subreddits with the most posts created on a single calendar day and returns each name with its post count. It tests joining posts to subreddits, filtering on the right timestamp column, grouping, and ordering with a unique tie-breaker before LIMIT.

Top 5 subreddits by post count on 2024-01-15

Company: Reddit

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Using the `subreddits` and `posts` tables, find the top 5 subreddits by number of posts whose `created_at` falls on 2024-01-15 (00:00:00 to 23:59:59). Return each subreddit's name with its post count. If fewer than 5 subreddits have posts that day, return only those. ```hint Which timestamp Both tables have a `created_at` column. Make sure the day filter reads the one that describes the post. ``` ### Input Table(s) Table `subreddits`, primary key `subreddit_id`: | Column | Type | Meaning | |---|---|---| | subreddit_id | INT | Unique id of the subreddit (primary key) | | name | VARCHAR(100) | Subreddit name, never NULL | | description | VARCHAR(500) | Free-text description, may be NULL | | created_at | TIMESTAMP | When the subreddit was created, never NULL | Table `posts`, primary key `post_id`; `subreddit_id` references `subreddits.subreddit_id`: | Column | Type | Meaning | |---|---|---| | post_id | INT | Unique id of the post (primary key) | | subreddit_id | INT | Subreddit the post belongs to, never NULL | | user_id | INT | Author of the post, never NULL | | title | VARCHAR(300) | Post title, never NULL | | content | TEXT | Post body, may be NULL | | created_at | TIMESTAMP | When the post was created, never NULL | ### Output Contract - Columns, in this order: `subreddit_name` (the subreddit's `name`), `post_count` (integer, the number of posts created on 2024-01-15). - One row per subreddit with at least one post on 2024-01-15, at most 5 rows. - Sort by `post_count` descending, then by `subreddit_id` ascending. `subreddit_id` is unique, so the order is fully determined even though it is not an output column. - No rounding applies. ### Example Table `subreddits` (`description` omitted): | subreddit_id | name | created_at | |---|---|---| | 1 | science | 2012-05-01 09:00:00 | | 2 | woodworking | 2013-02-10 14:00:00 | | 3 | cats | 2011-08-20 18:30:00 | | 4 | nba | 2010-11-03 12:00:00 | | 5 | budgetfood | 2019-06-07 07:45:00 | | 6 | astronomy | 2014-09-14 21:10:00 | | 7 | newcity | 2024-01-15 08:00:00 | Table `posts` (`user_id`, `title` and `content` omitted; they do not affect the result): | post_id | subreddit_id | created_at | |---|---|---| | 201 | 2 | 2024-01-15 00:00:00 | | 202 | 1 | 2024-01-15 07:00:00 | | 203 | 3 | 2024-01-15 07:45:00 | | 204 | 2 | 2024-01-15 10:20:00 | | 205 | 4 | 2024-01-15 12:00:00 | | 206 | 1 | 2024-01-15 13:30:00 | | 207 | 5 | 2024-01-15 15:00:00 | | 208 | 3 | 2024-01-15 18:05:00 | | 209 | 2 | 2024-01-15 23:59:59 | | 210 | 6 | 2024-01-15 21:40:00 | | 211 | 6 | 2024-01-14 23:59:59 | | 212 | 4 | 2024-01-16 00:00:00 | | 213 | 7 | 2024-01-16 09:00:00 | Expected result: | subreddit_name | post_count | |---|---| | woodworking | 3 | | science | 2 | | cats | 2 | | nba | 1 | | budgetfood | 1 | woodworking has three posts on the day, including ones at 00:00:00 and 23:59:59. science and cats tie at 2, and science comes first because its `subreddit_id` (1) is lower. nba, budgetfood and astronomy each have one post on the day (nba's post at 2024-01-16 00:00:00 and astronomy's at 2024-01-14 23:59:59 fall outside it), and the five-row limit keeps the two with the lowest ids. newcity was created on 2024-01-15, but its only post is from the next day, so it does not appear. ### Constraints and Clarifications - `posts` has at most 10,000 rows and `subreddits` at most 1,000 rows. - Each post belongs to exactly one existing subreddit. - A post counts when its `created_at` is on or after `2024-01-15 00:00:00` and before `2024-01-16 00:00:00`. With whole-second timestamps this is the same as 00:00:00 to 23:59:59 inclusive. - Only `posts.created_at` decides the day. `subreddits.created_at` is when the community was created and has no effect on the count. - `description` and `content` may be NULL; they do not affect the result. - Subreddit names are not declared unique, so two subreddits with the same name are separate rows. - Subreddits with no posts on the day are left out, so the result can have fewer than 5 rows.

Overview: Write a SQL query that finds the five subreddits with the most posts created on a single calendar day and returns each name with its post count. It tests joining posts to subreddits, filtering on the right timestamp column, grouping, and ordering with a unique tie-breaker before LIMIT.

Reddit logo
Reddit
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Using the subreddits and posts tables, find the top 5 subreddits by number of posts whose created_at falls on 2024-01-15 (00:00:00 to 23:59:59). Return each subreddit's name with its post count. If fewer than 5 subreddits have posts that day, return only those.

Input Table(s)

Table subreddits, primary key subreddit_id:

ColumnTypeMeaning
subreddit_idINTUnique id of the subreddit (primary key)
nameVARCHAR(100)Subreddit name, never NULL
descriptionVARCHAR(500)Free-text description, may be NULL
created_atTIMESTAMPWhen the subreddit was created, never NULL

Table posts, primary key post_id; subreddit_id references subreddits.subreddit_id:

ColumnTypeMeaning
post_idINTUnique id of the post (primary key)
subreddit_idINTSubreddit the post belongs to, never NULL
user_idINTAuthor of the post, never NULL
titleVARCHAR(300)Post title, never NULL
contentTEXTPost body, may be NULL
created_atTIMESTAMPWhen the post was created, never NULL

Output Contract

  • Columns, in this order: subreddit_name (the subreddit's name ), post_count (integer, the number of posts created on 2024-01-15).
  • One row per subreddit with at least one post on 2024-01-15, at most 5 rows.
  • Sort by post_count descending, then by subreddit_id ascending. subreddit_id is unique, so the order is fully determined even though it is not an output column.
  • No rounding applies.

Example

Table subreddits (description omitted):

subreddit_idnamecreated_at
1science2012-05-01 09:00:00
2woodworking2013-02-10 14:00:00
3cats2011-08-20 18:30:00
4nba2010-11-03 12:00:00
5budgetfood2019-06-07 07:45:00
6astronomy2014-09-14 21:10:00
7newcity2024-01-15 08:00:00

Table posts (user_id, title and content omitted; they do not affect the result):

post_idsubreddit_idcreated_at
20122024-01-15 00:00:00
20212024-01-15 07:00:00
20332024-01-15 07:45:00
20422024-01-15 10:20:00
20542024-01-15 12:00:00
20612024-01-15 13:30:00
20752024-01-15 15:00:00
20832024-01-15 18:05:00
20922024-01-15 23:59:59
21062024-01-15 21:40:00
21162024-01-14 23:59:59
21242024-01-16 00:00:00
21372024-01-16 09:00:00

Expected result:

subreddit_namepost_count
woodworking3
science2
cats2
nba1
budgetfood1

woodworking has three posts on the day, including ones at 00:00:00 and 23:59:59. science and cats tie at 2, and science comes first because its subreddit_id (1) is lower. nba, budgetfood and astronomy each have one post on the day (nba's post at 2024-01-16 00:00:00 and astronomy's at 2024-01-14 23:59:59 fall outside it), and the five-row limit keeps the two with the lowest ids. newcity was created on 2024-01-15, but its only post is from the next day, so it does not appear.

Constraints and Clarifications

  • posts has at most 10,000 rows and subreddits at most 1,000 rows.
  • Each post belongs to exactly one existing subreddit.
  • A post counts when its created_at is on or after 2024-01-15 00:00:00 and before 2024-01-16 00:00:00 . With whole-second timestamps this is the same as 00:00:00 to 23:59:59 inclusive.
  • Only posts.created_at decides the day. subreddits.created_at is when the community was created and has no effect on the count.
  • description and content may be NULL; they do not affect the result.
  • Subreddit names are not declared unique, so two subreddits with the same name are separate rows.
  • Subreddits with no posts on the day are left out, so the result can have fewer than 5 rows.