---
title: "Distinct Vs Group By: Optimal Method for Sorting Table Rows"
description: "You can sort rows in SQL. Here are the methods that explain how to sort using District and Group by and advantages."
url: https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/
date: 2023-10-26
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/03/pexels-photo-10375936.jpeg?fit=1880%2C1253&ssl=1
categories: ["SQL"]
tags: ["SQL sorting"]
type: post
lang: en
---

# Distinct Vs Group By: Optimal Method for Sorting Table Rows

Sorting rows in SQL helps retrieve older or newer entries. Instead of using ORDER BY, you can use DISTINCT or [GROUP BY](https://srinimf.com/2014/09/18/db2-sql-distinct-and-group-by/). The notes below provide a better approach. [Tuning the SQL query](https://srinimf.com/2021/05/15/db2-5-effective-ways-to-tune-sql-queries/) is useful for easing the load on the RDBMS server.

Related[SQL Count Vs Count Distinct With Group By](https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/)

![Sorting Table rows.](https://i0.wp.com/srinimf.com/wp-content/uploads/2022/03/pexels-photo-7573282.jpeg?resize=1880%2C1254&ssl=1)*Photo by Mikhail Nilov on Pexels.com*

ON THIS PAGE

1. [Group By Clause](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/#group-by-clause)
2. [DISTINCT Clause](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/#distinct-clause)

## Group By Clause

ORDER BY Clause

When you use the ORDER BY clause, the column name on which you are ordering (Ascending order is default) should not be used in the SELECT statement.

Here is an example:

```
SELECT emp_no, last_name, first_name 
FROM emp 
ORDER BY work_dept;
```

Here, the `work_dept` is used for ordering. So, it is not given in the SELECT. You had better limit the number of columns participating in the sort. Otherwise, sorting takes longer.

Grouping is another way of sorting helpful when you want to return aggregate data. Consider the following query:

Here’s an example:

```
SELECT work_dept, SUM(salary) 
FROM emp 
GROUP BY work_dept;
```

In the output, you’ll find, the work_dept and its aggregate sum of salaries of all the employees in the work_dept. [The order of the work_dept is random since it does not use the ORDER BY clause.](https://srinimf.com/2018/10/23/ordering-sql-query-results-using-desc-vs-asc/)

## DISTINCT Clause

The DISTINCT internally does sort, even if there are no [duplicate rows](https://srinimf.com/2015/01/27/sql-eliminating-duplicates-in-a-table/). Remember, overhead is high for the [DISTINCT clause](https://srinimf.com/2016/03/24/2-key-differences-of-count-and-countdistinct-db2-v11/). But this is not a recommendation to never use it. There will be valid reasons for doing so.

Having clause

Additionally, the HAVING clause eliminates departments that are of no use.

Here’s an example:

```
SELECT work_dept, SUM(salary)   
FROM emp    
GROUP BY work_dept    
HAVING AVG(salary) < 5000;
```

Conclusion

In summary, the Group By is more efficient **than** the Distinct clause for sorting rows.

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F10%2F26%2Fhow-to-sort-rows-in-sql-shortcuts-the-easy-way%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F03%2Fpexels-photo-10375936.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Distinct%20Vs%20Group%20By%3A%20Optimal%20Method%20for%20Sorting%20Table%20Rows)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/10/26/how-to-sort-rows-in-sql-shortcuts-the-easy-way/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Distinct%20Vs%20Group%20By%3A%20Optimal%20Method%20for%20Sorting%20Table%20Rows&body=https%3A%2F%2Fsrinimf.com%2F2023%2F10%2F26%2Fhow-to-sort-rows-in-sql-shortcuts-the-easy-way%2F&share=email)
-

### Like this:

Like Loading…
