Skip to content

Instantly share code, notes, and snippets.

@gabidavila
Last active August 21, 2020 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabidavila/25eec2e8117b89e7aadb10ae07128d88 to your computer and use it in GitHub Desktop.
Save gabidavila/25eec2e8117b89e7aadb10ae07128d88 to your computer and use it in GitHub Desktop.
How is your internal query execution?
use blog;
-- Count posts id
SELECT COUNT(DISTINCT id) FROM posts;
-- => 7
-- count posts id
SELECT COUNT(DISTINCT post_id) from comments;
-- => 3
-- select all the posts that doesn't have comments
SELECT id FROM posts WHERE id NOT IN (SELECT DISTINCT post_id from comments);
-- => There are no results to be displayed.
-- hoooooow??? If I do this this works:
SELECT DISTINCT p.id
FROM posts p
LEFT JOIN comments c ON c.post_id = p.id
WHERE c.id IS NULL
-- => [1, 2, 3, 4]
-- ping me for the solution or add your comments here
@gingerCodeNinja
Copy link

Your first one worked for me - http://sqlfiddle.com/#!9/1e130d/1

@gabidavila
Copy link
Author

You are correct @gingerCodeNinja

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment