To get distinct values from a column in SQL Server, you can use the SELECT DISTINCT statement. This query will return all unique values from the specified column.
Here’s the basic syntax:
SELECT DISTINCT column_name
FROM table_name;
Example
Assume you have a table called Customers and you want to get all distinct values from the
City column:
SELECT DISTINCT City
FROM Customers;
This query will return a list of unique cities from the City column in the
Customers table.
Multiple Columns
If you want to get distinct combinations of values from multiple columns, you can include multiple columns in the
SELECT statement:
SELECT DISTINCT column1, column2
FROM table_name;
For example, to get distinct combinations of City and Country from the
Customers table:
SELECT DISTINCT City, Country
FROM Customers;
Ordering the Results
You can also order the distinct values using the ORDER BY clause:
SELECT DISTINCT City
FROM Customers
ORDER BY City;
This query will return the distinct cities in alphabetical order.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To get distinct values from a column in SQL Server, you can use the
SELECT DISTINCTstatement. This query will return all unique values from the specified column.Here’s the basic syntax:
Example
Assume you have a table called
Customersand you want to get all distinct values from theCitycolumn:This query will return a list of unique cities from the
Citycolumn in theCustomerstable.Multiple Columns
If you want to get distinct combinations of values from multiple columns, you can include multiple columns in the
SELECTstatement:For example, to get distinct combinations of
CityandCountryfrom theCustomerstable:Ordering the Results
You can also order the distinct values using the
ORDER BYclause:This query will return the distinct cities in alphabetical order.