Skip to content

Instantly share code, notes, and snippets.

@davejlong
Created May 13, 2011 14:24
Show Gist options
  • Save davejlong/970617 to your computer and use it in GitHub Desktop.
Save davejlong/970617 to your computer and use it in GitHub Desktop.
SQL Output with Nulls at the bottom of the output
SELECT schools.id AS schoolid,schools.name AS school,districts.id AS districtid, districts.name AS district
FROM sms_schools AS schools
LEFT JOIN sms_districts AS districts ON schools.districtid = districts.id
WHERE 1 = 1
ORDER BY (CASE WHEN districts.id IS NULL then 1 ELSE 0 END),districts.name, schools.name;
SELECT
schools.id AS schoolid,
schools.name AS school,
districts.id AS districtid,
(CASE WHEN districts.name IS NULL THEN 'Other' ELSE districts.name END) AS district
FROM sms_schools AS schools
LEFT JOIN sms_districts AS districts ON schools.districtid = districts.id
WHERE 1 = 1
ORDER BY (CASE WHEN districts.id IS NULL then 1 ELSE 0 END),districts.name, schools.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment