Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER BY before SELECT?

Tags:

sql

sql-server

I'm just trying to add in an the newest estimated return date for a car as part of a subquery in my Select statement, I just wanted to know if this was how it is done?

I think I heard that Select happens before Order By so wanted to do a quick check.

select top 1 ESTIMATE_RETURN_DATE 
from CHECK.AOS 
where AOS.AUTO_NO = CHECK_EVENT.AUTONUM_4 
ORDER BY REVISION_NO desc
like image 505
finn green Avatar asked Aug 28 '26 00:08

finn green


2 Answers

ORDER BY is evaluated before the SELECT, as the ordering changes the results returned.

TOP 1 also ensures the lowest REVISION_NO is returned, therefore it appears you are using the query correctly.

like image 152
Dominic Zukiewicz Avatar answered Aug 30 '26 15:08

Dominic Zukiewicz


Yes Top works after OrderBY. First it will find all the records and then it would apply Top. So you are doing it right. I am also writing down the order in which a query process in sql server

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
TOP/OFFSET FETCH
like image 25
Ashish Rajput Avatar answered Aug 30 '26 14:08

Ashish Rajput



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!