SQL ORDER BY
Presentation
Begin
Rules
A SQL Server result-set is a factor of the manner by which the information was gone into the table. Sometimes, the information seemed, by all accounts, to be in arranged request yet this doesn't imply that information entered and information put away in the hard-drive are same
SQL Server doesn't ensure the request for the outcome set. It's autonomous of the request except if we characterize it expressly on how we need those records arranged. The best way to change the request in which the outcomes show up is to utilize the SQL Order by provision
The SQL Order by condition isn't substantial for in-line works, sees, determined tables, and sub-questions, except if it is indicated with SQL TOP or OFFSET and FETCH provisos
Having limitations, grouped file, character esteems or arrangements doesn't ensure the requesting of the outcomes. Once more, on the off chance that you get a yield in the arranged request, at that point it is only an occurrence of the way that information entered in the table in a similar request of the arranging of the information.
A SQL inquiry that utilizations set administrators, for example, UNION, EXCEPT, or INTERSECT, SQL Order by is permitted distinctly toward the finish of the assertion
Models
In this segment, we will see a couple of instances of SQL, so we can have a superior comprehension of how we're really functioning with SQL Order by provision. So how about we begin with not many examples.
Instructions to utilize the SQL Order by provision in a SQL inquiry
In the accompanying model we will run the example SQL without utilizing the SQL Order by statement. The yield is an unmistakable sign that when we run a question in SQL Server, the inquiry enhancer takes a gander at the information demand and creates an inquiry plan and returns the records from the table or tables, and it depends on the inquiry and furthermore it depends on how the information is genuinely coordinated on the drive.
Syntax :
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example :
SELECT * FROM Customers
ORDER BY Country;
2
SELECT * FROM Customers
ORDER BY Country DESC;
3
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
ASC | DESC
Second, use ASC or DESC to specify whether the values in the specified column should be sorted in the ascending or relative order.
The CSA sorts the result of the value in addition to low to the value in addition to high while the DESC sorts the results set from the value in addition to high to the value in addition to low.
If you do not explicitly specify ASC or DESC, SQL Server uses ASC as a standard sorting order default. In addition, SQL Server treats NULL as the in addition to low values.
When processing the SELECT statement, which includes an ORDER BY provision, the Provisos ORDER BY is the latest statement to be processed.
Example of a table
Name |
| |
---|---|---|
Rita | 12 | |
Alvaro | 19 | |
Serge | 13 | |
Valérie | 17 | |
Monica | 11 | |
Rita | 15 | |
Jean | 10 | |
Diane | 16 | |
Adrien | 11 | |
Daniel | 17 | |
Daniel | 18 | |
Marinah | 14 | |
Paco | 19 |
TEST ASC
SELECT Name , Note FROM student
ORDER BY Note ASC;
Name |
|
---|
Jean | 10 |
Adrien | 11 |
Monica | 11 |
Rita | 12 |
Serge | 13 |
Marinah | 14 |
Rita | 15 |
Diane | 16 |
Valérie | 17 |
Daniel | 17 |
Daniel | 19 |
Alvaro | 19 |
Paco | 19 |
TEST DESC
SELECT Name , Note FROM student
ORDER BY Note DESC;
Paco | 19 |
Adrien | 19 |
Daniel | 19 |
Daniel | 17 |
Valérie | 17 |
Marinah | 16 |
Diane | 15 |
Rita | 15 |
Marinah | 14 |
Serge | 14 |
Rita | 13 |
Monica | 12 |
Adrien | 11 |
0 Comments