Geek Slack

Getting Started with SQL Server
About Lesson


SQL SELECT DISTINCT Statement


SQL SELECT DISTINCT Statement

The SELECT DISTINCT statement is used to return only distinct (different) values. It removes duplicate rows from the result set.

Basic SELECT DISTINCT Statement:

SELECT DISTINCT column1, column2
FROM table_name;

This query selects distinct values from the specified columns in the table.

SELECT DISTINCT with WHERE Clause:

SELECT DISTINCT country
FROM customers
WHERE age > 30;

This query selects distinct country values from the “customers” table where the age is greater than 30.

SELECT DISTINCT with ORDER BY Clause:

SELECT DISTINCT city
FROM customers
ORDER BY city ASC;

This query selects distinct city values from the “customers” table and orders the result by city in ascending order.

The SELECT DISTINCT statement is useful when you want to retrieve unique values from a column or combination of columns in a table.

Join the conversation