Sorting data by more than one column is essential when working with structured datasets in Google Sheets. Whether you’re organizing employee records, sales logs, or event schedules, the QUERY function offers a powerful way to sort by primary and secondary columns, all within a single formula.
In this article, you’ll learn how to use the ORDER BY clause inside the QUERY function to sort by multiple columns in ascending or descending order. We’ll also cover how to apply conditional filters before sorting and show best practices to keep your formulas clean and flexible.
Steps to sort rows by multiple columns using the QUERY function in Google Sheets
➤ Use the dataset in cells A1:D11 with columns: Name, Department, Join Date, and Salary.
➤ To sort employees by Department (ascending) and then by Salary (descending), enter this formula in cell A13:
=QUERY(A1:D11, “SELECT A, B, C, D ORDER BY B ASC, D DESC”, 1)
➤ Press Enter to see employees grouped by department, with the highest earners on top.
Sort Rows by Multiple Columns Using QUERY ORDER BY
The QUERY function in Google Sheets provides a powerful way to sort data based on multiple columns using the ORDER BY clause. This method is especially useful when you need to organize information using a primary and secondary sort, for example, ordering employees first by department, and then by salary within each department.
You can specify the columns to sort and define the direction (ASC for ascending or DESC for descending) for each one independently. This approach is clean, dynamic, and works well for both static datasets and those updated regularly.
This is the sample dataset we will be using to demonstrate the methods:
Steps:
➤ Use the dataset in cells A1:D11 with columns: Name, Department, Join Date, and Salary.
➤ To sort employees by Department (ascending) and then by Salary (descending), enter the following QUERY formula in cell A13:
=QUERY(A1:D11, “SELECT A, B, C, D ORDER BY B ASC, D DESC”, 1)
➧ ORDER BY B ASC, D DESC: Sorts first by column B (Department) in ascending order, then by column D (Salary) in descending order.
➧ 1: Indicates that the first row contains headers.
➤ You can adjust the sort order by changing ASC or DESC, or sort by different columns such as Join Date or Name. For example, we will sort here by joining date in ASC order.
This is the formula we used to make this happen:
=QUERY(A1:D11, “SELECT A, B, C, D ORDER BY B ASC, C ASC”, 1)
This method is ideal for sorting datasets based on multiple columns without making changes to the original table.
Sort Specific Columns: Join Date and Salary
You don’t always need every column from your dataset. Sometimes you just want a simplified view that highlights key information. This method shows you how to use QUERY to sort by Join Date and Salary, and return only the relevant columns. It’s a clean way to display sorted personnel info without crowding your sheet.
Steps:
➤ To extract just the Name and Salary, sorted first by Join Date and then by Salary (both ascending), enter this formula in cell A13:
=QUERY(A2:D11, “SELECT A, D ORDER BY B ASC, D ASC”, 1)
➧ ORDER BY B ASC, D ASC: Sorts the data by Join Date (column B) and then by Salary (column D), both in ascending order.
➧ 1: Tells the function that the first row contains headers.
➤ Press Enter.
This view provides a simplified report of employees, sorted by the date they joined and their earnings, ideal for quick HR reviews or onboarding tracking.
Frequently Asked Questions
Can I sort by more than two columns using the QUERY function?
Yes! You can include multiple columns in the ORDER BY clause by separating them with commas. For example: ORDER BY B ASC, C DESC, D ASC will sort first by column B ascending, then column C descending, and finally column D ascending.
How do I sort by date columns correctly in QUERY?
Make sure your date column is formatted as a proper date in Google Sheets. The QUERY function will then sort dates chronologically when you use ORDER BY on the date column (e.g., ORDER BY B ASC).
Can I apply filters and sorts together using QUERY?
Absolutely. You can combine WHERE conditions with ORDER BY to filter your data before sorting it. For example: SELECT A, B, C WHERE C = ‘HR’ ORDER BY B ASC, D DESC.
Why does my QUERY formula not sort as expected?
Common issues include having filters active on the dataset, data formatting problems (like dates stored as text), or incorrect QUERY syntax. Try removing filters, verifying data types, and double-checking your formula.
Can I sort and display only specific columns using QUERY?
Yes, by specifying the columns in the SELECT clause, you can extract and sort just the data you want. For instance, SELECT A, D ORDER BY B ASC will show only columns A and D, sorted by column B.
Wrapping Up
The Google Sheets QUERY function is a versatile tool for sorting data by multiple columns efficiently. By mastering the ORDER BY clause, you can organize datasets dynamically by sorting with primary and secondary criteria like dates, names, or salaries, all within a single formula. Whether you need a full dataset or a simplified view, QUERY keeps your sheets clean and flexible. Practice these methods to streamline your data management and gain deeper insights effortlessly.