LeetCode 196: Delete Duplicate Emails [SQL]

  Рет қаралды 18,135

Frederik Müller

Frederik Müller

3 жыл бұрын

Solution and walkthrough of leetcode database problem 196: Delete Duplicate Emails. I'm using MySQL but this solution should work in any SQL dialect such as PostgreSQL SQL Server, etc.
Link to the problem: leetcode.com/problems/delete-...

Пікірлер: 31
@frederikmuller
@frederikmuller 2 жыл бұрын
If you want to have access to more free SQL problems, check out StrataScratch: stratascratch.com/?via=frederik
@tongkezhao2188
@tongkezhao2188 3 жыл бұрын
I like the way that you always give interview tips relate to each question at the end of a video. Good job!
@andrew5291
@andrew5291 3 жыл бұрын
Thank you! You are the best! Very helpful : )
@ahmedboutaraa8771
@ahmedboutaraa8771 3 жыл бұрын
thank you
@stellaueda4565
@stellaueda4565 3 жыл бұрын
Thx again
@pranitmhatre8924
@pranitmhatre8924 3 жыл бұрын
I got exactly the same problem in my coding Interview.
@joshuaperez2271
@joshuaperez2271 Жыл бұрын
Thanks for explaining this concept!
@frederikmuller
@frederikmuller Жыл бұрын
Glad it was helpful!
@sheetalm5959
@sheetalm5959 3 жыл бұрын
Thank you for the video, I am new to SQL coding. Can you please explain how did you get the 'Intuition' behind using self join here? Thanks.
@frederikmuller
@frederikmuller 3 жыл бұрын
Sure, the intuition behind using self join is that we compare the table to another copy of itself to see if we find any matching email addresses with higher ID values than the current row we're looking at (these will be the duplicates). We're then deleting these rows from our data base by using the DELETE statement since by the problem definition we only want to keep the entry with the lowest ID value. Hope that makes sense!
@sheetalm5959
@sheetalm5959 3 жыл бұрын
@@frederikmuller Got it. Thank you🙂
@utsavsagar6665
@utsavsagar6665 5 ай бұрын
Please let me know where i am wrong in this in syntax 🙏 delete from person where email in(select email from person group by email order by id having count(email) >1);
@satyamgavane9345
@satyamgavane9345 Жыл бұрын
what mistake i have done in my code please can you tell me with cte as(select *,row_number() over (partition by email order by id) as r from Person) delete from cte where r>1;
@shriyamjalan247
@shriyamjalan247 2 жыл бұрын
Hi Frederik, I was trying to write a query by selecting the duplicate emails and then deleting it. Can you help me explain what I am doing wrong ? Also, if it is possible to write a query in this way. I selected the duplicate emails in following way: select email from Person group by email having count(Email)>1 I put this in where clause and then tried deleting it.
@inzamamsafi5261
@inzamamsafi5261 2 жыл бұрын
you need a minimum id while grouping. so take that into consideration as well
@mickyman753
@mickyman753 2 жыл бұрын
you can do this select temp.email from (select email from Person group by email order by id) as temp having count(temp.Email)> 1;
@shuchivardan375
@shuchivardan375 2 жыл бұрын
can we use row_number and partition by email function?
@frederikmuller
@frederikmuller 2 жыл бұрын
actually yes, and then just delete everything with row_number > 1 if you order by Id in ascending order.
@frederikmuller
@frederikmuller 2 жыл бұрын
good idea!
@christophercastro6078
@christophercastro6078 9 ай бұрын
I am assuming this wouldn't work if the table had more than 1 duplicate email. Only works if there is at most 1 duplicate email. The statement in the problem descriptions states: Write a solution to delete ALL duplicate emails... Which could be interpreted two ways.
@christophercastro6078
@christophercastro6078 9 ай бұрын
Never mind, this works with several identical emails since there is only one id that is the smallest from the bunch!
@frederikmuller
@frederikmuller 9 ай бұрын
Thanks for bringing this up, this will help people.
@utsavsagar6665
@utsavsagar6665 5 ай бұрын
Thank you for the help but still I am unable to understand what is wrong with this syntax.
@uddipangoswami8427
@uddipangoswami8427 2 жыл бұрын
Why this is not working in Oracle?
@kwoktungwong9486
@kwoktungwong9486 2 жыл бұрын
Hi, why do we delete p2 here not p1?
@frederikmuller
@frederikmuller 2 жыл бұрын
Because we’re supposed to keep the email entry with the lowest id. Since we have a filter on p1.Id < p2.Id, p2 will always be the email duplicate entry with the higher id. There can be more than one duplicate but this query deletes every p2 that can be joined based on the conditions: same email and bigger id as another entry (our p1). Hope that makes sense.
@paper-studio
@paper-studio Жыл бұрын
this code not working, not even the discussion code working, there is some issue I guess
@ekdumfarzi8039
@ekdumfarzi8039 2 жыл бұрын
delete p2 from... why did u use p2, we dont write delete statement like that...where can i learn more about that way of writing..
@frederikmuller
@frederikmuller 2 жыл бұрын
I just replied this to another comment asking the same question: Because we’re supposed to keep the email entry with the lowest id. Since we have a filter on p1.Id < p2.Id, p2 will always be the email duplicate entry with the higher id. There can be more than one duplicate but this query deletes every p2 that can be joined based on the conditions: same email and bigger id as another entry (our p1). Hope that makes sense. The DELETE statement works just like the SELECT statement but instead it deletes rows instead of selecting them for output. It doesn’t show up often in interviews so I don’t think you need to read up more on it. The trick with this question is to find what you want to delete by using a JOIN which makes the question tricky and non-standard.
@vasiliyosipov43
@vasiliyosipov43 Жыл бұрын
I was dissapointed about that too, because we can't write anything after delete except from. But I guess it's the MySql special option. Didn't work for PostgreSQL btw
@sungwonjudycho
@sungwonjudycho Жыл бұрын
Hi Fredrik! Thank you so much for your videos; it's been really helpful while trying to study SQL by myself. I had some questions about your explanation and would be great if I could get some pointers on what I'm missing here. Q1. why "WHERE" is not accepted? [My Attempt] DELETE p2.id FROM Person p1 JOIN Person p2 ON p1.email = p2.email WHERE p1.id < p2.id Q2.I'm not sure I understand how the [AND] clause could be used on this Join table. Shouldn't ON only accept when the two columns are identical? [ACCEPTED ANSWER] DELETE p2.id FROM Person p1 JOIN Person p2 ON p1.email = p2.email AND p1.id < p2.id
LeetCode 197: Rising Temperature [SQL]
8:20
Frederik Müller
Рет қаралды 17 М.
LeetCode 182: Duplicate Emails [3 Solutions]
11:49
Frederik Müller
Рет қаралды 10 М.
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 36 МЛН
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 26 МЛН
Scary Teacher 3D Nick Troll Squid Game in Brush Teeth White or Black Challenge #shorts
00:47
1 or 2?🐄
00:12
Kan Andrey
Рет қаралды 55 МЛН
541 Leetcode problems are NOT enough.
7:12
Sahil & Sarra
Рет қаралды 157 М.
[Easy] LeetCode SQL - 196. Delete Duplicate Emails
12:06
Cloud Concepts By Chandra
Рет қаралды 165
Duplicate Emails | LeetCode Problem |
9:13
GeekCoders
Рет қаралды 1,5 М.
LeetCode 1667 Interview SQL Question with Detailed Explanation | Practice SQL
6:28
How to MASTER Data Structures & Algorithms FAST in 2023
10:21
Internet Made Coder
Рет қаралды 207 М.
LeetCode 181: Employees Earning More Than Their Managers [SQL]
8:28
Frederik Müller
Рет қаралды 14 М.
LeetCode SQL: 197. Rising Temperature (Using SELF JOIN)
7:44
Cloud Concepts By Chandra
Рет қаралды 158
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 36 МЛН