Results 1 to 8 of 8
  1. #1

    Default [MSSQL Query] TOP along with ORDER BY, which is executed first


    Sample Query:

    Code:
    SELECT TOP 1 id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC
    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?

  2. #2

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    question...why man nangutana ka ana ts? its already the work of mssql's engine...tested and verified to fast.

  3. #3

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    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

  4. #4

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    Quote Originally Posted by SuperStar View Post
    question...why man nangutana ka ana ts? its already the work of mssql's engine...tested and verified to fast.
    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.

  5. #5

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    i think you do not need to use top when querying in SQL with order by, because the first record is the top record.

  6. #6

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    Quote Originally Posted by elatagaw View Post
    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
    Code:
    SELECT TOP 1 id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC
    id equivalent to:
    Code:
    SELECT id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC LIMIT 0,1
    in MySQL and:
    Code:
    SELECT id FROM myTable WHERE col1 = 'myCriteria' ORDER BY col2 DESC OFFSET 0 LIMIT 1
    in PostgreSQL
    Last edited by moodsey211; 03-08-2012 at 11:34 PM. Reason: it's offset not from. sorry

  7. #7

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    Quote Originally Posted by elatagaw View Post
    i think you do not need to use top when querying in SQL with order by, because the first record is the top record.
    So, unsay imo solution/suggestion if gusto ka mo pa return sa highest or lowest value if you'll not be using TOP along with ORDER BY?

  8. #8

    Default Re: [MSSQL Query] TOP along with ORDER BY, which is executed first

    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'

  9.    Advertisement

Similar Threads

 
  1. Homosexuality, along with Incest and Bestiality is Wrong
    By Gwapito.com in forum General Discussions
    Replies: 36
    Last Post: 11-12-2012, 10:13 AM
  2. Homosexuality, along with Incest and Bestiality is Wrong
    By Gwapito.com in forum Politics & Current Events
    Replies: 19
    Last Post: 11-09-2012, 09:24 AM
  3. Replies: 7
    Last Post: 02-28-2010, 01:40 AM
  4. With You by BTU
    By norilyn.tan in forum Music & Radio
    Replies: 2
    Last Post: 12-01-2007, 02:44 PM
  5. E-VAT is approved with Finality by SC
    By UCAV in forum Politics & Current Events
    Replies: 71
    Last Post: 11-13-2005, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top