2

Query:

SELECT * FROM station

Output:

id | stationname | total
-------------------------
 1 | Khar        | 5
 2 | SantaCruz   | 3
 3 | Sion        | 2
 4 | VT          | 1
 5 | newFort     | 3
 6 | Bandra      | 2
 7 | Worli       | 1
 8 | Sanpada     | 3
 9 | Joe         | 2
10 | Sally       | 1
11 | Elphiston   | 2
12 | Currey Road | 1

Is it possible to display all the record but with a condition that will display particular record at 1st position and below then display rest all records

E.g. my where condition is

where id=10

Desired output:

id | stationname | total
-------------------------
10 | Sally       | 1
 1 | Khar        | 5
 2 | SantaCruz   | 3
 3 | Sion        | 2
 4 | VT          | 1
 5 | newFort     | 3
 6 | Bandra      | 2
 7 | Worli       | 1
 8 | Sanpada     | 3
 9 | Joe         | 2
11 | Elphiston   | 2
12 | Currey Road | 1

Any idea, hint will be appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Satinder singh
  • 10,100
  • 16
  • 60
  • 102

2 Answers2

3

You can union two different query using UNION ALL like this:

SELECT * FROM Station WHERE ID=10
UNION ALL
SELECT * FROM Station WHERE ID<>10

See this SQLFiddle


Update (related to returned order is undefined)

If you see the execution plan of both queries (using UNION and UNION ALL), the query using UNION will sort the result while the query using UNION ALL will not sort the result. see execution plan of both queries in this SQLFiddle.

Execution Plan using UNION query:

enter image description here

Execution Plan using UNION ALL query:

enter image description here

I found this from SQL SERVER – Introduction and Example of UNION and UNION ALL

Community
  • 1
  • 1
Himanshu
  • 31,810
  • 31
  • 111
  • 133
3

Using a UNION ALL could work in the case presented by OP but comes with some pitfalls that might lead to unexpected results when used indiscriminately.

A searched sort order alleviates these problems, provided that an id can be choosen to be guaranteed the first in the list (0 in this example)

SELECT * 
FROM   Station 
ORDER BY
       CASE WHEN id = 10 THEN 0 ELSE id END

See this SQLFiddle (cudo's to hims056 for setting it up)

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • @satindersingh - Thank you. I might have added that the `THEN` clause should contain the lowest possible `id` value but I assume that speaks for itself. – Lieven Keersmaekers Nov 24 '12 at 11:05
  • @LievenKeersmaekers - Your solution is better. – Himanshu Nov 24 '12 at 11:11
  • 1
    @hims056 - it might. Time will tell ;) – Lieven Keersmaekers Nov 24 '12 at 11:15
  • In free time will definetly do rearch on this which one is better, soon you get to know :) – Satinder singh Nov 24 '12 at 11:17
  • 1
    @LievenKeersmaekers - In the execution plan of both queries (using `UNION` and `UNION ALL`), query using `UNION` will sort the result while query using `UNION ALL` will not sort the result. see execution plan of both queries [here](http://sqlfiddle.com/#!3/b0f65/15). I found it from [this article](http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/) – Himanshu Nov 24 '12 at 11:32
  • 1
    @hims056 - I finally taken the time to test one and another. For this particular case it works but to prove a point *(yes I **am** that evil)*, I have altered the statements a bit and added a clustered index **without altering the insert order**. You will notice that the results are now returned not on insert order but on clustered index order. All in all, you can trust on the UNION ALL behaving the way you expect but you have to keep in mind all those little things that might throw off your results. [SQL Fiddle Demo](http://sqlfiddle.com/#!3/dc0ce/1) – Lieven Keersmaekers Nov 24 '12 at 12:04
  • This is the correct answer. The Union method may not produce the expected sort all the time. – Sam Anwar Nov 24 '12 at 19:22