Skip to content

Instantly share code, notes, and snippets.

@maebeale
Created February 17, 2015 16:57
Show Gist options
  • Save maebeale/2c85f2d691efcab4c441 to your computer and use it in GitHub Desktop.
Save maebeale/2c85f2d691efcab4c441 to your computer and use it in GitHub Desktop.
# SQL challenge #6
# find authors who have no surveys
# In Rails:
Survey.where(author_id :1).empty?
-OR-
a = Author.find(1)
a.surveys.empty?
a.surveys.any?
Rails vs SQL
Author.where(name: [fallon, jonathan, rocky, chris])
Author.where(id: [1, 2, 3, 4 ]).map(&:name)
SELECT name FROM authors;
fallon
jonathan
rocky
chris
# Author.where in Rails == SELECT * from authors
# Author.all.map(&:name) == SELECT name from authors
1.
SELECT name
FROM authors
LEFT JOIN inner surveys
ON surveys.author_id=authors.id
WHERE surveys.author_id IS NULL;
2.
SELECT name, email, phone
FROM authors
WHERE id
NOT IN
( SELECT author_id FROM surveys );
()
SELECT name
FROM authors
WHERE id
IN
( SELECT id FROM surveys );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment