ORDER BY sorts rows. GROUP BY groups them. Here's how to use both effectively.
If you’re learning SQL, two clauses you’ll encounter frequently are ORDER BY
and GROUP BY
. While they may look similar at first glance, they serve very different purposes. In this beginner-friendly guide, we’ll break down what each clause does, how they’re used, and when to use one over the other.
By the end of this article, you'll be confident using ORDER BY
and GROUP BY
to sort and summarize your data effectively.
The ORDER BY
clause is used to sort the results of a query in ascending or descending order based on one or more columns.
SELECT * FROM employees
ORDER BY last_name ASC;
This will return all rows from the employees
table, sorted alphabetically by last_name
.
You can sort by multiple columns:
SELECT * FROM employees
ORDER BY department ASC, salary DESC;
This query sorts first by department, and within each department, it sorts employees by salary in descending order.
Use ORDER BY
when you want to control the display order of your query results.
The GROUP BY
clause is used to aggregate rows into groups based on the values in one or more columns. It’s often used with functions like COUNT()
, SUM()
, AVG()
, MAX()
, and MIN()
.
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
This query counts how many employees are in each department.
You can group by more than one column:
SELECT department, job_title, AVG(salary)
FROM employees
GROUP BY department, job_title;
This returns the average salary for each combination of department and job title.
Use GROUP BY
when you want to summarize or aggregate data across multiple rows.
ORDER BY
: Sorts the final result set.GROUP BY
: Combines rows into summary rows.ORDER BY
: Any query, regardless of aggregation.GROUP BY
: Almost always paired with aggregate functions.ORDER BY
: Same number of rows as input.GROUP BY
: Fewer rows, based on the number of groups.In many queries, you’ll use both clauses. GROUP BY
processes the aggregation, and ORDER BY
sorts the aggregated result.
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department
ORDER BY employee_count DESC;
This groups employees by department and shows the number of employees in each, sorted from most to fewest.
Want to test a query like this yourself? Try it in the Galaxy SQL Editor—a free, fast, and AI-powered way to explore your data.
ORDER BY
when:created_at
timestamp.Check out Galaxy’s Top Data Jobs page for a real example where job listings are sorted using ORDER BY
behind the scenes.
GROUP BY
when:For more insights on common mistakes people make while grouping, you might find our Common SQL Errors article helpful.
GROUP BY
clause.GROUP BY
doesn’t sort by default—add ORDER BY
to control display order.Understanding the difference between ORDER BY
and GROUP BY
is essential for writing effective SQL queries. While they’re often used together, their functions are very different: one is about how your results are presented, and the other is about how your data is aggregated.
When in doubt, ask yourself: Do I want to sort data? Use ORDER BY
. Do I want to summarize data? Use GROUP BY
.
For more hands-on SQL practice and modern query tooling, check out Galaxy—a fast, AI-enabled SQL editor built for developers and analysts.
Continue learning: