Find All Unique Email Domains - Problem

๐Ÿ” Find All Unique Email Domains

You're tasked with analyzing email data to understand domain distribution! Given a table of emails, you need to extract and count unique email domains that end with .com.

๐Ÿ“Š Database Schema

Table: Emails
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id is the primary key

๐ŸŽฏ Your Mission

Write a SQL query to find all unique email domains that end with .com and count how many individuals are associated with each domain. Return results ordered by domain name in ascending order.

๐Ÿ“ Example

Input:

Emails table:
+----+------------------+
| id | email            |
+----+------------------+
| 1  | alice@gmail.com  |
| 2  | bob@yahoo.com    |
| 3  | charlie@gmail.com|
| 4  | david@yahoo.org  |
| 5  | eve@hotmail.com  |
+----+------------------+

Output:

+-------------+-------+
| domain      | count |
+-------------+-------+
| gmail.com   | 2     |
| hotmail.com | 1     |
| yahoo.com   | 1     |
+-------------+-------+

Input & Output

example_1.sql โ€” Basic Case
$ Input: Emails table: +----+------------------+ | id | email | +----+------------------+ | 1 | alice@gmail.com | | 2 | bob@yahoo.com | | 3 | charlie@gmail.com| +----+------------------+
โ€บ Output: +-------------+-------+ | domain | count | +-------------+-------+ | gmail.com | 2 | | yahoo.com | 1 | +-------------+-------+
๐Ÿ’ก Note: We extract domains from .com emails: gmail.com appears twice, yahoo.com once. Results are sorted alphabetically.
example_2.sql โ€” Mixed Domains
$ Input: Emails table: +----+--------------------+ | id | email | +----+--------------------+ | 1 | user@example.com | | 2 | admin@test.org | | 3 | hello@world.net | | 4 | support@help.com | +----+--------------------+
โ€บ Output: +-------------+-------+ | domain | count | +-------------+-------+ | example.com | 1 | | help.com | 1 | +-------------+-------+
๐Ÿ’ก Note: Only .com domains are included: example.com and help.com. The .org and .net domains are filtered out.
example_3.sql โ€” Single Domain
$ Input: Emails table: +----+------------------+ | id | email | +----+------------------+ | 1 | user1@company.com| | 2 | user2@company.com| | 3 | user3@company.com| +----+------------------+
โ€บ Output: +-------------+-------+ | domain | count | +-------------+-------+ | company.com | 3 | +-------------+-------+
๐Ÿ’ก Note: All emails belong to the same domain company.com, so we get one row with count 3.

Constraints

  • 1 โ‰ค Number of emails โ‰ค 104
  • Email addresses are valid and contain exactly one @ symbol
  • All email addresses are in lowercase
  • Only consider domains ending with .com
  • Domain names are case-sensitive (though all lowercase in this problem)

Visualization

Tap to expand
SQL Email Domain Analysis Pipeline๐Ÿ“ง Raw Emailsalice@gmail.combob@yahoo.orgeve@hotmail.comโœ‚๏ธ Extract DomainSUBSTRING(...)gmail.comhotmail.com๐Ÿ” Filter .comWHERE LIKE '%.com'gmail.com โœ“hotmail.com โœ“๐Ÿ“Š GROUP BYCOUNT(*)gmail.com: 2hotmail.com: 1๐Ÿ“‹ Final Resultsdomain | countgmail.com | 2hotmail.com | 1yahoo.com | 1ORDER BY domain
Understanding the Visualization
1
Extract Domain
Use string functions to extract the part after '@' from each email address
2
Filter .com
Keep only domains ending with '.com' using WHERE clause
3
Group & Count
GROUP BY domain automatically creates groups and COUNT(*) tallies each group
4
Sort Results
ORDER BY domain name to present results alphabetically
Key Takeaway
๐ŸŽฏ Key Insight: SQL's GROUP BY is like having automatic sorting bins that group identical domains and count them efficiently in a single database scan!
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~12 min Avg. Time
840 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen