The ORDER BY clause in SQL is used to sort the result set of a SELECT statement based on one or more columns. It allows you to specify the column(s) by which you want to sort the data and the order in which you want the sorting to be performed. The basic syntax of the ORDER BY clause is as follows:
sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Here, "column1, column2, ..." represents the column(s) by which you want to sort the data. You can specify multiple columns to create a hierarchical sorting order. The ASC keyword is used for ascending order (default), and the DESC keyword is used for descending order.
1. Sort the records in ascending order based on the "name" column:
sql
SELECT *
FROM employees
ORDER BY name ASC;
2. Sort the records in descending order based on the "salary" column:
sql
SELECT *
FROM employees
ORDER BY salary DESC;
3. Sort the records first by the "department" column in ascending order, and then by the "age" column in descending order:
sql
SELECT *
FROM employees
ORDER BY department ASC, age DESC;
4. Sort the records by the "name" column in ascending order, ignoring the case (case-insensitive):
sql
SELECT *
FROM employees
ORDER BY name COLLATE SQL_Latin1_General_CP1_CI_AS ASC;
In the above examples, you can replace "table_name" with the actual name of your table and adjust the column names according to your specific database structure and requirements.
Remember that the ORDER BY clause is typically used at the end of a SELECT statement to sort the result set based on the specified columns.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc