SELECT DISTINCT with ORDER BY Clause

In this example, we are going to display all the distinct data from multiple columns of our table in descending order. We will use ORDER BY Clause along with DESC keyword to achieve this task.

Query:

SELECT DISTINCT score, course
FROM w3wiki
ORDER BY score DESC;

Output:

SELECT DISTINCT with ORDER BY Clause

Explanation: In the above image, we can clearly see that all the data in the result table are unique/ distinct. We can notice that the data is sorted in descending order with respect to score column. We can see that score 150 comes first, followed by 100 then 50.

How to SELECT DISTINCT on Multiple Columns in SQL?

In the world of databases, data duplication can lead to confusion and inefficiency. SQL provides a powerful tool, SELECT DISTINCT, to retrieve unique values from columns. However, when dealing with multiple columns, the approach becomes more detailed.

In this article, We will explore How to SELECT DISTINCT on multiple columns in SQL by understanding various methods along with the practical implementation and so on.

Similar Reads

How to Get Distinct Values From Multiple Columns in SQL?

When working with SQL databases, it’s common to encounter scenarios where we need to retrieve unique combinations of values from multiple columns. This is where the SELECT DISTINCT statement comes into play. Below are methods that help us to SELECT DISTINCT on multiple columns in SQL...

Creating a demo table in our database

To understand How to SELECT DISTINCT on multiple columns in SQL we need a table on which we will perform various operations and queries. Here we will consider a table called geeksforgeeks which contains id, name, score, and course as Columns....

1. SELECT DISTINCT without WHERE Clause

In this example, we are going to implement SELECT DISTINCT statement for multiple values but without using WHERE clause. We will explore each and every data of the table....

2. SELECT DISTINCT with WHERE Clause

In this method, we are going to perform similar kind of operation as we have done in ‘method 1‘ but this time we will work with some specified data. We will use WHERE clause along with the SELECT DISTINCT statement....

3. SELECT DISTINCT with ORDER BY Clause

In this example, we are going to display all the distinct data from multiple columns of our table in descending order. We will use ORDER BY Clause along with DESC keyword to achieve this task....

4. SELECT DISTINCT with COUNT() and GROUP BY Clause

In the above example, we will count distinct values considering two of the columns of the table. We will use GROUP BY clause and COUNT() function....

Conclusion

Overall, the SELECT DISTINCT statement in SQL is a powerful tool for retrieving unique combinations of values from multiple columns. By understanding the various approaches and strategies outlined in this article, you can effectively use SELECT DISTINCT on multiple columns in SQL to streamline your data querying processes and eliminate duplicate data....