In this tutorial post, we are going discussing how to Eliminating Duplicate Records from the table.
SQL DISTINCT
- The table could have duplicate row. In such case how to view only unique rows i.e distinct rows to achieve it DISTINCT clause can be used.
- The DISTINCT clause can be used to removing duplicate records from the result set. The DISTINCT clause can only be used with SELECT statements.
- The DISTINCT clause scans through the values of the columns specified and display only unique values from the result set.
Consider we have EMPLOYEE demo table which is using for the executing DISTINCT command
EMP_ID | EMP_NAME | EMP_DESIGNATION | EMP_GENDER | EMP_QUALIFICATION | EMP_LOCATION |
100 | Narayanaswamy | Sr.Software Engineer | Male | MCA | Chennai |
101 | Kumar | Quality Analyst | Male | B.Tech | Chennai |
102 | Ramesh | Database Administration | Male | B.Tech | Chennai |
100 | Narayanaswamy | Sr.Software Engineer | Male | MCA | Chennai |
Selected all columns and all rows
Syntax:
SELECT DISTINCT * FROM <Table-Name>;
Query:
SELECT DISTINCT * FROM EMPLOYEE;
The above query scan entire rows, and eliminates rows which are having same content in each column.
Output
EMP_ID | EMP_NAME | EMP_DESIGNATION | EMP_GENDER | EMP_QUALIFICATION | EMP_LOCATION |
100 | Narayanaswamy | Sr.Software Engineer | Male | MCA | Chennai |
101 | Kumar | Quality Analyst | Male | B.Tech | Chennai |
102 | Ramesh | Database Administration | Male | B.Tech | Chennai |
Selected columns and all rows
Syntax:
SELECT DISTINCT <Column-Name 1> FROM <Table-Name>;
Query:
SELECT DISTINCT EMP_QUALIFICATION FROM EMPLOYEE;
The above query scan entire rows, and eliminates rows which are having same content in EMP_QUALIFICATION column.
Output:
EMP_QUALIFICATION |
MCA |
B.Tech |
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.