Skip to content

Instantly share code, notes, and snippets.

@tmilewski
Created June 18, 2009 17:31
Show Gist options
  • Save tmilewski/132033 to your computer and use it in GitHub Desktop.
Save tmilewski/132033 to your computer and use it in GitHub Desktop.
Trying to get the num of faculty assoc w/ a dept incl. the depts w/o any associations
SELECT d.id as dept_id, d.department, count(f.id) as cnt
FROM departments d JOIN faculty f ON f.department_primary = d.id
GROUP BY d.department
UNION
SELECT d.id as dept_id, d.department, 0 as cnt
FROM departments d
WHERE d.id NOT IN (SELECT DISTINCT(department_primary) FROM faculty)
ORDER BY department ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment