I’m reading Querying Microsoft SQL Server 2012 by By Dejan Sarka, Itzik Ben-Gan, Ron Talmage and I thought it would be good to post something for each chapter.
My first offering is a sketchnote of the order in which a query is performed.
The example from the book is
SELECT country, YEAR(hiredate) AS yearhired, COUNT() AS numemployees
WHERE hiredate >= '20030101'
GROUP BY country, YEAR(hiredate)
HAVING COUNT() > 1
ORDER BY country, yearhired DESC;
In principle, the order is:
- group by
- order by
(Better version to be uploaded soon, I hope)
‘Order by’ and the Sorting Hat
In the course of doing the sketch, I thought about drawing Harry Potter’s Sorting Hat for the ‘order by’….but they aren’t all sorted into a specific order, so I didn’t.
On reflection, though the Sorting Hat would have been fine, it’s just that the ‘order by’ is
ORDER BY house
ORDER BY house, surname, forename
…but you’d still do it with an order by. How the Hat determines which house for which student is, of course, a more complicated question.