Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lasha90
Starting Member
5 Posts |
Posted - 2011-12-13 : 02:01:37
|
| i made the following table and was trying the order by clause on it by specifying multiple column names..i dint kno the proper syntax of writing asc/desc after every column name.and got the following varied results:::---this is the table named for_orderby:-create table for_orderby( rollno int primary key,name varchar(20),marks int,addre varchar(20))insert into for_orderby values(1,'abhi',20,'ggn')insert into for_orderby values(2,'ankur',30,'saket')insert into for_orderby values(3,'anshu',40,'nakrola')insert into for_orderby values(4,'ayat',50,'delhi')insert into for_orderby values(5,'nishtu',60,'kakrola')insert into for_orderby values(6,'lola',70,'dwarka')I WROTE THE FOLLOWING QUERY FOR ORDER BY:-select * from for_orderby order by marks,name descAND GOT THE FOLLOWING RESULT:-1 abhi 20 ggn2 ankur 30 saket3 anshu 40 nakrola4 ayat 50 delhi5 nishtu 60 kakrola6 lola 70 dwarkaAFTER THIS I WROTE THE FOLLOWING SLIGHTLY DIFFERENT QUERY:-select * from for_orderby order by name,marks descAND GOT THE FOLLOWING OUTPUT:-1 abhi 20 ggn2 ankur 30 saket3 anshu 40 nakrola4 ayat 50 delhi6 lola 70 dwarka5 nishtu 60 kakrolaCAN ANYONE PLZZ EXPLAIN ME THIS CONCEPT OF ORDER BY..HOW IS THIS CLAUSE WORKING ON THE COLUMN NAMES!!!!!!!!!!!!!!!!????????edit: moved to proper forum |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 02:12:43
|
| it will order based on columns you've specified in same sequenceso first case it starts ordering based on ascending order of marks then out of them it tries order based on names in descending order for them with same markssecond case it starts ordering based on ascending order of names then out of them it tries order based on marks in descending order for them with same name------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
johntech
Yak Posting Veteran
51 Posts |
Posted - 2011-12-13 : 08:08:52
|
| Hello ,I can explain it in another way ,1- for your first case you order by marks then by name ,So engine look first for numbers 20,30,40,50,60,70 ,Then he look to alphapitical order if 2 number is equal ,by another word he give first periority to numbers the second for alphaptical orders2- for second case vice versa |
 |
|
|
lasha90
Starting Member
5 Posts |
Posted - 2011-12-13 : 08:43:28
|
| thanks a lot johntech and visakh16. your explanations really helped in clearing my concept. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-13 : 08:52:16
|
quote: Originally posted by lasha90 thanks a lot johntech and visakh16. your explanations really helped in clearing my concept.
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|