+—-+———+
| Id | Electronic mail |
+—-+———+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+—-+———+
For instance, your question ought to return the next for the above desk:
+———+
| Electronic mail |
+———+
| a@b.com |
+———+
Notice: All emails are in lowercase.
SQL question to search out duplicate values in a Column – Answer
Listed below are 3 ways to unravel this drawback in SQL question, first by utilizing group by clause, second by utilizing self-join, after which third by utilizing subquery with exists clause. Whereas I agree that this drawback could be solved differently, however it is usually an ideal instance of how you should utilize the SQL GROUP BY and HAVING clause.
However, in case you are new to the SQL world, it is higher to begin with a complete SQL course like The Full SQL Bootcamp course by Jose Portilla on Udemy. That may assist you to to be taught SQL higher and faster, and these sorts of articles may even make extra sense upon getting some SQL information below your belt.
1. Discovering Duplicate parts By utilizing GROUP BY
The only answer to this drawback is by utilizing the GROUP BY and HAVING Clause. Use GROUP BY to group the consequence set on e-mail, this can convey all duplicate emails in a single group, now if the depend for a selected e-mail is bigger than 1 it means it’s a duplicate e-mail. Right here is the SQL question to search out duplicate emails :
# Write your MySQL question assertion under
SELECT Electronic mail FROM Particular person GROUP BY Electronic mail HAVING COUNT(Electronic mail) > 1
That is additionally my accepted reply on LeetCode. You’ll be able to see by utilizing the depend perform you’ll be able to depend a lot of parts within the group and in case your group comprises greater than 1 row then it is a duplicate worth that you simply wish to print.
2. Discovering Duplicate values in a column By utilizing Self Be part of
By the best way, there are a few extra methods to unravel this drawback, one is by utilizing Self Be part of. For those who keep in mind, In Self Be part of we be a part of two situations of the identical desk to match one report to a different. Now if an e-mail from one report within the first occasion of the desk is the same as the e-mail of one other report within the second desk it means the e-mail is duplicate. Right here is the SQL question utilizing Self Be part of
# Write your MySQL question assertion under
SELECT DISTINCT a.Electronic mail FROM Particular person a JOIN Particular person b ON a.Electronic mail = b. Electronic mail WHERE a.Id != b.Id
Keep in mind to make use of the key phrase distinct right here as a result of it’s going to print the duplicate e-mail as many instances it seems within the desk. That is additionally an accepted answer in Leetcode. If you wish to be taught extra about how be a part of works and find out how to use left and proper out be a part of, I counsel you be a part of an SQL course that works with actual information like SQL for Newbs: Knowledge Evaluation for Learners on Udemy.
3. Discovering duplicate emails By utilizing Sub-query with EXISTS:
You’ll be able to even resolve this drawback utilizing a correlated subquery. In a correlated subquery, the internal question is executed for every report within the outer question. So one e-mail is in comparison with the remainder of the e-mail in the identical desk utilizing a correlated subquery and EXISTS clause in SQL as proven under.
SELECT DISTINCT p1.Electronic mail FROM Particular person p1 WHERE EXISTS( SELECT * FROM Particular person p2 WHERE p2.Electronic mail = p1.Electronic mail AND p2.Id != p1.Id )
For those who nonetheless want extra steering then becoming a member of a complete SQL course like The Full SQL Bootcamp course by Jose Portilla on Udemy can also be a good way to know find out how to discover duplicate parts in a desk utilizing GROUP BY with HAVING clause.
That is all about find out how to discover duplicates in SQL utilizing the GROUP BY and HAVING clause. I’ve additionally proven you how one can resolve this drawback utilizing Self-join and a subquery with the EXISTS clause as effectively. When you get accustomed to the sample you’ll be able to resolve many such issues. If you wish to be taught extra take a look at the next assets.
Different associated SQL queries, Interview questions, and articles:
- The way to be a part of three tables in a single single SQL question (answer)
- Write a SQL question to search out all desk names on a database in MySQL (answer)
- 5 Programs to be taught Database and SQL Higher (programs)
- The true distinction between WHERE and HAVING clause in SQL? (reply)
- High 5 Programs to be taught Microsoft SQL Server in-depth (programs)
- The way to migrate SQL queries from Oracle to SQL Server? (reply)
- High 5 Web sites to be taught SQL on-line for FREE? (useful resource)
- What’s the distinction between UNION and UNION ALL in SQL? (reply)
- Write a SQL question to repeat or backup a desk in MySQL (answer)
- How do you discover the duplicate rows in a desk on a database? (answer)
- 5 Superior SQL books to degree up your SQL expertise (books)
- 5 Programs to be taught Oracle and Microsoft SQL Server database (programs)
- Distinction between Self and Equi Take part SQL? (reply)
- 4 Free Books to be taught Microsoft SQL Server database (books)
- High 5 Web sites to be taught SQL on-line for FREE? (web sites)
- 5 Free Programs to be taught Database and SQL (free programs)
- 5 Free Programs to be taught Oracle and SQL Server? (programs)
- High 5 Programs to be taught MySQL Database for Learners (Programs)
- What’s the distinction between View and Materialized View in Database? (reply)
- Distinction between clustered and non-clustered indexes in SQL? (reply)
- Distinction between Major and Candidate key within the desk? (reply)
- 5 Free Programs to be taught T-SQL and SQL Server for Learners (Programs)
- Distinction between the Distinctive and Major keys within the desk? (reply)
- High 5 Programs to be taught PostgreSQL in-depth (programs)
- 5 Free Programs to be taught T-SQL and SQL Server for Learners (Programs)
- Distinction between Major and Overseas keys within the desk? (reply)
Thanks for studying this text, in the event you like this SQL Group by HAVING tutorial and find out how to discover duplicate values for a selected column then please share it with your mates and colleagues. In case you have any questions or suggestions, then please drop a be aware.
P.S. – If you’re inquisitive about studying Database and SQL and on the lookout for some free assets to begin your journey, then you may also check out this listing of Free SQL Programs for Learners to kick-start your studying.