Java JDBC SQL Sorting Column Order By Clause - Java @ Desk

Saturday, November 30, 2013

Java JDBC SQL Sorting Column Order By Clause

Java JDBC SQL Sorting Column Order By Clause
Sorting a column is done using the Order By clause in SQL. Sorting a column can be done using following keyword:
1) Order By <COLUMN_NAME> asc - Display the result in ascending order for the <COLUMN_NAME>
2) Order By <COLUMN_NAME> desc - Display the result in descending order for the <COLUMN_NAME>


SELECT col_name FROM table_name ORDER BY col_name ASC:
SELECT col_name FROM table_name ORDER BY col_name DESC:

Consider the below table:
Customer
CustId CustName
1 Cynthia
2 Thanh
3 Peter
4 Eric




Below Query will display the records in ascending order for CustName Column

SELECT * FROM Customer ORDER BY CustName ASC:
CustId CustName
1 Cynthia
4 Eric
3 Peter
2 Thanh



Below Query will display the records in descending order for CustName Column

SELECT * FROM Customer ORDER BY CustName DESC:
CustId CustName
2 Thanh
3 Peter
4 Eric
1 Cynthia







No comments:

Post a Comment