SQL SORT FUNCTION | LEARN SQL

HEY GIRUILLIANS WELCOME BACK TO OUR SQL SERIES WE HAVE POSTED 4 BLOGS ABOUT AND THIS IS THE 6TH IF YOU ARE NEW PLEASE READ PREVIOUS BLOGS TO LEARN WITH FLOW LET’S START.

IN THE PREVIOUS CLASS WE HAD LEARNT ABOUT

1.SQL CLAUSES:
  • SQL WHERE
  • SQL AND
  • SQL OR
  • SQL AS
  • SQL HAVING
TODAY WE WILL LEARN ABOUT

A)SQL ORDER BY FUNCTIONS:
  • ORDER BY CLAUSE
  • ORDER BY ASC
  • ORDER BY DESC
  • ORDER BY RANDOM
  • ORDER BY MULTIPLE COLUMNS
SO GUYS LET’S START

QUES. WHAT IS SQL ORDER BY FUNCTIONS?

ANS. The result set can be sorted ascending or descending using the ORDER BY keyword.

NOTE: Records are sorted in ascending order by default when the ORDER BY keyword is used. Use the DESC keyword to sort the records so that they are in descending order.

Here, you specify the columns you want to retrieve from the table in the SELECT clause and then use the ORDER BY clause to limit the columns you want to sort the result set. You can also specify the sorting order for each column using the optional ASC (ascending) or DESC (descending) keyword. By default, if you do not specify an order, it will be sorted in ascending order.

a)ORDER BY CLAUSE

The result set of a SELECT query can be sorted in ascending or descending order using one or more columns by using the ORDER BY clause in SQL. The following is how to format the ORDER BY clause:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

LET TABLE:-GUERILLATECK


FOR EXAMPLE:


Note that ASC is the default sorting order if you don't specify either ASC or DESC keyword in the ORDER BY clause.

OUTPUT:-


IT WILL SORT NAMES BY ASCENDING ORDER

b) Order By Ascending:-

ORDER BY ASC is used in SQL to sort the result set of a SELECT query in ascending order. When you use ASC in the ORDER BY clause, it sorts the result set from the lowest value to the highest value based on the specified column or expression.

The basic syntax of using ORDER BY ASC is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC, column2 ASC, ...;

Here, you specify the columns you want to retrieve from the table in the SELECT clause and then use the ORDER BY clause to limit the columns you want to sort the result set. You can also specify the sorting order for each column using the ASC keyword.

For example:-


Output:-

This will return all employees in the table, sorted by their salary in ascending order. If two or more employees have the same salary, they will be sorted based on their EmployeeID in ascending order as well.

Note that ASC is the default sorting order if you don't specify either ASC or DESC keyword in the ORDER BY clause.


c) Order By Descending

ORDER BY DESC is used in SQL to sort the result set of a SELECT query in descending order. When you use DESC in the ORDER BY clause, it sorts the result set from the highest value to the lowest value based on the specified column or expression.

The basic syntax of using ORDER BY DESC is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 DESC, column2 DESC, ...;

Here, you specify the columns you want to retrieve from the table in the SELECT clause and then use the ORDER BY clause to limit the columns you want to sort the result set. You can also specify the sorting order for each column using the DESC keyword.



Output:-

This will return all entries in the table, sorted by their salary in descending order. If two or more employees have the same salary, they will be sorted based on their names in ascending order.

Note that DESC is the opposite sorting order of ASC, and it reverses the sorting direction of the specified column or expression.


d) ORDER BY RANDOM

To order the result set in a random order, you can use the RAND() function in the ORDER BY clause of a SQL SELECT statement. The RAND() function generates a random number for each row in the result set, and then the rows are sorted based on these random numbers.

The basic syntax for using ORDER BY RAND() is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY RAND();

THIS FUNCTION NOT WORK IN THE LATEST VERSIONS

Here, you specify the columns that you want to retrieve from the table in the SELECT clause, and then you use the ORDER BY clause to order the result set in a random order using the RAND() function

e) Order by multiple columns

You can sort the result set by multiple columns in SQL by specifying each column in the ORDER BY clause. The sorting order for each column can be specified separately using the ASC or DESC keyword.

The basic syntax of using ORDER BY with multiple columns is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

For example:-


Output:-

This will return all entries in the table, sorted by name in ascending order first and then by class in descending order within each department. If two or more employees have the same salary within a department, they will be sorted based on their id in ascending order.






So that’s it for today guys if you want to learn more, so follow us...

No comments:

Post a Comment