|
In MySQL, the ORDER BY Clause is used to sort the result set either in ascending order or descending order. By default, the ORDER BY sorts the displayed data in ascending order. If you want your data displayed in descending order we need to use the DESC keyword along with the ORDER BY Clause. To make any analysis, sorted data can save much time and that can be achieved by ORDER BY Clause. It is very useful in organizing displayed data in a very meaningful way. ORDER BY ClauseThe ORDER BY Clause in MySQL is a powerful tool that allows you to sort the result set of a query in ascending or descending order based on one or more columns. It is an essential part of querying databases when you want to retrieve data in a specific order. In this article, we will explore the syntax and usage of the MySQL ORDER BY Keyword. Syntax: SELECT column1, column2, ... Where,
Demo MySQL DatabaseWe will be using the following MySQL table for our examples on ORDER BY. table – horje To use this table on your system, write the following MySQL queries: Create Table: CREATE TABLE horje( Insert the value in horje Table: INSERT INTO horje(user_id,name,rank,courses_enrolled,questions_solved) ORDER BY Clause ExamplesNow let’s look at some examples of the ORDER BY clause, and understand its workings in different scenarios. Example 1: ORDER BY CLAUSE Using ASC/DESC AttributeWe can use the ASC attribute to sort in ascending order and the DESC attribute to sort in descending order. These are both very useful attributes of the ORDER BY clause.
CASE 1: Let’s sort the displayed data in the table in ascending order for the “courses enrolled” column, but for this time we are going to use theĀ ASC keyword along with the ORDER BY clause. Query:SELECT * from Output: Result – CASE 01 Explanation: The query retrieves all columns from the
CASE 2: Let’s sort the displayed data with respect to the courses enrolled column but this time we are displaying the data in descending order. QuerySELECT * FROM Output:Result – CASE 02 Explanation: The query retrieves all columns from the Example 2: ORDER BY CLAUSE With Multiple ColumnsIn this example, we are going to implement ORDER BY clause in multiple columns in a single query. Before implementing this we will add some more data in our table with duplicate ranks for a clear understanding of how this will work with multiple columns. Query:INSERT INTO horje(user_id,name,rank,courses_enrolled,questions_solved) Now let’s implement our query of the ORDER BY clause in multiple columns Query:SELECT * FROM horje Output:Output – Order By in Multiple Columns Here sorting of data displayed is done on a priority basis. Let’s see how this works
We can conclude this, the first column which is mentioned, after the ORDER BY clause gets higher priority than the next mentioned column, and so on.. Example 3: ORDER BY CLAUSE With NULL VALUESTo implement this example we need to add some NULL values in the rank column. Lets update rank column values to NULL for user id = ‘ayush105’ or user id = ‘harsh05’. We will use the UPDATE statement to achieve this task. Query: UPDATE horje Now let’s display our table values in ascending order with respect to the rank column. Query:SELECT * Output:Output – ORDER BY NULL Values Explanation: In MYSQL, NULL values are considered lower than any other non-NULL values. In the above example, we can observe that all the rows with NULL values in their rank column appeared first followed by non-null values in ascending order. ConclusionThe MySQL ORDER BY clause is used to sort the result set by one or more columns in ascending (ASC) or descending (DESC) order. It helps organize data in a meaningful way, making it easier to analyze and understand. Proper use of the ORDER BY clause enhances data retrieval efficiency and readability. FAQs on MySQL ORDER BY ClauseWhat is the MySQL
|
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
|
|
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |