Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does order 1,2 do

Tags:

sql

oracle11g

Imagine we have following table

create table t
  (item number,
   bin number,
   primary key (bin , item) );

I have used insert into command to insert several values into table t, now i am interested in what does this code

select * from t
  order by 1,2;

As far as i know it selects everything from table t and sorts it, because order means to sort selected query using condition listed in order command, but order 1,2 i could not understand what does it means, could you help me ?

like image 946
dato datuashvili Avatar asked Jun 28 '12 08:06

dato datuashvili


People also ask

What is the meaning of order by 1/2 in SQL?

It will order the results by the first column, and then, if there are some rows with the same value in the first column it will order them by the second column.

What does order by 1 do in SQL?

it simply means sorting the view or table by 1st column of the query's result.

What does it mean order by 2 in SQL?

SELECT first_name, last_name FROM sales.customers ORDER BY 1, 2; In this example, 1 means the first_name column, and 2 means the last_name column. Using the ordinal positions of columns in the ORDER BY clause is considered a bad programming practice for a couple of reasons.

What does group by 1 do in SQL?

It means to group by the first column of your result set regardless of what it's called. You can do the same with ORDER BY .


2 Answers

It sorts the result by the first and second column, so in your case it's identical to

select *
from t
order by item, bin;
like image 155
a_horse_with_no_name Avatar answered Oct 12 '22 23:10

a_horse_with_no_name


select * from t
  order by item, bin; // just different written but result is same.

Result will be sorted by first and second column.

Difference is in readability of code so if someone don't know information about table, your select says nothing him. Person only knows that result will be sorted by 1. and 2. column, nothing more.

like image 38
Simon Dorociak Avatar answered Oct 13 '22 00:10

Simon Dorociak