This sounds like a stupid question while I’m asking it, but I’m new to SQL and not sure if something like this is possible.
I want to select all records from a table except for 2, order them alphabetically and then append the last two to the end. Can I do this in a select statement or do I have to create a temp table and returned a result set from that?
Basically, in pseudo-code, I want to do this:
select
firstname,
lastname,
otherdata
from People
where firstname != 'john' or 'mark'
order by firstname
add John and Mark
Thanks in advance.
No, you need to use a
UNIONto combine queries andORDER BYmust come last. e.g.If your intention is to place those two rows last, then you need something else to order by, e.g.
You might see cute tricks where people will put the query in a subquery and apply an order by there (with the help of
TOP), but beware, this is a ruse! This will not guarantee how the outer query is ordered.