Ad Code

SQL Distinct Statement

   

SQL Distinct Statement




In SQL, the DISTINCT keyword is used to retrieve only the unique values from a column or a combination of columns in a table. The syntax for using the DISTINCT keyword in a SELECT statement is as follows:

SELECT DISTINCT column_name

FROM table_name;

 This query will return only the unique values from the specified column_name in the table_name. If you want to retrieve distinct values from multiple columns, you can specify them separated by commas:

SELECT DISTINCT column_name_1, column_name_2

FROM table_name;

This query will return only the unique combinations of values from column_name_1 and column_name_2 in the table_name.

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

 Here, column1, column2, etc. are the names of the columns from which you want to retrieve distinct values, and table_name is the name of the table containing those columns.

 When you execute this query, it will return only the unique values from the specified columns, and will exclude any duplicates. For example, if you have a table employees with columns name, department, and salary, and you want to retrieve a list of all the unique department names, you can use the following query:

             SELECT DISTINCT department FROM employees;

 This will return only the unique department names from the employees table.

 

❮ PreviousNext ❯

Post a Comment

0 Comments

Close Menu