Sorting Data in ADO.NET ado.net tutorials

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson covers how to sort data in ADO.NET

Once you learn how to retrieve a list of columns from a table and filter the results, the next thing to learn is how to sort the data that are retrieved using the ORDER BY clause.

The ORDER BY clause is the very last part of the SELECT statement that the database engine will process, and you can specify one or more columns in the ORDER BY clause separated by commas.

    SELECT FirstName, LastName FROM Users ORDER BY FirstName, Age
  

The sort order is ascending by default, but you can specify descending order by using the keyword DESCENDING or DESC after the column name.

    SELECT FirstName, LastName FROM Users ORDER BY FirstName DESC, Age DESC
  

You can also specify ASCENDING or ASC if you wish, but as mentioned the sort order is ascending by default.

The syntax of the ORDER BY clause is the following:

    SELECT [column1] FROM [tablename] ORDER BY [column1] [sort direction]
  

In addition you can use functions in the ORDER BY clause.

    SELECT FirstName, LastName FROM Users ORDER BY YEAR(Birthday)
  
    SELECT FirstName, LastName FROM Users ORDER BY MAX(TotalDue)
  


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.