SQL Server Tutorial
In SQL Server, DISTINCT clause is used with SELECT statement to remove the duplicate rows fetched in the SQL query result.
SELECT [ALL | DISTINCT] column_name1[, column_name2, ....] FROM table_name WHERE Filter_condition
Let us consider the example 'Employee' database table.
Getting all the rows with duplicate rows. This is also similar like using ALL keyword in SELECT Statement.
SELECT DeptId, Country FROM Employee ORDER BY Country ASCOutput:
To get the distinct rows of columns DeptId and Country from Employee table in ascending order based on Country column.
SELECT DISTINCT DeptId, Country FROM Employee ORDER BY Country ASCOutput:
To get particular column distinct values, here getting distinct country values.
SELECT DISTINCT Country FROM Employee ORDER BY Country ASCOutput:
To get the distinct rows based on all the column in the database table.
SELECT DISTINCT * FROM Employee ORDER BY Country ASCOutput:
Getting all the rows with duplication.
SELECT ALL Country FROM Employee ORDER BY Country ASCOutput:
Computing sum of salary's in SELECT Statement using SUM function with DISTINCT Keyword.
SELECT SUM(DISTINCT Salary) 'Total Salary' FROM Employee Output:SUM Function with ALL
Computing average salary in SELECT Statement using SUM function with ALL Keyword.
SELECT SUM(ALL Salary) 'Total Salary' FROM Employee Output:
« Previous Next »
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page