MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL Order By Clause

The MYSQL ORDER BY Clause is used to sort the result-set in ascending or descending order. To sort the result set, you must use the ORDER BY clause. MySQL ORDER BY can be used to for single column or multiple columns.

Note: The MySQL ORDER BY clause can be used with SELECT statement, SELECT LIMIT statement, and DELETE LIMIT statement.

MySQL ORDER BY CLAUSE Syntax

To sort the result set from existing table, use the following syntax:

Syntax

SELECT col1, col2,...
FROM tablename
ORDER BY col1 [ASC|DESC], col2 [ASC|DESC],...

Parameters:

tablename: The table name from which you want to remove all records.

ASC: To sort data set in ascending order.

DESC: To sorts data set in descending order.

Note: By default, the ORDER BY clause sorts the result set in ascending order if you don't specify ASC or DESC explicitly.

MySQL ORDER BY CLAUSE example without using ASC/DESC attribute

The following MySQL statement to retrieve records from "tblCustomerLoan" table without ASC/DESC attribute:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan Order By firstName;

Note:

In the above example, we have fetched records from "tblCustomerLoan" table without specifying ASC or DESC in order by clause.

You can use MySQL Command Line Client to fetched records from from "tblCustomerLoan" table without using ASC or DESC in order by clause.It will look like this:

MySQL ORDER BY CLAUSE example with using ASC attribute

The following MySQL statement to retrieve records from "tblCustomerLoan" table with ASC attribute:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan Order By firstName ASC;

Note:

In the above example, we have fetched records from "tblCustomerLoan" table with ASC attribute in order by clause.

You can use MySQL Command Line Client to fetch records from from "tblCustomerLoan" table with using ASC in order by clause.It will look like this:

MySQL ORDER BY CLAUSE example with using DESC attribute

The following MySQL statement to retrieve records from "tblCustomerLoan" table with DESC attribute:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan Order By firstName DESC;

Note:

In the above example, we have fetch records from "tblCustomerLoan" table with DESC attribute in order by clause.

You can use MySQL Command Line Client to fetch records from "tblCustomerLoan" table with using DESC in order by clause.It will look like this:

MySQL ORDER BY CLAUSE example with using ASC & DESC attribute

The following MySQL statement to retrieve records from "tblCustomerLoan" table with ASC & DESC attribute:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan Order By firstName DESC, address ASC;

Note:

In the above example, we have fetch records from "tblCustomerLoan" table with ASC & DESC attribute in order by clause.

You can use MySQL Command Line Client to fetch records from "tblCustomerLoan" table with using ASC & DESC in order by clause.It will look like this: