The Sql Order by Clause

The Sql Order by Clause

  • Order by Clause is an optional expression in an sql query. If we don’t write, the query sorts according to the primary key column.
  • Orderby clause is used to sort table according to the specified column or the columns.
  • For ascending sort, we write “asc” or nothing after column name.
  • For Descending sort, we write “desc” after column name.
  • For Random sort, we write newid() function after order by clause
  • Instead of column names, sequence number of column names would be wriiten.

Sql Order By Syntax

If we have conditions at the same time then:

TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK

Sql Order By Examples on Library Database

Example 1: List all students sorted by name

Result Of The Query;

Example 2: List all students sorted by surnames(rewerse), then by names

or

Result Of The Query;

Example 3: List the studets where student id in between 30 and 40 sorted by name

Result Of The Query;

11 rows listed

Example 4: List all students randomly.

Result Of The Query;

505 rows listed

Example 5: List one student randomly selected.

Result Of The Query;

1 row listed.

Example 6: List randomly selected two students from 11B Class

Result Of The Query;

2 rows listed.

Example 7: List oldest student

Result Of The Query;

Example 8: List youngest student

Result Of The Query;

Example 9: List the book which have biggest page count

Result Of The Query;

1 row listed.

YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: