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
id is the primary key
+-------------+---------+| 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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code