JOHN
JOHN

Reputation: 11

How is the Order By working in this query

SELECT * FROM TT 
ORDER BY (SELECT AGE 
          FROM TTT 
          WHERE NAME='TANDEL'
          );

Result is,

JOHN    2
PRAVEEN 2
KUMAR   2
RAKESH  2
WASIB   2
GURUNG  2
DAYALAN 5
DEEPAK  2
TANDEL  5
RAGHU   5
GIGA    5
DEEPA   5
DEVARAJ 2

Note: Table TT and TTT both have same data.

Upvotes: 0

Views: 57

Answers (3)

nitesh.kodle123
nitesh.kodle123

Reputation: 1033

In the order by clause we can add two thing:

  • Column names

    Select * from TTT order by age;
    
  • Coulmn number to filter

    Select * from TTT order by 2;//if age is the second column in the table 
                or
    Select name,age from TTT order by 2; // this will order by age
    

In the following case the inner query

  SELECT AGE 
      FROM TTT 
      WHERE NAME='TANDEL'

will return 5

and the parent query

  SELECT * FROM TT 
   ORDER BY 5

Upvotes: 0

Nagaraj S
Nagaraj S

Reputation: 13484

Order By requires a column or some columns

ORDER BY CLAUSE

SELECT * 
FROM TT 
ORDER BY colname

Upvotes: 1

Solaris
Solaris

Reputation: 93

Quite simply, it's not working. Order By requires a column or columns, if random data is passed into it instead of column names, it does nothing.

Upvotes: 4

Related Questions