codesnout.com

SQL AND & OR

Google Ads

 

SQL ORDER BY


SQL Sample Code - SQL ORDER BY keyword.


SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC or DESC;

Notes:- Choose ASC for Ascending order or DESC for Descending order.


What does the SQL ORDER BY keyword do?

The SQL ORDER BY keyword simply orders results by specified column(s) in either ascending or descending order.


Sample 1 – SQL ORDER BY (Descending DESC)

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this example we will order the table tblCompany using the Sales column in descending order.

Use the SELECT statement below:

SELECT * FROM tblCompany ORDER BY Sales DESC;

The table of results will look like this:

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
4 SQL Reference Ltd 34 Reference St Pisa 17000
3 Sample Code World 66 SQL St Curry 18000
2 SQL Code Land 2 Code Rd Hamburg 20000

Sample 2 – SQL ORDER BY (Ascending ASC)

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this example we will order the table tblCompany using the Sales column in ascending order.

Use the SELECT statement below:

SELECT * FROM tblCompany ORDER BY Sales ASC;

The table of results will look like this:

Company_ID CompanyName Address Town Sales
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000
1 SQL Sample 1 Sample St Hamburg 10000

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |