• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

MySQL Tip: Use ORDER BY keyword to sort the result in ascending or descending order.

December 20, 2023

MySQL Tip: Use ORDER BY keyword to sort the result in ascending or descending order

MySQL is a popular open-source relational database management system that is widely used for various web applications. One of the essential features of MySQL is the ability to sort query results using the ORDER BY keyword. This powerful keyword allows you to sort the result set in either ascending or descending order based on one or more columns.

Understanding the ORDER BY keyword

The ORDER BY keyword is used in conjunction with the SELECT statement to sort the result set based on specified columns. By default, the sorting is done in ascending order, but you can also specify the DESC keyword to sort in descending order.

Here’s a basic syntax of the ORDER BY keyword:

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

Let’s dive deeper into how the ORDER BY keyword works and explore some examples.

Sorting in ascending order

When you use the ORDER BY keyword without specifying the sorting order, MySQL assumes it to be ascending. Ascending order means that the result set will be sorted from the lowest value to the highest value.

For example, let’s say we have a table called “employees” with columns “id,” “name,” and “salary.” To retrieve the employees’ names in ascending order based on their salaries, we can use the following query:

SELECT name, salary
FROM employees
ORDER BY salary;

This query will return the names and salaries of employees sorted in ascending order based on their salaries.

Sorting in descending order

If you want to sort the result set in descending order, you can use the DESC keyword after the column name in the ORDER BY clause.

Continuing with our previous example, if we want to retrieve the employees’ names in descending order based on their salaries, we can modify the query as follows:

SELECT name, salary
FROM employees
ORDER BY salary DESC;

This query will return the names and salaries of employees sorted in descending order based on their salaries.

Sorting by multiple columns

The ORDER BY keyword also allows you to sort the result set based on multiple columns. In such cases, the sorting is done based on the first column specified, and if there are any ties, it moves on to the next column.

Let’s consider a table called “products” with columns “name,” “category,” and “price.” To retrieve the products’ names in ascending order based on their category and then by price, we can use the following query:

SELECT name, category, price
FROM products
ORDER BY category, price;

This query will return the names, categories, and prices of products sorted first by category in ascending order and then by price in ascending order.

Summary

The ORDER BY keyword is a powerful tool in MySQL that allows you to sort query results in ascending or descending order based on one or more columns. By default, the sorting is done in ascending order, but you can specify the DESC keyword to sort in descending order. It is a valuable feature for organizing and presenting data in a meaningful way.

If you want to learn more about VPS hosting and how it can benefit your web applications, consider exploring Server.HK. They offer reliable and high-performance VPS hosting solutions tailored to your specific needs.

Recent Posts

  • Data Privacy Laws in Hong Kong: What VPS Users Need to Know
  • Hong Kong VPS Security Checklist: 10 Steps to Harden Your Server in 2026
  • NVMe SSD vs SATA SSD for VPS Hosting: Does Storage Type Really Matter?
  • Hong Kong VPS Docker Setup: Run Containers with Full Root Access
  • How to Set Up a Game Server on Hong Kong VPS: Low-Latency Gaming for Asia

Recent Comments

  1. doxycyklin on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  2. dapoxetine in usa on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. tadalafil tablets on Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  4. ivermectina tabletas on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. hello world on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot