ComputersDatabase

How is the SQL sorted?

At work with databases quite often there is a necessity to deduce result of request in the certain order, for example, under the alphabet. To do this, the DBMS has a special function in the SQL language - sorting. In this case, the programmer can choose which fields and in what order it will occur in order to achieve the desired result without applying serious programming skills.

What is sorting in the database?

Working with databases is constantly associated with a large amount of information, which must be determined by the order. Currently, there are a number of databases that have a wide range of functions, the most popular of which are Oracle and MS SQL. Sorting information as one of the main procedures in working with the database is provided by a special built-in function in each of them.

Ranking the data allows you to simplify the search process, and in some cases helps to solve some problems or optimize the program. SQL-sorting is performed on a separately selected field, and if necessary, if there are identical values in the elements of this field, you can specify additional parameters that determine the location of the rows.

Sort command

SQL-sorting in the database is provided by using the ORDER BY function. So, when displaying information from the database, after specifying the columns and tables from which the data will be read, you must specify the sort command in the query, and then determine the field or fields for sorting.

For example, if you need to get data from the Name and Age fields from the People table, and if you display the result in alphabetical order by the Name column, this will help you make the following query: SELECT Name, Age FROM People ORDER BY Name.

How do I set the sort order?

Modern conditions put different tasks for programmers, and sometimes it is necessary to determine in which order the result will be output - in descending order or ascending, alphabetically or in reverse order? And for this in the SQL language, the sort order is determined by adding a keyword to the query. After selecting fields and tables from which the desired information will be obtained, you must add ORDER BY, and then specify the name of the column on which you want to sort.

To obtain the reverse order, you must specify DESC after the name. In the event that you order the elements by two or more criteria, the columns are indicated with a comma, and the ranking priority will be given to the field that comes first in the list. It should be noted that the alignment of elements in the reverse order of the DESC parameter provides for only one field, after the name of which this keyword is indicated, so if necessary, it must be specified in all selected columns.

Alternative sorting methods

If there is no possibility to use the built-in SQL-sort function, you can write one of the known algorithms. If it is necessary to achieve the fastest ordering of the elements, it is worthwhile to apply the "Quick Sort" method , based on the half-breaking of the array of elements. Bubble ranking methods are also very popular, where two adjacent elements change places in case of an incorrect location, a "pyramidal sorting" that sends the largest element to the end of the list, and "insertion sorting", which determines the location of each element in turn.

The handwriting of the algorithm will not significantly increase the sorting speed, however, it will contribute to the development of programming skills, and also will allow to modify the process by adjusting the ranking scheme to a certain database to increase the program's efficiency.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.