Tag Archives: learn SQL

100 Days of SQL

sql

Day 02 – SELECT DISTINCT Statement

The SQL SELECT DISTINCT statement is used to retrieve only unique values from a table. It is commonly used to find all the unique values in a particular column or set of columns in a table. The basic syntax of a SELECT DISTINCT statement is:

SELECT DISTINCT column1, column2, ...
FROM table_name;

In this syntax, column1, column2, etc. are the names of the columns that you want to retrieve unique values from, and table_name is the name of the table that you want to retrieve data from.

Here’s an example of a SELECT DISTINCT statement that retrieves all unique values from a column named “country” in a table named “customers”:

SELECT DISTINCT country FROM customers;

In this example, the SELECT DISTINCT statement is used to retrieve all the unique values from the “country” column in the “customers” table.

You can also use the SELECT DISTINCT statement with multiple columns to retrieve unique combinations of values from those columns. Here’s an example:

SELECT DISTINCT city, country FROM customers;

In this example, the SELECT DISTINCT statement is used to retrieve all unique combinations of the “city” and “country” columns from the “customers” table.

The SELECT DISTINCT statement is useful when you want to retrieve only unique values from a table, without retrieving any duplicates. It is commonly used to generate a list of all the unique values in a particular column, which can then be used for further analysis or processing.