Earlier this week, I have discussed SQL SERVER – TOP and DISTINCT – Epic Confusion. I got lots of feedback on this blog post and lots of people wrote emails asking about why the result we get is the result we get. The answer is very simple – it is because of the Logical Processing Order of the SELECT Statement.
The most asked question was which keyword or operator is processed first – DISTINCT or TOP. Well, the answer is very simple and it is given in detail on Microsoft’s website over here.
Here is the list of all the logical processing orders.
- FROM
- ON
- JOIN
- WHERE
- GROUP BY
- WITH CUBE or WITH ROLLUP
- HAVING
- SELECT
- DISTINCT
- ORDER BY
- TOP
While what you see is the logical processing order, the physical processing order is different from it. However, this order should give you an idea of how the query will work logically and will give results accordingly.
Here is the video which started all the conversation related to the logical processing order.
You can find the code of the video listed above in this blog post SQL SERVER – TOP and DISTINCT – Epic Confusion.
If you like the blog post, you can always follow my YouTube channel where you can see many similar videos.
Reference: Pinal Dave (https://blog.sqlauthority.com)
First appeared on SQL SERVER – Logical Processing Order of the SELECT Statement