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
13916-Jul-2024
Updated on 16-Jul-2024
Home / DeveloperSection / Forums / SQL Query to Get Distinct Values from a Column in SQL Server
I need a query to get distinct values from the City
column in the Customers
table. Any ideas?
Ravi Vishwakarma
16-Jul-2024To 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:
Example
Assume you have a table called
Customers
and you want to get all distinct values from theCity
column:This query will return a list of unique cities from the
City
column in theCustomers
table.Multiple Columns
If you want to get distinct combinations of values from multiple columns, you can include multiple columns in the
SELECT
statement:For example, to get distinct combinations of
City
andCountry
from theCustomers
table:Ordering the Results
You can also order the distinct values using the
ORDER BY
clause:This query will return the distinct cities in alphabetical order.