What is the use of distinct in sql

SQL DISTINCT: The Complete Guide

What is the use of distinct in sql


what is the use of distinct in sql

Overview of the SQL Count Distinct Function

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values. The SQL DISTINCT command used along with the SELECT keyword retrieves only unique data entries depending on the column list you have specified after it. To illustrate the usage of the DISTINCT keyword, we'll use our Users table introduced in the previous chapters.

This article explores SQL Count Distinct operator for eliminating what does self opinionated mean duplicate rows in the result set. A developer needs to get data from a SQL table with multiple conditions.

Sometimes, we want to get all rows in a table but eliminate the available NULL values. Suppose we want to get distinct customer records that have placed an order last year.

We use SQL Count aggregate function to get the number of rows in the output. Suppose we have a product table that holds records for all products sold by a company. We want to know the count of products sold during the last quarter. It means that SQL Server counts all records in a table.

It also includes the rows having duplicate values as well. Suppose we want to know the distinct values available in the table. In the following output, we get only 2 rows. In this example, we have a location table that consists of two columns City and State. If we look at the data, we have similar city name present in a different state as well. The combination of city and state is unique, and we do not want that unique combination to be eliminated from the output. It checks for the combination of values and removes if the combination is not unique.

It does not remove the duplicate city names from the output because of a unique combination of values. We have 5 records in the location table. In the data, you can see we have one combination of city and state that is not unique.

You get the following error message. If we use a combination of columns to how to update the ps3 browser distinct values and any of the columns contain NULL values, it also becomes a unique combination for the SQL Server. We did not specify any state in this query. It returns the total number of rows after satisfying conditions specified in the where clause. It returns the distinct number of rows after satisfying conditions specified in the where clause.

Execute the query to get an Actual execution plan. In this execution plan, you can see top resource consuming operators:. You can hover the mouse over the sort operator, and it opens a tool-tip with the operator details. In the properties windows, also we get more details around the sort operator including memory allocation, statistics, and the number of rows.

In a table with million records, SQL Count Distinct might cause performance issues because a distinct count operator is a costly operator in the actual execution plan. This new function of SQL Server provides an approximate distinct count of the rows. I would suggest reviewing them as per your environment.

If you have any comments or questions, feel free to leave them in the comments below. Insert into location values 'Gurgaon''Haryana'.

Insert into location values 'Gurgaon''Rajasthan'. Insert into location values 'Jaipur''Rajasthan'. Insert into location values 'Jaipur''Haryana'.

FROM Location. Insert into location values 'Gurgaon'''. Insert into location city values 'Gurgaon'. Author Recent Posts. Rajendra Gupta. As an MCSA certified and Microsoft Certified Trainer in Gurgaon, India, with 13 years of experience, Rajendra works for a variety of large companies focusing on performance optimization, monitoring, high availability, and disaster recovery strategies and implementation. He is the creator of one of the biggest free online collections of articles on a single topic, with his part series on SQL Server Always On Availability Groups.

Raj is always interested in new challenges so if you need consulting help on any subject covered in his writings, he can be reached at rajendra. Latest posts by Rajendra Gupta see all. Related posts: What is causing database slowdowns? Insert into location values 'Gurgaon''' Insert into location city values 'Gurgaon'.

How to Use SQL SELECT DISTINCT Statement to Retrieve Unique Data

Oct 14,  · Distinct command in SQL is used along with the Select command in order to retrieve only distinct or dissimilar values from a table. It is mainly used as a method to filter duplicate records and fetch only unique records from a given table, where there is a possibility of fields having multiple duplicate records. Jul 24,  · DISTINCT is a keyword in SQL that allows you to show unique or distinct results. It’s added to a SELECT query to eliminate duplicates in the data it displays because columns often contain duplicate values and sometimes you may only want to show unique or distinct values. Code language: SQL (Structured Query Language) (sql) The query uses the combination of values in all specified columns in the SELECT list to evaluate the uniqueness. If you apply the DISTINCT clause to a column that has NULL, the DISTINCT clause .

By Priya Pedamkar. Distinct command in SQL is used along with the Select command in order to retrieve only distinct or dissimilar values from a table. It is mainly used as a method to filter duplicate records and fetch only unique records from a given table, where there is a possibility of fields having multiple duplicate records. On running, it will return the unique city names, which are Mumbai, Delhi, Lucknow. If we remove a distinct keyword, it will retrieve four values instead of three.

Below is the syntax for a distinct keyword. As we have already discussed the parameters. Now we will use DML statements to enter data in the table. We will get 5 values on executing the query as we have only five distinct states as Jharkhand is repeated twice. Below is the result set we should get on executing the query. The above query will return each unique city and state combination. In the above case, distinct applies to each field which is written after the distinct keyword. So we will have five pairs of city and state as there Jamshedpur city, which has been repeated twice.

So we will have Jamshedpur along with Jharkhand once. The city will be ordered in ascending order. The result set on executing the query is shown below. First of all, we will update a field in the state column as NULL and then use a distinct keyword to obtain the result set. This will insert a NULL value in the last field of the state column. The table will be updated as below. On executing the above query, we will set five values as the distinct keyword considers NULL also a unique value.

Jharkhand being repeated twice will only have one value in the result set. Below is the result set we should see on executing the above query. Here we discuss the introduction, how to use distinct keyword in SQL, and its Parameter and some Examples.

You may also look at the following articles to learn more —. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. Note: In SQL, a distinct keyword also retrieves the null value, which means it considers null also as a distinct value.

So if we have null values, then the result set for a distinct keyword will also include a null value. Popular Course in this category. Course Price View Course. Free Data Science Course. Login details for this Free course will be emailed to you.

Email ID. Contact No.



More articles in this category:
<- How to become a high school swim coach - How to play mahjong hong kong style->

Comment on post

2 to post “What is the use of distinct in sql

Add a comment

Your email will not be published. Required fields are marked *