SQL Server > Query

Distinct (unique) in SQL Server

How to retrieve distinct (unique) record from SQL Server database?


To return distinct (unique) value of a particular column from a database table, we use DISTINCT keyword.

SELECT DISTINCT LastName FROM PersonalDetails

Above query will list records with unique LastName value from the PersonalDetails table.

In above case, the all 3 records have LastName as “Narayan”, only one unique value so only “Narayan” is listed as the result of the 2nd query.

Important

If we keep more than only column with the DISTINCT keywords, uniqueness is determined with the combined value of all those columns.

SELECT DISTINCT LastName, FirstName FROM PersonalDetails

In above query, all three records will be given as result because the combined value of LastName and FirstName are unique.

 Views: 8985 | Post Order: 45



Write for us






Hosting Recommendations