Select Distinct

Select Distinct a way to tell SQL Server to not return duplicate rows. Adding the distinct keyword makes it an expensive query because it essentially performs a group by for all of the returned columns. That being the case, it should only optimally be used on queries that return a small number of rows, or small number of columns.

When using Select Distinct, you should keep in mind that this is generally a workaround to either improper joining of tables, or an improper where clause that is not limiting the result set to only returning one instance of a record.

In order to utilize distinct, all you need to do, is place it before the select list.

[cc lang=”sql”]
SELECT DISTINCT *
FROM yourtable
[/cc]

This will surely only return a unique instance of every record in the result.

2 comments
Mike Pindrik 09 Mar 2012 at 9:25 am

To analyze if a view returns diplicate rows, compare

SELECT COUNT(*)
FROM

against:

SELECT COUNT(*)
FROM
(
SELECT DISTINCT FROM
) abc

Featured Articles

 Site Author

  • Thanks for visiting!
css.php