bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1241. Number of Comments per Post

1241. Number of Comments per Post

Leetcode Solutions

Self-Join and Aggregation

  1. Select the sub_id from the Submissions table where parent_id is NULL. This will give us the unique post IDs.
  2. Perform a LEFT JOIN on the Submissions table with itself, joining on the condition that the sub_id of the first instance (representing a post) matches the parent_id of the second instance (representing a comment).
  3. Use COUNT(DISTINCT ...) to count the unique sub_id of comments for each post, ensuring that duplicate comments are only counted once.
  4. Group the results by the sub_id of the posts.
  5. Order the final result by post_id in ascending order.

erDiagram
    Submissions {
        int sub_id PK
        int parent_id
    }

Subquery for Comment Count

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...