DISTINCT
DISTINCT is a keyword in SQL that is used to remove duplicates in the result set of a SELECT statement. It only allows unique values to be returned, ensuring the elimination of any duplicate entries.
Example
SELECT DISTINCT countryFROM customers;
Output
+-----------+| country |+-----------+| USA || Germany || France |+-----------+
Explanation
The DISTINCT
keyword in SQL is used to return only distinct (unique) values. In the example above, SELECT DISTINCT country FROM customers
returns only unique country names from the ‘customers’ table.
Example
SELECT DISTINCT column_nameFROM table_name;
Output
column_name-------------- value1 value2 value3
Explanation
DISTINCT
keyword in SQL is used to return unique values in the output. It removes all the duplicate records and only shows distinct (unique) records. In the provided example, unique values from the specified column in the table are returned.
Example
SELECT DISTINCT LastNameFROM Employees;
Output
LastName--------JohnsonSmithBrown
Explanation
The DISTINCT keyword is used in SELECT statements to return unique values in the output. In the example above, the SQL returns only unique last names from the “Employees” table.
Example
SELECT DISTINCT department_idFROM employees;
Output
DEPARTMENT_ID-------------102030405060708090100110120130140150
Explanation
In the provided example, the SELECT DISTINCT
statement is used to return unique department IDs from the ‘employees’ table, thus eliminating duplicate entries. The result is a list of distinct or unique ‘department_id’s.
Example
SELECT DISTINCT Country FROM Customers;
Output
Country----------GermanyMexicoUKCanadaUSAFrance
Explanation
The DISTINCT keyword in SQL is used to return only distinct (different) values. In the example above, from the ‘Customers’ table, it returns all distinct countries present in the ‘Country’ column.