In SQL Server, the
DISTINCT keyword is used to eliminate duplicate values from a result set. It is often used in combination with the SELECT statement to retrieve unique values from one or more columns in a table.
The syntax for using
DISTINCT in SQL Server is as follows:
SELECT DISTINCT column1, column2, ...
Here, column1, column2, etc. are the names of the columns for which you want to retrieve unique values, and table_name is the name of the table from which you want to retrieve the values.
For example, if you have a table called "customers" with columns "customer_id", "customer_name", "city", and "state", and you want to retrieve a list of unique city names from the table, you could use the following SQL query:
SELECT DISTINCT city
This query would return a list of all unique city names from the "customers" table.
It is important to note that the DISTINCT keyword only applies to the columns specified in the SELECT statement. If you select additional columns along with the DISTINCT column, the query will return all unique combinations of values for those columns. For example, the following query would return all unique combinations of city and state:
SELECT DISTINCT city, state
In summary, the
DISTINCT keyword in SQL Server is a powerful tool for eliminating duplicate values from a result set. It can be used to retrieve unique values from one or more columns in a table and is often used in combination with the SELECT statement.