SELECT - with ORDER BY - but field not in SELECT list

  • is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.

    Like ...

    SELECT F1, F2, F3 from table ORDER BY F4 asc

  • In simple words yes it is. As an example:

    SELECT [Id],[Col1], [Col2]

    FROM [dbo].[QOD60] ORDER BY [Col2]

    Results:

    Id Col1 Col2

    ----------- ----------- -----------

    4 4 1

    1 1 2

    2 2 3

    3 3 4

    SELECT [Id],[Col1]

    FROM [dbo].[QOD60] ORDER BY [Col2]

    Results:

    Id Col1

    ----------- -----------

    4 4

    1 1

    2 2

    3 3

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • balasach82 (6/16/2012)


    is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.

    Like ...

    SELECT F1, F2, F3 from table ORDER BY F4 asc

    Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.

  • peacesells (6/16/2012)


    balasach82 (6/16/2012)


    is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.

    Like ...

    SELECT F1, F2, F3 from table ORDER BY F4 asc

    Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.

    There's another exception, when you're using SELECT DISTINCT you must include the fields used in the GROUP BY clause in the select list.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (6/19/2012)


    peacesells (6/16/2012)


    balasach82 (6/16/2012)


    is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.

    Like ...

    SELECT F1, F2, F3 from table ORDER BY F4 asc

    Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.

    There's another exception, when you're using SELECT DISTINCT you must include the fields used in the GROUP BY clause in the select list.

    Why is this in addition to

    For a group by clause though you'd need it to be in your select statement.

    ?

    Jared
    CE - Microsoft

  • My mistake, I meant ORDER BY.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • If you're SELECTing from the results delivered by a CTE, any field listed on the ORDER BY must also be returned from the CTE, even if they're not part of the SELECT list FROM CTE.

    Just thought I'd mention it. A CTE isn't really different than a table in this respect.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply