You Forgot to Say the Magic Word…
Posted by Dylan Beattie on 20 October 2009 • permalinkIn Microsoft SQL Server, this query won’t work:
SELECT * FROM ( SELECT * FROM Customer UNION SELECT * FROM Supplier) ORDER BY CompanyName
But – if you ask nicely, it does exactly what you’d expect:
SELECT * FROM ( SELECT * FROM Customer UNION SELECT * FROM Supplier) PLEASE ORDER BY CompanyName
You won’t believe the look on your colleague’s faces when you solve their problem using simple good manners.
(Of course, it actually works because PLEASE in that context just acts as a table-name alias for result of the UNION sub-select, and sub-selects in SQL Server need to have a name. But don't let that stop you using it for fun and profit.)