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 System Data Collection Sets Predefined Inside SQL Se ...

How to Show Line Numbers in MS SQL Server ?

Using MS SQL Order By

Getting the Id of MS SQL Server Last Entered Record

What is the Bookmark Lookup and RID Lookup in SQL Server ?