SQL clause is the specific part of a SQL statement which is used to perform various operations. It can be combined to create more complex queries to retrieve and manipulate data.
DISTINCT clause is used to eliminate the duplicate rows from the result.
It is often used with other clauses such as 'SELECT' and 'FROM' etc. to retrieve, specify table and manipulate data.
SELECT DISTINCT column_name(s) FROM table_name;
• Specify the column(s) name after the 'SELECT' keyword.
• Specify Asterisk (*) symbol to selects all columns from the table after the 'SELECT' keyword.
• Specify the table name after the 'FROM' keyword.
• 'DISTINCT' keyword eliminates duplicate rows from the result.
Suppose we have a table called [Employees] table that contains duplicate rows.
Let's assume you want to retrieve all columns with only unique rows by eliminating duplicate rows from [Employees] table.
Run below SQL statement :
SELECT DISTINCT * FROM [Employees];
Above statement will retrieve only unique rows by eliminating duplicate rows from the [Employees] table and such all columns are retrieved with only 3 rows.
That's it! You have successfully retrieved all columns with unique rows from [Employees] table.
Let's assume you want to retrieve only Employee_Id column with unique rows by eliminating duplicate rows from [Employees] table.
Run below SQL statement :
SELECT DISTINCT Employee_Id FROM [Employees];
Above statement will retrieve only unique rows by eliminating duplicate rows from Employee_Id column in the [Employees] table and such Employee_Id column is retrieved with only 3 rows.
That's it! You have successfully retrieved Employee_Id column with unique rows from [Employees] table.
The DISTINCT clause removes duplicate rows from the result set in SQL.
The DISTINCT rule in SQL removes duplicate rows from the result set of a query.
Using DISTINCT in SQL can impact performance by requiring additional processing to identify and remove duplicates.
No, DISTINCT cannot be used directly in a WHERE clause; it's used in the SELECT clause to filter duplicate rows in the result set.