1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

MySQL ORDER BY Clause guide with practical examples

  • SciTools
  • November 22, 2023 at 6:40 PM
  • 709 times read
Contents [hideshow]
  1. Understanding the ORDER BY Clause
  2. Basic Syntax
  3. 1. Sorting in Ascending Order
    1. Example: Single Column Sort
    2. Example: Multiple Column Sort
  4. 2. Sorting in Descending Order
    1. Example: Single Column Descending
    2. Example: Mixed Order Sorting
  5. 3. Using ORDER BY with Aggregated Data
    1. Example: Grouped Sorting
  6. 4. ORDER BY with JOIN Statements
    1. Example: Sorting Joined Tables
  7. 5. Sorting by Column Position
    1. Example: Position-Based Sorting

Sorting data is a common requirement in database management, and MySQL's ORDER BY clause is the tool designed for this task. This guide is tailored for beginners and provides a clear understanding of how the ORDER BY clause works through practical examples.

Understanding the ORDER BY Clause

The ORDER BY clause in MySQL is used to sort the result set of a query by one or more columns. You can sort the data in ascending order (using ASC) or descending order (using DESC).

Basic Syntax

The basic structure of an ORDER BY clause in a SQL statement is:

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

If you don't specify ASC or DESC, MySQL defaults to ascending order.

1. Sorting in Ascending Order

Example: Single Column Sort

To sort users by their names in ascending order:

SQL
SELECT * FROM Users
ORDER BY Name;

Example: Multiple Column Sort

To sort orders first by date and then by the order amount:

SQL
SELECT * FROM Orders
ORDER BY OrderDate, Amount;

2. Sorting in Descending Order

Example: Single Column Descending

To sort products by price in descending order:

SQL
SELECT * FROM Products
ORDER BY Price DESC;

Example: Mixed Order Sorting

To sort users by age in descending order and then by name in ascending order:

SQL
SELECT * FROM Users
ORDER BY Age DESC, Name ASC;

3. Using ORDER BY with Aggregated Data

Example: Grouped Sorting

To display the total sales per customer and sort by the total sales:

SQL
SELECT CustomerID, SUM(Sales) AS TotalSales
FROM Orders
GROUP BY CustomerID
ORDER BY TotalSales DESC;

4. ORDER BY with JOIN Statements

Example: Sorting Joined Tables

If you have a Users table and an Orders table, to list users and their order counts, sorted by the order count:

SQL
SELECT Users.Name, COUNT(Orders.OrderID) AS OrderCount
FROM Users
JOIN Orders ON Users.UserID = Orders.UserID
GROUP BY Users.Name
ORDER BY OrderCount DESC;

5. Sorting by Column Position

Example: Position-Based Sorting

To sort by the first column in ascending order:

SQL
SELECT Name, Age FROM Users
ORDER BY 1;

Here, 1 refers to the first column (Name).

  • Previous Article Complete MySQL WHERE Clause guide with examples
  • Next Article Guide to GROUP BY and HAVING clauses in MySQL with examples

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)

Tags

  • mysql
  • ORDER BY Clause
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2025. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™