Skip to content

Instantly share code, notes, and snippets.

@ggodreau
Created July 23, 2018 11:58
Show Gist options
  • Save ggodreau/07224e42ca6e2b8fdd79d36f9980c5b7 to your computer and use it in GitHub Desktop.
Save ggodreau/07224e42ca6e2b8fdd79d36f9980c5b7 to your computer and use it in GitHub Desktop.
mySQL vs PostgreSQL exception join example
/* All product subcategory IDs
* for non bicycle frame items
* mySQL example using WHERE / NOT IN
*/
SELECT p.Name, p.Color, p.ProductSubcategoryID
FROM Product p
LEFT JOIN ProductSubcategory ps on ps.ProductSubcategoryID = p.ProductSubcategoryID
WHERE ps.ProductSubcategoryID NOT IN
(SELECT ProductSubcategoryID FROM ProductSubcategory WHERE Name LIKE '%Frame%')
/* All product subcategory IDs
* for non bicycle frame items
* postgres example using except
*/
SELECT p.ProductSubcategoryID
FROM Product p
LEFT JOIN ProductSubcategory ps on ps.ProductSubcategoryID = p.ProductSubcategoryID
EXCEPT ps.ProductSubcategoryID IN
(SELECT ProductSubcategoryID FROM ProductSubcategory WHERE Name LIKE '%Frame%')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment