2 Ways to FIND and DELETE Duplicate Rows in a SQL Table | LeetCode SQL
Skills:
SQL Analytics90%
Key Takeaways
Deletes duplicate rows in a SQL table using two approaches: beginner's approach and efficient approach
Full Transcript
try this new and more efficient way to delete duplicate rows I will use the lead code SQL interview question 196 delete duplicate emails as an example here I'm going to present two approaches to solve this problem the last one is the most efficient and is the best suited for tables with large data so please watch this video until the end according to the question we need to create a person table with two columns ID and email the ID column is the primary key which means that each row must have a unique ID value now let's populate this table with some data there are 10 rows and some of them are duplicates let's read the question further the task is to write a solution to delete all duplicate emails keeping only one unique email with the smallest ID for example the email address Raj example.com is associated with id8 while its duplicate entry has ID 1 therefore we should remove the entry with ID 8 and retain the one with ID 1 as it represents the smallest ID now let's quickly write a select a statement to find all the unique emails with the smallest ID here we are choosing the smallest minimum ID for each group of the people who shares the same email this tells SQL that we are working with the table named person we are organizing a list of people people into groups based on their email addresses each group includes people who have the same email this way we ensures that each email appears only once in our final list and we pick the smallest ID for each group it's handy for finding and managing duplicate email entries in our data let's execute out of the total of 10 records seven are unique emails it's evident in the result set that rose with ID 58 eight and 10 are absent indicating that they represents duplicate entries we will save this statement because we will need it later now there are two approaches to handle the problem of removing duplicate records the first one is The Beginner's approach in this approach we will Begin by creating a new table this table will store these unique emails along with their corresponding IDs to create this table I will use the create table as a statement along with the select statement we just discussed after execution we will have a table named tempore person containing all the unique emails with the smallest ID let's verify now we will use the data of this table to delete all the duplicate records let's write the delete statement this is a simple delete statement in the where Clause we will use the not in operator with a subquery after execution any Row in the person table with IDs that do not exist in the tempore person table will be removed it's important to note that in the tempore person table the IDS of duplicate rows are not present therefore this delete operation will effectively remove all the duplicate rows let's execute three rows deleted let's confirm this by examining the person table as you can see in the result set only the rules with the smallest ID have remained and all the duplicate rules have been successfully deleted however while this method gets the job done it doesn't allly with professional SQL development practices and is not the most efficient approach let me tell you why first it makes a new temporary table which can take up a lot of space if your original table has millions of of roles this means it's not very efficient and can also slow things down because it uses a lot of storage if your original table has indexes they will also make the temporary table even bigger second it involves writing two separate queries which can be more complicated and take more work this makes it harder to understand and maintain we are creating a SQL statement to remove duplicate record here's how it goes this part tells SQL that we want to delete some data from the person table this is where we specify the condition for deleting duplicates we will compare the combination of ID and email columns in the table then we have not in operator then we put parenthesis to enclose the special select statement this select a statement finds the unique rowes based on the email column this this is the same select statement that we wrote in the starting of the tutorial let's copy it from here and paste it inside the parenthesis the not in condition checks if the combination of ID and email is not in the list of unique combination found by our select statement so we are simply telling SQL to delete rows in the person table where the combination of ID and email is not among the unique combination found by our selected statement let's execute three rows deleted this SQL statement is a handy way to keep our data Clean and Free from duplicates this is the efficient approach because it doesn't involve saving data into a separate table which means no additional storage or iio overhead it's also simple and easy to understand let's check this statement by submitting it on lead code submitted successfully if you want to learn how to quickly find the number of duplicate records in a table using just one line of code then watch my this short video I explained it in just 50 seconds Link in the description that's it for today thanks for watching this is Manish from Rebellion rider.com
Original Description
Learn how to find and delete duplicate records in an SQL table efficiently. In this video, I will show you two methods: the beginner's approach and the efficient approach. The beginner's approach is simple, but it's not the most efficient. The efficient approach is more complex, but it's faster and more scalable. I will also show you how to use LeetCode's SQL interview question 196 to practice your SQL skills.
------------------------------------------------------------------------
►►►LINKS◄◄◄
Blog: Coming Soon
Must WATCH
► How To FIND DUPLICATE Records In A SQL Table (Fast and Easy!) https://youtube.com/shorts/kMTxEOYJBrE
-------------------------------------------------------------------------
►►►Social Media – Where I sort of Live
https://www.instagram.com/RebellionRider/
https://www.facebook.com/TheRebellionRider/
https://twitter.com/RebellionRider
https://www.linkedin.com/in/mannbhardwaj/
-------------------------------------------------------------------------
►►►Books I refer to
PL/SQL https://amzn.to/2QE1jX0
Performance Tuning https://amzn.to/2sgiAw4
1z0-071 Exam https://amzn.to/2sgfeJw
Python Programming https://amzn.to/305UEbh
-------------------------------------------------------------------------
►►►Camera Gears
► Video Gears That We Use For Creating Our YouTube Video
https://www.amazon.in/shop/manishsharma?listId=DU9UM0XL97KM&ref=idea_share_inf
► Work From Home Essential Tech That We Use Daily
https://www.amazon.in/shop/manishsharma?listId=XAZ18JLLSNB5&ref=idea_share_inf
AFFILIATE DISCLOSURE:
Some of the links used in the description will direct you to Amazon.in. As an Amazon Associate, I earn from qualifying purchases at no additional cost to you.
#RebellionRider
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Manish Sharma · Manish Sharma · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Oracle Database tutorials 1: How to install Oracle Database 11g on windows 7
Manish Sharma
Oracle Database tutorials 2:How To install SQL Developer on windows 7
Manish Sharma
Oracle Database tutorials 3:How to enable Line numbers in SQL Developer.
Manish Sharma
Oracle Database tutorials 4: database connectivity using SQL developer and command prompt
Manish Sharma
Oracle Database tutorials 5: how to Fetch Data using SELECT - SQL statement by Manish Sharma
Manish Sharma
Oracle Database11g tutorials 6 | | How to use Concatenation operator, character String
Manish Sharma
Oracle Database11g tutorials 7 | |SQL DISTINCT keyword || SQL tutorials
Manish Sharma
Canon EOS 600D 2 lens kit/Canon rebell EOS T3i 2 lens kit Unboxing
Manish Sharma
First look: ORACLE CERTIFIED ASSOCIATE (OCA) CERTIFICATE - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Oracle Database11g tutorials 8 || SQL DISTINCT with multiple columns |SQL Distinct with Two columns
Manish Sharma
Oracle Database11g tutorials 9 || What is archive log mode and how to enable archive log mode
Manish Sharma
Oracle Database11g tutorials 10 || SQL Single Row Function (SQL Functions )
Manish Sharma
Oracle Database11g tutorials 11: SQL case manipulation function in Oracle Database
Manish Sharma
how to add channel trailer and section on your youtube channel 2014
Manish Sharma
Oracle Database11g tutorials 12 || SQL Concat Function - SQL character manipulation function
Manish Sharma
Oracle Database11g tutorials 13 || SQL substr function / SQL substring function
Manish Sharma
Oracle Database11g tutorials 14 : How to CREATE TABLE using sql developer and command prompt
Manish Sharma
SQL tutorials 15 || How To CREATE TABLE using enterprise manager 11g
Manish Sharma
Oracle Database11g tutorials 16: How to uninstall oracle 11g from windows 7 64 bit
Manish Sharma
ORACLE CERTIFIED PROFESSIONAL(OCP) CERTIFICATE First look - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Plantronics audio 655 USB headset with Mic Unboxing and Review and Plantronics audio 655 Mic test
Manish Sharma
SQL tutorials 17: SQL Primary Key constraint, Drop primary Key
Manish Sharma
SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
Manish Sharma
SQL tutorial 19: ON DELETE SET NULL clause of Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorials 20: On Delete Cascade Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 21: How To Rename Table in SQL using ALTER TABLE statement By Manish Sharma
Manish Sharma
SQL tutorial 22: How to Add / Delete column from an existing table using alter table
Manish Sharma
SQL tutorial 23: Rename and Modify Column Using Alter Table By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 24 :SQLJoins- Natural Join With ON and USING clause By Manish/Rebellionrider
Manish Sharma
Oracle Database11g tutorials 25: How to install Oracle Database 11g Express Edition R2 on Windows 7
Manish Sharma
SQL tutorial 26: Introduction to SQL Joins in Oracle Database
Manish Sharma
Vidcon 2014 YouTube Fan Funding: How to enable fan funding on YouTube Channel
Manish Sharma
SQL tutorial 27: Right Outer Join in SQL by Manish Sharma for RebellionRider
Manish Sharma
SQL tutorial 28: Left Outer Join By Manish Sharma / RebellionRider
Manish Sharma
SQL tutorial 29: Full Outer Join with example By Manish Sharma/ RebellionRider
Manish Sharma
SQL tutorial 30: Inner Join In SQL by Manish Sharma/RebellionRider
Manish Sharma
SQL tutorial 31 : SQL Cross Join In Oracle Database By Manish Sharma from RebellionRider
Manish Sharma
SQL tutorial 32: How To Insert Data into a Table Using SQL Developer
Manish Sharma
SQL tutorial 33:How To Insert Data into a Table Using SQL INSERT INTO dml statement
Manish Sharma
SQL tutorial 34: How to copy /Insert data into a table from another table using INSERT INTO SELECT
Manish Sharma
SQL tutorial 35: DELETE and TRUNCATE how to delete data from a table
Manish Sharma
SQL tutorial 36: how to create database using database configuration assistant DBCA
Manish Sharma
SQL tutorial 37: How to create NEW USER account using Create User statement in Oracle database
Manish Sharma
SQL tutorial 38: How to create user using SQL Developer in Oracle database
Manish Sharma
SQL tutorial 39: How to create user in oracle using Enterprise Manager
Manish Sharma
SQL tutorial 40: DBA Trick, How to drop a user when it is connected to the database
Manish Sharma
Motorola Moto G 2nd Generation / G2 Unboxing and Review
Manish Sharma
SQL tutorial 41: How to UNLOCK USER in oracle Database
Manish Sharma
SQL tutorial 42: How to Unlock user using SQL Developer By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 43: How to create an EXTERNAL USER in oracle database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 44: How to import data from Microsoft Excel to Oracle Database using SQL Developer
Manish Sharma
SQL tutorial 45: Introduction to user Privileges in Oracle Database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 46: What are System Privileges & How To Grant them using Data Control Language
Manish Sharma
SQL tutorial 47: How to Grant Object Privileges With Grant Option in Oracle Database
Manish Sharma
SQL tutorial 48: How to create Roles in Oracle Database
Manish Sharma
SQL tutorial 49: CASE - Simple Case Expression in Oracle Database (1/2)
Manish Sharma
SQL tutorial 50: CASE - Searched Case Expression In Oracle (2/2)
Manish Sharma
SQL tutorial 51: DECODE function in Oracle Database By Manish Sharma (RebellionRider)
Manish Sharma
Oracle Database Tutorial 52 : Data Pump expdp - How to Export full database using expdp
Manish Sharma
Oracle Database Tutorial 53 : Data pump expdp - How to Export tablespace in Oracle Database
Manish Sharma
More on: SQL Analytics
View skill →Related Reads
📰
📰
📰
📰
Data Analytics (AI/ML) ROI 101 series: Article 3: Accounting fundamentals for data analytics…
Medium · AI
Stop Testing One Indicator at a Time. Loop Your Way to Better Trading
Medium · Data Science
Fact Tables, Star Schemas, Joins and Dashboards
Dev.to · samwel
The Ultimate Step-by-Step Guide: Connecting Power BI to Cloud-Based & Local PostgreSQL
Dev.to · michael imani
🎓
Tutor Explanation
DeepCamp AI