×

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword:

By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases.

A table contains more duplicate values and duplicate values can cause redundancy.

To remove duplicates we use the DISTINCT keyword in sql.

The syntax for DISTINCT Keywords in SQL:

Select DISTINCT column1, column2, column3, …….., column from Table_name where (condition);

 Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

1.Display the table with the removal of duplicates in the sgender column of the Student Table.

Code:

Select DISTINCT sgender from Student;

Output:

 Number of records: 2

 sgender
Male
Female

Note: From the above student table it removes duplicates, such as male and female from the gender column and prints the male and female as unique.

2. Display the table with the removal of duplicates in a sage column of the Student Table.

Code:

 Select DISTINCT sage from Student;

Output

Student Table

Number of records: 5

sage
22
24
21
23
26

Note: From the above student table it removes duplicates, such as 22, 24, 21, and 23 from the sage column and prints the values 22, 24, 21, 23, and 26.

3. Display the table with the removal of duplicates in the sname column of the Student Table.

Code:

Select DISTINCT sname from Student;

Output:

Student Table

 Number of records: 10

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshita
Bindu
Nandhini
Hashish
Rahul

Note: From the above student table, it does not remove duplicates in the sname column, because the sname column does not have any duplicates. Hence it prints all the names present in the sname column.

4. Display the table with the removal of duplicates in the sname column of the Student Table.

Code:

 Select DISTINCT sid from Student;

Output:

 Student Table

Number of records: 10

sid
1
2
3
4
5
6
7
8
9
10

Note: From the above student table, it does not remove duplicates in the sage column, because the sage column does not have any duplicates. Hence it prints all the names present in the age column.

5. Display the table with the removal of duplicates in the Phonenumber column of the Student Table.

Code:

 Select DISTINCT Phonenumber from Student;

Output:

Student Table

 Number of records: 10

Phonenumber
9895678909
6687654634
9867546453
7675643423
6567784532
9867546231
6563412768
6785674839
9453215052
9998989898

Note: From the above student table, it does not remove duplicates in the Phonenumber column, because the Phonenumber column does not have any duplicates.

6. Hence it prints all the names present in the Phonenumber column. 6. Display the table with the removal of duplicates in the sname and id column of the table.

Code:

Select DISTINCT sid,sname from Student;

Output:

Student Table

 Number of records: 10

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Note: From the above student table, it does not remove duplicates in the sid and sname columns, because the sname and sid columns do not have any duplicates. Hence it prints all the names and id’s present in the sid and sname column.

7.Display the table with the count of removal of duplicates in the sgender column of the Student Table.

Code:

 Select count (DISTINCT sgender) from Student;

Output:

Number of records: 1

COUNT (Distinct Sgender)
2

Note: The count is used to count the number of records in a column or table, But here it counts the distinct values present in a column or table. From the above table, the output is 2. Because the distinct values are 2.

8.Display the table with the count of removal of duplicates in a sage column of the Student Table.

Code:

Select count (DISTINCT sage) from Student;

Output:

 Number of records: 1

COUNT (DISTINCT sage)
5

Note: From the above table, the output is 5. Because the distinct values present in the sage column are 5.

9. Display the table with the count of removal of duplicates in the sname column of the Student Table.

Code:

Select count (DISTINCT sname) from Student;

Output:

Number of records: 1

COUNT (DISTINCT sname)
10

Note: From the above table, the output is 10. Because the distinct values present in the sname column are 10.

10. Display the table with the count of removal of duplicates in the sid column of the Student Table.

Code:

 Select count (DISTINCT sid) from Student;

Output:

Number of records: 1

COUNT (DISTINCT sid)
10

Note: From the above table, the output is 5. Because the distinct values present in the sid column are 10.

11. Display the table with the count of removal of duplicates in the Phonenumber column of the Student Table.

Code:

Select count (DISTINCT Phonenumber) from Student;

Output:

Number of records: 1

COUNT (DISTINCT Phonenumber)
10

Note: From the above table, the output is 10.

Because the distinct values present in the Phonenumber column are 10.


Related Topics

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.