Top 5 public subreddits by comment count on 2024-01-15

Quick Overview

Write a SQL query that returns the five public subreddits with the most comments inside a one-day timestamp window. It tests inner joins, half-open date filters, filtering before aggregation, and a deterministic tie-break that decides which rows survive the LIMIT.

Top 5 public subreddits by comment count on 2024-01-15

Company: Reddit

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a SQL query that returns the top 5 public subreddits by comment count, counting only comments whose `created_at` is in the window `>= '2024-01-15 00:00:00'` and `< '2024-01-16 00:00:00'`. Private subreddits (`is_private = 1`) are excluded. If fewer than 5 public subreddits have comments in the window, return only those. ```hint Window boundaries Check which end of the window is inclusive, and trace what happens to a comment stamped exactly at midnight on either end. ``` ### Input Table(s) Table `subreddits`, primary key `subreddit_id`: | Column | Type | Meaning | |---|---|---| | subreddit_id | INT | Unique id of the subreddit (primary key) | | subreddit_name | VARCHAR(100) | Display name, never NULL | | is_private | SMALLINT | 0 for public, 1 for private; never NULL, defaults to 0 | Table `comments`, primary key `comment_id`; `subreddit_id` references `subreddits.subreddit_id`: | Column | Type | Meaning | |---|---|---| | comment_id | INT | Unique id of the comment (primary key) | | subreddit_id | INT | Subreddit the comment was posted in, never NULL | | user_id | INT | Author of the comment, never NULL | | content | TEXT | Comment body, may be NULL | | created_at | TIMESTAMP | When the comment was posted, never NULL | ### Output Contract - Columns, in this order: `subreddit_name`, `comment_count` (integer, the number of comments in the window). - One row per public subreddit that has at least one comment in the window, at most 5 rows. - Sort by `comment_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`: | subreddit_id | subreddit_name | is_private | |---|---|---| | 1 | python | 0 | | 2 | worldnews | 0 | | 3 | secretclub | 1 | | 4 | gaming | 0 | | 5 | movies | 0 | | 6 | travel | 0 | | 7 | cooking | 0 | | 8 | art | 0 | Table `comments` (`content` omitted; it does not affect the result): | comment_id | subreddit_id | user_id | created_at | |---|---|---|---| | 101 | 1 | 11 | 2024-01-15 00:00:00 | | 102 | 1 | 12 | 2024-01-15 09:30:00 | | 103 | 1 | 11 | 2024-01-15 23:59:59 | | 104 | 2 | 13 | 2024-01-15 08:00:00 | | 105 | 2 | 14 | 2024-01-15 12:15:00 | | 106 | 3 | 15 | 2024-01-15 10:00:00 | | 107 | 3 | 15 | 2024-01-15 10:05:00 | | 108 | 3 | 16 | 2024-01-15 11:00:00 | | 109 | 4 | 12 | 2024-01-15 14:00:00 | | 110 | 4 | 17 | 2024-01-15 18:45:00 | | 111 | 4 | 18 | 2024-01-16 00:00:00 | | 112 | 5 | 13 | 2024-01-15 20:00:00 | | 113 | 6 | 19 | 2024-01-15 07:20:00 | | 114 | 8 | 20 | 2024-01-15 16:10:00 | | 115 | 7 | 21 | 2024-01-14 23:59:59 | Expected result: | subreddit_name | comment_count | |---|---| | python | 3 | | worldnews | 2 | | gaming | 2 | | movies | 1 | | travel | 1 | python has three comments in the window, including one at exactly 00:00:00 and one at 23:59:59. secretclub also has three comments that day but is private, so it is left out. gaming's comment at 2024-01-16 00:00:00 falls outside the window, which leaves gaming with 2 and a tie with worldnews; worldnews comes first because its `subreddit_id` (2) is lower. movies, travel and art each have one comment, and only the two with the lowest ids fit in the five rows. cooking's only comment is from 2024-01-14 23:59:59, so cooking does not appear. ### Constraints and Clarifications - `subreddits` has at most 10,000 rows and `comments` at most 100,000 rows. - `is_private` is always 0 (public) or 1 (private). Only public subreddits are reported. - The window includes `2024-01-15 00:00:00` and excludes `2024-01-16 00:00:00`. - `subreddit_id`, `user_id` and `created_at` in `comments` are never NULL, and every `comments.subreddit_id` points to an existing subreddit. `content` may be NULL; such a comment still counts. - Every comment row counts, including several comments by the same user. - Subreddit names are not declared unique, so two subreddits with the same name are separate rows. - A public subreddit with no comments in the window does not appear, so the result can have fewer than 5 rows.

