Welcome to SQL world. The SQL is very useful in programming. The sql queries helps in developers. There are different types of keyword, joins, sub-queries and commands are available.
The DISTINCT keyword is used for remove the duplicate values from database table in SELECT query. The SELECT DISTINCT statement is used to return only different values.
In database table, the column may contain duplicate values; and if you want to display only unique values or different values. For this process we use the distinct values in SQL SELECT query.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name FROM table_name;
SELECT DISTINCT expressions FROM tables WHERE conditions;
expressions: The columns or calculations that you wish to retrieve.
tables:The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause and we can use more than one table and field.
conditions:The conditions that must be met for the records to be selected.
SELECT DISTINCT employee_name FROM Employee;
EXAMPLE – WITH SINGLE FIELD
We can use the SQL DISTINCT in SELECT query to return a single field that removes the duplicates from the result set.
SELECT DISTINCT city FROM City_table;
This SQL DISTINCT example would return all unique city values from the City_table table.
EXAMPLE – WITH MULTIPLE FIELDS
SQL DISTINCT in SELECT query to remove duplicates from more than one field in your SQL SELECT statement.
SELECT DISTINCT city, state FROM City_table;
Return each unique city and state combination. In this case, the DISTINCT applies to each field listed after the DISTINCT keyword.