Ordering Query Output By Two Fields

Ordering Query Output By Two Fields

Question:
I want to order the output of my query by two fields. Is this possible? That is, if two values are the same, I want the output ordered by a second field.

Answer:
The standard ORDER BY clause allows you to sort query output by one or more columns. For example, if you have a query to retrieve orders from the Northwind Orders table you can sort it by CustomerID and EmployeeID like this:

SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM OrdersORDER BY CustomerID, EmployeeID

With this result:

OrderID     CustomerID EmployeeID  OrderDate                   ----------- ---------- ----------- --------------------------- 10835       ALFKI      1           1998-01-15 00:00:00.00010952       ALFKI      1           1998-03-16 00:00:00.00011011       ALFKI      3           1998-04-09 00:00:00.00010692       ALFKI      4           1997-10-03 00:00:00.00010702       ALFKI      4           1997-10-13 00:00:00.00010643       ALFKI      6           1997-08-25 00:00:00.00010625       ANATR      3           1997-08-08 00:00:00.00010759       ANATR      3           1997-11-28 00:00:00.000

By default each ORDER BY expression sorts the results in ascending order. But you can specify the direction of the sort for each expression separately like this:

SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM OrdersORDER BY CustomerID ASC, EmployeeID DESC

With this result:

 OrderID     CustomerID EmployeeID  OrderDate                   ----------- ---------- ----------- --------------------------- 10643       ALFKI      6           1997-08-25 00:00:00.00010692       ALFKI      4           1997-10-03 00:00:00.00010702       ALFKI      4           1997-10-13 00:00:00.00011011       ALFKI      3           1998-04-09 00:00:00.00010835       ALFKI      1           1998-01-15 00:00:00.00010952       ALFKI      1           1998-03-16 00:00:00.00010308       ANATR      7           1996-09-18 00:00:00.00010926       ANATR      4           1998-03-04 00:00:00.000

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes