DISTINCT
Example
Section titled “Example”SELECT DISTINCT countryFROM customers;Output
Section titled “Output”+-----------+| country |+-----------+| USA || Germany || France |+-----------+Explanation
Section titled “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
Section titled “Example”SELECT DISTINCT column_nameFROM table_name;Output
Section titled “Output” column_name-------------- value1 value2 value3Explanation
Section titled “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
Section titled “Example”SELECT DISTINCT LastNameFROM Employees;Output
Section titled “Output”LastName--------JohnsonSmithBrownExplanation
Section titled “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
Section titled “Example”SELECT DISTINCT department_idFROM employees;Output
Section titled “Output”DEPARTMENT_ID-------------102030405060708090100110120130140150Explanation
Section titled “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
Section titled “Example”SELECT DISTINCT Country FROM Customers;Output
Section titled “Output”Country----------GermanyMexicoUKCanadaUSAFranceExplanation
Section titled “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.