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
You can union two different query using
UNION ALLlike this:See this SQLFiddle
Update (related to returned order is undefined)
If you see the execution plan of both queries (using
UNIONandUNION ALL), the query usingUNIONwill sort the result while the query usingUNION ALLwill not sort the result. see execution plan of both queries in this SQLFiddle.Execution Plan using
UNIONquery:Execution Plan using
UNION ALLquery:I found this from SQL SERVER – Introduction and Example of UNION and UNION ALL