Overview: Write a SQL query that returns the five public subreddits with the most comments inside a one-day timestamp window. It tests inner joins, half-open date filters, filtering before aggregation, and a deterministic tie-break that decides which rows survive the LIMIT.

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

Write a SQL query that returns the top 5 public subreddits by comment count, counting only comments whose created_at is in the window >= '2024-01-15 00:00:00' and < '2024-01-16 00:00:00'. Private subreddits (is_private = 1) are excluded. If fewer than 5 public subreddits have comments in the window, return only those.

Input Table(s)

Table subreddits, primary key subreddit_id:

ColumnTypeMeaning
subreddit_idINTUnique id of the subreddit (primary key)
subreddit_nameVARCHAR(100)Display name, never NULL
is_privateSMALLINT0 for public, 1 for private; never NULL, defaults to 0

Table comments, primary key comment_id; subreddit_id references subreddits.subreddit_id:

ColumnTypeMeaning
comment_idINTUnique id of the comment (primary key)
subreddit_idINTSubreddit the comment was posted in, never NULL
user_idINTAuthor of the comment, never NULL
contentTEXTComment body, may be NULL
created_atTIMESTAMPWhen the comment was posted, never NULL

Output Contract

  • Columns, in this order: subreddit_name , comment_count (integer, the number of comments in the window).
  • One row per public subreddit that has at least one comment in the window, at most 5 rows.
  • Sort by comment_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:

subreddit_idsubreddit_nameis_private
1python0
2worldnews0
3secretclub1
4gaming0
5movies0
6travel0
7cooking0
8art0

Table comments (content omitted; it does not affect the result):

comment_idsubreddit_iduser_idcreated_at
1011112024-01-15 00:00:00
1021122024-01-15 09:30:00
1031112024-01-15 23:59:59
1042132024-01-15 08:00:00
1052142024-01-15 12:15:00
1063152024-01-15 10:00:00
1073152024-01-15 10:05:00
1083162024-01-15 11:00:00
1094122024-01-15 14:00:00
1104172024-01-15 18:45:00
1114182024-01-16 00:00:00
1125132024-01-15 20:00:00
1136192024-01-15 07:20:00
1148202024-01-15 16:10:00
1157212024-01-14 23:59:59

Expected result:

subreddit_namecomment_count
python3
worldnews2
gaming2
movies1
travel1

python has three comments in the window, including one at exactly 00:00:00 and one at 23:59:59. secretclub also has three comments that day but is private, so it is left out. gaming's comment at 2024-01-16 00:00:00 falls outside the window, which leaves gaming with 2 and a tie with worldnews; worldnews comes first because its subreddit_id (2) is lower. movies, travel and art each have one comment, and only the two with the lowest ids fit in the five rows. cooking's only comment is from 2024-01-14 23:59:59, so cooking does not appear.

Constraints and Clarifications

  • subreddits has at most 10,000 rows and comments at most 100,000 rows.
  • is_private is always 0 (public) or 1 (private). Only public subreddits are reported.
  • The window includes 2024-01-15 00:00:00 and excludes 2024-01-16 00:00:00 .
  • subreddit_id , user_id and created_at in comments are never NULL, and every comments.subreddit_id points to an existing subreddit. content may be NULL; such a comment still counts.
  • Every comment row counts, including several comments by the same user.
  • Subreddit names are not declared unique, so two subreddits with the same name are separate rows.
  • A public subreddit with no comments in the window does not appear, so the result can have fewer than 5 rows.