Retrieving Unique Rows

The last element we'll learn about today is the DISTINCT keyword. DISTINCT asks SQL Server to remove all duplicate rows from your result set.

Most of the time, you won't need to use DISTINCT. Sometimes, when you think you need DISTINCT you really have a problem somewhere else in your code. But there are legitimate situations where duplicate rows can emerge from a table. In the next pages, we'll learn how to use DISTINCT and how to avoid it.

Using DISTINCT to Find Unique Rows

The keyword DISTINCT is placed optionally between SELECT and the select list. When you include DISTINCT, the server removes duplicate rows from your results.

What if you wanted a list of countries where you have suppliers? Consider this query:

 select ...

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.