×

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 CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 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 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.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 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.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.