EXCEPT with ORDER BY
EXCEPT with ORDER BY is a SQL statement in the Set Operations category. Set operations can have ORDER BY at the end, applied to the entire combined result set. The syntax is SELECT columns FROM table1 EXCEPT SELECT columns FROM table2 ORDER BY column;. It returns ordered set result. A typical example: SELECT product_id FROM products EXCEPT SELECT product_id FROM discontinued ORDER BY product_id DESC; -- Products not discontinued, ordered by id descending -- ORDER BY refers to column names from the first SELECT -- Can also use column position: ORDER BY 1 A close relative is UNION, which combines results from two queries, removing duplicate rows. Each SELECT must have the same number of columns. A close relative is UNION ALL, which like UNION but does NOT remove duplicate rows. Faster because no dedup is needed. A close relative is INTERSECT, which returns rows that appear in BOTH query results. Supported in PostgreSQL, SQL Server, Oracle.