Find All Unique Email Domains
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Table: Emails
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.
Write a solution to find all unique email domains and count the number of individuals associated with each domain. Consider only those domains that end with .com.
Return the result table orderd by email domains inascending order.
The result format is in the following example.
Examples
Example 1:
Input:
Emails table:
+-----+-----------------------+
| id | email |
+-----+-----------------------+
| 336 | [email protected] |
| 489 | [email protected] |
| 449 | [email protected] |
| 95 | [email protected] |
| 320 | [email protected] |
| 411 | [email protected] |
+----+------------------------+
Output:
+--------------+-------+
| email_domain | count |
+--------------+-------+
| outlook.com | 2 |
| yahoo.com | 1 |
+--------------+-------+
Explanation:
- The valid domains ending with ".com" are only "outlook.com" and "yahoo.com", with respective counts of 2 and 1.
Output table is ordered by email_domains in ascending order.
Solution
Method 1 – SQL String Functions and Group By
Intuition
We need to extract the domain from each email, filter for domains ending with .com, and count the number of unique users for each domain. This can be done using SQL string functions and grouping.
Approach
- Use
SUBSTRING_INDEX(email, '@', -1)to extract the domain from the email. - Filter for domains ending with
.comusingLIKE '%.com'. - Group by the domain and count the number of users for each domain.
- Order the result by domain in ascending order.
Code
MySQL
SELECT SUBSTRING_INDEX(email, '@', -1) AS domain, COUNT(*) AS count
FROM Emails
WHERE SUBSTRING_INDEX(email, '@', -1) LIKE '%.com'
GROUP BY domain
ORDER BY domain ASC;
PostgreSQL
SELECT SPLIT_PART(email, '@', 2) AS domain, COUNT(*) AS count
FROM Emails
WHERE SPLIT_PART(email, '@', 2) LIKE '%.com'
GROUP BY domain
ORDER BY domain ASC;
Python (pandas)
def find_unique_email_domains(emails: 'pd.DataFrame') -> 'pd.DataFrame':
emails = emails.copy()
emails['domain'] = emails['email'].str.split('@').str[-1]
filtered = emails[emails['domain'].str.endswith('.com')]
result = filtered.groupby('domain').size().reset_index(name='count').sort_values('domain')
return result
Complexity
- ⏰ Time complexity:
O(n), where n is the number of emails, as each email is processed once. - 🧺 Space complexity:
O(n), for storing the result set and intermediate columns.