Sample Query:
Question: Unsaon ni pag handle sa MSSQL? Unahon ba niya ug fetch tanan records then do sorting and then pick the TOP 1 record or the other way around?Code:SELECT TOP 1 id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC
Sample Query:
Question: Unsaon ni pag handle sa MSSQL? Unahon ba niya ug fetch tanan records then do sorting and then pick the TOP 1 record or the other way around?Code:SELECT TOP 1 id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC
question...why man nangutana ka ana ts? its already the work of mssql's engine...tested and verified to fast.
kong naka index ang column "col1" payts rana "index seek" iya gamiton.. kong dili mag table scan na siya.. mao nai maka hinay sa query
kong gamay rapud og records solud sa table di ra mo matter pro kong mo abot nanang millions dira ka paksiton gamita ang "execution plan" para ma kita nimo ang gi buhat sa SQL
tanawa pud ang IO statistics sa query
There could be two ways on executing this:
1. Doing the ORDER BY then TOP
2. Doing the TOP then the ORDER BY
I posted this question in order to get someone's expert answer on top of their head. Anyway, it seems to me that MSSQL is doing the ORDER BY then the TOP after several hours of simulation. If this post would have got immediate answer then I would have not wasted my time doing some tests just to confirm how MSSQL is doing the order of execution.
i think you do not need to use top when querying in SQL with order by, because the first record is the top record.
I think lahi mo ug pasabot sa TOP. TOP is the MSSQL equivalent of LIMIT in MySQL and PostgreSQL
id equivalent to:Code:SELECT TOP 1 id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC
in MySQL and:Code:SELECT id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC LIMIT 0,1
in PostgreSQLCode:SELECT id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC OFFSET 0 LIMIT 1
Last edited by moodsey211; 03-08-2012 at 11:34 PM. Reason: it's offset not from. sorry
I think u need MAX/MIN Functions
Sample:
SELECT name
FROM employee
WHERE (salary, age ) = (SELECT MAX (salary), MAX (age)
FROM employee_details)
AND dept = 'Electronics'
Similar Threads |
|