SQL - Select Unique

To select unique values from a table in SQL, you can use the DISTINCT keyword in the SELECT statement. The DISTINCT keyword eliminates duplicate rows from the result set, returning only the unique values. Here's the syntax:

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

Here's an example to illustrate how to use the DISTINCT keyword:

Suppose you have a table named employees and If you want to select the unique values from the first_name column, you can use the DISTINCT keyword like this:

SELECT DISTINCT first_name
FROM employees;

The DISTINCT keyword ensures that only the unique values from the first_name column are returned in the result set.

You can also use the DISTINCT keyword with multiple columns to select unique combinations of values. For example:

SELECT DISTINCT first_name, last_name
FROM employees;

This query will return the unique combinations of first_name and last_name values from the employees table.

Remember that the DISTINCT keyword operates on all the specified columns together. It considers the combination of values in those columns to determine uniqueness.