Skip to content

Instantly share code, notes, and snippets.

@mattm
Last active January 6, 2021 09:27
SELECT
users.user_id,
COUNT(DISTINCT post_id) AS post_count,
COUNT(DISTINCT page_id) AS page_count
FROM users
LEFT JOIN posts ON posts.user_id = users.user_id
LEFT JOIN pages ON pages.user_id = users.user_id
GROUP BY 1
+---------+------------+------------+
| user_id | post_count | page_count |
+---------+------------+------------+
| 1 | 3 | 0 |
| 2 | 2 | 2 |
| 3 | 0 | 5 |
+---------+------------+------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment