-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3059-FindAllUniqueEmailDomains.sql
More file actions
57 lines (54 loc) · 1.89 KB
/
3059-FindAllUniqueEmailDomains.sql
File metadata and controls
57 lines (54 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- 3059. Find All Unique Email Domains
-- 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 in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Emails table:
-- +-----+-----------------------+
-- | id | email |
-- +-----+-----------------------+
-- | 336 | hwkiy@test.edu |
-- | 489 | adcmaf@outlook.com |
-- | 449 | vrzmwyum@yahoo.com |
-- | 95 | tof@test.edu |
-- | 320 | jxhbagkpm@example.org |
-- | 411 | zxcf@outlook.com |
-- +----+------------------------+
-- 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.
-- Write your MySQL query statement below
-- SELECT SUBSTRING_INDEX(str, delimiter, count) FROM table;
SELECT
email_domain,
COUNT(*) AS count
FROM
(
SELECT
SUBSTRING_INDEX(email, '@', -1) AS email_domain
FROM
Emails
WHERE
email LIKE "%.com" -- Consider only those domains that end with .com.
) AS t
GROUP BY
email_domain
ORDER BY
email_domain -- orderd by email domains in ascending order.