×

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 SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

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

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

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 remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

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 SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.