Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to Find Duplicate Rows in PL/SQL
Next article icon

How to delete duplicate rows in SQLite?

Last Updated : 13 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

SQLite is an open-source and serverless database system that does not require any server to perform various queries also it is widely used in the development of embedded software like television and mobile phones Sometimes it might happen that we by mistake insert multiple times similar data into tables which leads to the problem of inconsistency and data integrity. In this article, we will learn about How to delete duplicate rows in SQLite with its syntax examples and so on.

Prerequisites

Before understanding how to perform operations on duplicate row deletion we must have familiarity with SQLite fundamentals and basic SQL commands like SELECT, DELETE and GROUP BY.

Introduction to the Duplicate Rows

Duplicate rows in an SQLite database are defined as multiple records within a table that have the same values in one or more columns. These duplicates can arise due to various reasons, such as data entry errors, inconsistencies in data sources or incomplete data normalization processes. Identifying and managing duplicate rows is important for maintaining data integrity and ensuring efficient database operations. we will learn how to remove duplicate records from the table easily.

Syntax:

DELETE FROM students
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM students
GROUP BY first_name, last_name
);

Explanation: This SQL query deletes duplicate rows from the "students" table by retaining only the records with the minimum "rowid" for each unique combination of "first_name" and "last_name". It ensures that only the earliest entry for each distinct student name combination is preserved, effectively eliminating duplicate entries

By following these steps, we'll effectively declutter our database and ensure that each piece of data is unique, just like ensuring each book on the shelf is distinct. This process not only improves data organization but also enhances the efficiency and performance of our database operations.

Examples of How to Delete Duplicate Rows in SQLite

Example 1: Deleting Duplicate Employee Names

We have a table of employee names and salaries. Some names are repeated.

CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT,
salary REAL
);


INSERT INTO employees (name, salary) VALUES
('John Doe', 50000),
('Jane Smith', 60000),
('John Doe', 50000),
('Michael Johnson', 55000),
('Jane Smith', 60000);

Output:

Employee-Table
Employee Table

Let's delete the duplicate rows form the employees table then check the table.

DELETE FROM employees
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM employees
GROUP BY name
);

Output:

Employee-Output
Employee Output

Explanation:

  • We're identifying duplicate employee names and keeping only the earliest-hired employee with that name.
  • The DELETE statement removes the excess copies, leaving us with a neat and organized employee list.

Example 2: Removing Duplicate Student Names

We have a table of student names, last names, and ages. Some names are repeated.

CREATE TABLE students (
student_id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);


INSERT INTO students (first_name, last_name, age) VALUES
(1, 'John', 'Doe', 25),
(2, 'Jane', 'Smith', 30),
(3, 'John', 'Doe', 25),
(4, 'Michael', 'Johnson', 28),
(5, 'Jane', 'Smith', 30);

Output:

Student-Table
Student Table

Let's delete the duplicate rows form students the table then check the table.

DELETE FROM students
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM students
GROUP BY first_name, last_name
);

Output:

Student-Output
Student Output

Explanation:

  • We're identifying duplicate student names and retaining only the records of the oldest students with the same name.
  • The DELETE statement removes the redundant student entries, leaving us with a concise and organized student database.

Conclusion

Overall, In this article we have learned about how to delete duplicate rows when table consists multiple duplicates rows to ensure the data integrity and consistency With the help of command such as DELETE and GROUP BY which we learned above in the article will remove redundant entries effectively. By understanding these method allow the developers can fast database maintenance tasks, improve data organization also ensure the accuracy of their applications data.


Next Article
How to Find Duplicate Rows in PL/SQL
author
sharmaankit
Improve
Article Tags :
  • SQLite
  • Databases
  • SQLite Query

Similar Reads

  • How to Delete Duplicate Rows in PL/SQL?
    Inconsistencies and inefficiencies in data management are frequently caused by duplicate rows in a database table. Eliminating duplicate rows is a typical PL/SQL activity to maintain data integrity and improve database performance. This article will guide you on how to remove duplicated rows in PL/S
    4 min read
  • How to Delete Duplicate Rows in MySQL?
    Duplicate rows are a common problem in MySQL databases. Duplicate rows can cause problems with data accuracy and integrity. They can also make it difficult to query and analyze data. Ways to Delete Duplicate Rows in MySQLThere are a few different ways to delete duplicate rows from tables in MySQL: U
    4 min read
  • SQL Query to Delete Duplicate Rows
    Duplicate rows in a database can cause inaccurate results, waste storage space, and slow down queries. Cleaning duplicate records from our database is an essential maintenance task for ensuring data accuracy and performance. Duplicate rows in a SQL table can lead to data inconsistencies and performa
    6 min read
  • How to Delete Duplicate Rows in MariaDB
    Duplicate rows in a database can lead to data inconsistencies and inefficiencies. In MariaDB, we can remove duplicate rows using various methods to ensure data integrity and optimize database performance. In this article, We will explore different techniques to identify and delete duplicate rows in
    4 min read
  • How to Find Duplicate Rows in PL/SQL
    Finding duplicate rows is a widespread requirement when dealing with database analysis tasks. Duplicate rows often create problems in analyzing tasks. Detecting them is very important. PL/SQL is a procedural extension for SQL. We can write custom scripts with the help of PL/SQL and thus identifying
    5 min read
  • Delete Duplicate Rows in MS SQL Server
    In MS SQL Server, managing duplicate rows is a common task that can affect the integrity and performance of a database. To address this issue, SQL Server provides several methods for identifying and deleting duplicate rows. In this article, We will explore three effective approaches: using the GROUP
    5 min read
  • How to Fetch Duplicate Rows in a Table?
    Identifying duplicate rows in a database table is a common requirement, especially when dealing with large datasets. Duplicates can arise due to data entry errors, system migrations, or batch processing issues. In this article, we will explain efficient SQL techniques to identify and retrieve duplic
    3 min read
  • How to Remove All Duplicate Rows Except One in SQLite?
    SQLite is a lightweight and open-source relational database management system (RDBMS). SQLite does not require any server to process since it is a serverless architecture that can run operations and queries without any server. In this article, we will understand how to remove duplicate rows except o
    5 min read
  • How to Find Duplicate Records in SQL?
    To find duplicate records in SQL, we can use the GROUP BY and HAVING clauses. The GROUP BY clause allows us to group values in a column, and the COUNT function in the HAVING clause shows the count of the values in a group. Using the HAVING clause with a condition of COUNT(*) > 1, we can identify
    3 min read
  • How to Delete Duplicate Records in Oracle Database?
    Duplicate records in a database can lead to inefficiencies and incorrect query results. Oracle SQL provides several efficient methods for identifying and removing these duplicates, ensuring data accuracy and integrity. This article explains step-by-step how to remove duplicates using the ROWID, a un
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences