Number of Comments per Post - Problem

Imagine you're building a social media analytics dashboard that needs to track engagement metrics! You have a database table called Submissions that contains both posts and comments in a hierarchical structure.

Here's how it works:

  • Each row represents either a post or a comment
  • Posts have parent_id = NULL (they're the root of the conversation)
  • Comments have parent_id pointing to a post's sub_id

Your mission: Calculate how many unique comments each post has received. The table might contain duplicate posts (treat as one) and duplicate comments (count unique ones only).

Expected Output: A result table with post_id and number_of_comments, ordered by post_id ascending.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Submissions table: | sub_id | parent_id | |--------|----------| | 1 | null | | 2 | 1 | | 3 | 1 | | 4 | null | | 5 | 4 | | 6 | 4 |
โ€บ Output: | post_id | number_of_comments | |---------|-------------------| | 1 | 2 | | 4 | 2 |
๐Ÿ’ก Note: Post 1 has comments 2 and 3 (2 total). Post 4 has comments 5 and 6 (2 total). Results ordered by post_id ascending.
example_2.sql โ€” Duplicate Comments
$ Input: Submissions table: | sub_id | parent_id | |--------|----------| | 1 | null | | 2 | 1 | | 3 | 1 | | 2 | 1 | | 4 | null |
โ€บ Output: | post_id | number_of_comments | |---------|-------------------| | 1 | 2 | | 4 | 0 |
๐Ÿ’ก Note: Post 1 has duplicate comment 2, but we count only unique comments (2, 3). Post 4 has no comments.
example_3.sql โ€” No Comments
$ Input: Submissions table: | sub_id | parent_id | |--------|----------| | 1 | null | | 2 | null | | 3 | null |
โ€บ Output: | post_id | number_of_comments | |---------|-------------------| | 1 | 0 | | 2 | 0 | | 3 | 0 |
๐Ÿ’ก Note: All entries are posts with no comments. Each post has 0 comments.

Constraints

  • 1 โ‰ค Number of submissions โ‰ค 1000
  • 1 โ‰ค sub_id โ‰ค 105
  • parent_id is null for posts, valid sub_id for comments
  • The table may contain duplicate rows
  • Results must be ordered by post_id in ascending order

Visualization

Tap to expand
๐Ÿ“ฑ Social Media Analytics Dashboard๐Ÿ“ Posts1"Hello World!"4"Good morning"๐Ÿ’ฌ Comments2"Nice post!" โ†’ 13"Thanks!" โ†’ 15"Great day" โ†’ 46"Indeed!" โ†’ 4๐Ÿ“Š Analytics ReportPost 1:2 commentsPost 4:2 comments๐Ÿ” SQL Processing Steps1. LEFT JOINConnect postswith comments2. GROUP BYGroup bypost_id3. COUNT DISTINCTCount uniquecomments4. ORDER BYSort bypost_id
Understanding the Visualization
1
Identify Posts
Find all unique posts (entries with parent_id = NULL)
2
Match Comments
For each post, find all comments that reference it as parent
3
Count Unique
Count distinct comment IDs to handle duplicates
4
Generate Report
Present results ordered by post ID for consistent reporting
Key Takeaway
๐ŸŽฏ Key Insight: SQL's aggregation functions (GROUP BY + COUNT DISTINCT) handle complex data relationships efficiently in a single query, making it ideal for analytics reporting where duplicate handling and grouping are essential.
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen