Using MS SQL DISTINCT

If we want to know how many different types of records are in the desired column or columns of the query written in MS Sql, we use the DISTINCT function.

Let's have a table called PRODUCTS as below.

Product Price
Chocolate 5
Water 5
Pasta 3
Pen 10
Chocolate 7
Tea 20

If we want to see how many different products are in this table. Using the DISTINCT function, we write a query like the one below.

SELECT DISTINCT(Product) FROM PRODUCTS

The result returned to us will be as follows.

Product
Chocolate
Water
Pasta
Pen
Tea

There are 2 records named Chocolate in the PRODUCTS table. Since we apply the DISTINCT function only to the Product column, it returns only one record, even if the prices are different.



You May Interest

What are The Different Backup Options With SQL Server ?

What is the Use of Dedicated Admin Connection ?

What is SQL Server Agent ?

MS SQL Show Only Date of Current Time

What is a Covered Index in SQL Server ?