EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial ORDER BY in MySQL
Secondary Sidebar
MySQL Tutorial
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL?Table?Size
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

ORDER BY in MySQL

By Priya PedamkarPriya Pedamkar

Order By in MySQL

Introduction to ORDER BY in MySQL

The following article provides an outline for ORDER BY in MySQL. In SQL, we use several kinds of language for different purposes like defining the table at the time of creation, manipulation in the table, fetching records from the table, etc.

These languages are segregated into following:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • DDL (Data Definition Language)
  • DML (Data Manipulation Language)
  • DQL (Data Query Language)
  • DCL (Data Control Language)
  • TCL (Transaction Control Language)

DQL is used for data retrieval purposes by using the SELECT command. With SELECT, we use many clauses for filtering out or for getting structural data. ORDER BY is one of the clauses used to sort out data in either ascending or descending order.

Syntax:

We are used to sort both numeric and string values either in ascending or descending manner. But by default, it sorts in ascending manner. If we want to descend, then we need to specify it after using the ORDER BY clause.

Code:

SELECT column1,column2,column3 FROM table_name
ORDER BY column1;>/code>

(This will sort column1 in ascending order)

Code:

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

(This will sort column1 in descending order)

How ORDER BY Works in MySQL?

Given below shows how ORDER BY works in MySQL:

Example:

Let’s create one customer table having the following attributes:

Cust_id First_name Last_name Contact Email City Amount
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1002 Virat Kohli 8752877855 [email protected] Delhi 60000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000
1005 Hardik Pandya 5237658726 [email protected] Cuttack 80000
1006 Krunal Pandya 8765385386 [email protected] Bangalore 50000
1007 Jasprit Bumrah 3875636763 [email protected] Chennai 78000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1009 Ajinkya Rahane 8637578988 [email protected] Bhubaneswar 87000
1010 Ravi Sashtri 9796767676 [email protected] Kolkata 56000
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000
1012 Bhubaneswar Kumar 9736738787 [email protected] Chandigarh 25000

As we have mentioned above, this order by clause can be applied both to numeric and string values. Here in this example id, contact and amount contain numeric values. First_name, last_name, email and city contain string values. We are going to apply ORDER BY clauses to the amount and first_name. Though contact and id contain numeric, and the email contains string values, sorting value on top of those attributes won’t make any sense.

In ascending and descending order “ORDER BY” will sort numerically from 1 to infinity and string values from A to Z and vice versa.

Example #1

Code:

SELECT * FROM customer
ORDER BY amount DESC;

It will show the output by sorting amount field in descending order

Output:

Cust_id First_name Last_name Contact Email City Amount
1009 Ajinkya Rahane 8637578988 [email protected] Bhubaneswar 87000
1005 Hardik Pandya 5237658726 [email protected] Cuttack 80000
1007 Jasprit Bumrah 3875636763 [email protected] Chennai 78000
1002 Virat Kohli 8752877855 [email protected] Delhi 60000
1010 Ravi Sashtri 9796767676 [email protected] Kolkata 56000
1006 Krunal Pandya 8765385386 [email protected] Bangalore 50000
1012 Bhubaneswar Kumar 9736738787 [email protected] Chandigarh 25000
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000

Example #2

Code:

SELECT * FROM customer
ORDER BY amount;

(If we don’t specify anything ascending or descending, then by default it will sort by ascending). It will show the output by sorting amount field in ascending order.

Output:

Cust_id First_name Last_name Contact Email City Amount
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000
1012 Bhubaneswar Kumar 9736738787 [email protected] Chandigarh 25000
1006 Krunal Pandya 8765385386 [email protected] Bangalore 50000
1010 Ravi Sashtri 9796767676 [email protected] Kolkata 56000
1002 Virat Kohli 8752877855 [email protected] Delhi 60000
1007 Jasprit Bumrah 3875636763 [email protected] Chennai 78000
1005 Hardik Pandya 5237658726 [email protected] Cuttack 80000
1009 Ajinkya Rahane 8637578988 [email protected] Bhubaneswar 87000

Example #3

Code:

SELECT * FROM customer
ORDER BY first_name;

(If we don’t specify anything ascending or descending, then by default it will sort by ascending).

It will show the output by sorting first_name field in ascending order.

Output:

Cust_id First_name Last_name Contact Email City Amount
1009 Ajinkya Rahane 8637578988 [email protected] Bhubaneswar 87000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1012 Bhubaneswar Kumar 9736738787 [email protected] Chandigarh 25000
1005 Hardik Pandya 5237658726 [email protected] Cuttack 80000
1007 Jasprit Bumrah 3875636763 [email protected] Chennai 78000
1006 Krunal Pandya 8765385386 [email protected] Bangalore 50000
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000
1010 Ravi Sashtri 9796767676 [email protected] Kolkata 56000
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1002 Virat Kohli 8752877855 [email protected] Delhi 60000
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000

(In above query we can see Ajinkya and Anil starts from A, but Ajinkya comes first then Anil, because when 1st letter are same, ORDER BY clause will sort it considering 2nd, 3rd,…. If other letters are same. Here J comes before N in Ajinkya and Anil respectively. That’s why it gives the output like this.)

Example #4

Code:

SELECT * FROM customer
ORDER BY first_name DESC;

It will show the output by sorting first_name field in descending order.

Output:

Cust_id First_name Last_name Contact Email City Amount
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000
1002 Virat Kohli 8752877855 [email protected] Delhi 60000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1010 Ravi Sashtri 9796767676 [email protected] Kolkata 56000
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000
1006 Krunal Pandya 8765385386 [email protected] Bangalore 50000
1007 Jasprit Bumrah 3875636763 [email protected] Chennai 78000
1005 Hardik Pandya 5237658726 [email protected] Cuttack 80000
1012 Bhubaneswar Kumar 9736738787 [email protected] Chandigarh 25000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1009 Ajinkya Rahane 8637578988 [email protected] Bhubaneswar 87000

(Here also same thing happened. As in Anil and Ajinkya, N comes before J, that’s why it came Anil came first then Ajinkya.)

There is another clause we use with ORDER BY, which is LIMIT.

Example #5

Code:

SELECT * FROM customer
ORDER BY first_name
LIMIT 5;

Output:

Cust_id First_name Last_name Contact Email City Amount
1011 Rahul Dravid 3836768787 [email protected] Mumbai 10000
1001 Rohit sharma 9876736587 [email protected] Mumbai 10000
1008 Anil Kumble 8735653786 [email protected] Kolkata 12000
1003 Sachin Tendulkar 9867868678 [email protected] Mumbai 15000
1004 Virendra Shewag 9087788988 [email protected] Delhi 20000

(It will only fetch the top 5 records as we have specified LIMIT = 5. )

Conclusion – ORDER BY in MySQL

Order by clause can be used among many other clauses like LIMIT, WHERE, GROUP BY, etc and this is also used in window functions. While working on real-time examples like top customers in the database or highest scorer in school, this ORDER BY clause helps a lot for sorting records as per our requirement.

Recommended Articles

This is a guide to the ORDER BY in MySQL. Here we discuss the introduction and how ORDER BY works in MySQL with query examples. You can also go through our other suggested articles to learn more –

  1. ORDER BY Clause in SQL
  2. Joins in MySQL
  3. MySQL Relational Database
  4. Introduction to MySQL
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more