I need a query to get distinct values from the City column in the
Customers table. Any ideas?
SQL Query to Get Distinct Values from a Column in SQL Server
363
16-Jul-2024
Updated on 16-Jul-2024
Ravi Vishwakarma
16-Jul-2024To 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.