What’s evaluated first: DISTINCT or TOP? | Essential SQL
When writing SQL, the order of operation is important. When writing SQL, the order of operation is important. What’s evaluated first: DISTINCT or TOP? A coworker and I were writing a query and came across this exact question. To our surprise, between both of our experiences, we didn't know the answer off the top of our heads. That lead me to make this video for you! Don’t forget the larger picture, that SQL does evaluate the SELECT statement in an order of operations: 1. FROM 2. ON 3. JOIN 4. WHERE 5. GROUP BY 6. WITH CUBE or WITH ROLLUP 7. HAVING 8. SELECT 9. DISTINCT 10. ORDER BY 11. TOP Here is a good reference to learn more about this: https://docs.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver15#logical-processing-order-of-the-select-statement) -------------------- This video is part of my Beginner Learning series. If you're interested in learning SQL subscribe to @Essential SQL and then check out our Beginner Learner Playlist.