Find Users With Valid E-Mails - Problem
Find Users With Valid E-Mails
You're working as a data analyst for a popular coding platform. The user registration system has been collecting email addresses, but some users have entered invalid emails that are causing issues with the notification system.
Given a
Email Validation Rules:
โ Prefix Requirements: Must start with a letter, can contain letters (a-z, A-Z), digits (0-9), underscore (_), period (.), and dash (-)
โ Domain Requirement: Must end with
Table Structure:
Your task is to write a SQL query that returns all users with valid email addresses.
You're working as a data analyst for a popular coding platform. The user registration system has been collecting email addresses, but some users have entered invalid emails that are causing issues with the notification system.
Given a
Users table with user information, you need to identify all users who have valid email addresses that match the platform's email format requirements.Email Validation Rules:
โ Prefix Requirements: Must start with a letter, can contain letters (a-z, A-Z), digits (0-9), underscore (_), period (.), and dash (-)
โ Domain Requirement: Must end with
@leetcode.comTable Structure:
Users table contains user_id (primary key), name, and mail columns.Your task is to write a SQL query that returns all users with valid email addresses.
Input & Output
example_1.sql โ Basic Valid Emails
$
Input:
Users table:\n| user_id | name | mail |\n|---------|----------|-------------------------|\n| 1 | Winston | winston@leetcode.com |\n| 2 | Jonathan | jonathanisgreat |\n| 3 | Annabelle| bella-@leetcode.com |\n| 4 | Sally | sally.come@leetcode.com |\n| 5 | Marwan | quarz#2020@leetcode.com |\n| 6 | David | teewhy@leetcode.com |
โบ
Output:
| user_id | name | mail |\n|---------|----------|-------------------------|\n| 1 | Winston | winston@leetcode.com |\n| 4 | Sally | sally.come@leetcode.com |\n| 6 | David | teewhy@leetcode.com |
๐ก Note:
Winston's email starts with 'w' (letter) and contains only valid characters. Sally's email starts with 's' and uses allowed characters (period). David's email starts with 't' and is valid. Jonathan's email lacks '@leetcode.com', Annabelle's starts with 'b' but contains invalid dash at end of prefix, and Marwan's contains '#' which is not allowed.
example_2.sql โ Edge Cases
$
Input:
Users table:\n| user_id | name | mail |\n|---------|------|--------------------------|\n| 7 | Alex | a@leetcode.com |\n| 8 | Bob | 123bob@leetcode.com |\n| 9 | Eve | eve_123@leetcode.com |\n| 10 | Max | max.min@leetcode.com |
โบ
Output:
| user_id | name | mail |\n|---------|------|----------------------|\n| 7 | Alex | a@leetcode.com |\n| 9 | Eve | eve_123@leetcode.com |\n| 10 | Max | max.min@leetcode.com |
๐ก Note:
Alex has the shortest valid email (single letter prefix). Eve's email uses underscore and numbers (valid). Max uses a period in the prefix (valid). Bob's email starts with a number '1' instead of a letter, making it invalid.
example_3.sql โ Invalid Domain Cases
$
Input:
Users table:\n| user_id | name | mail |\n|---------|-------|---------------------------|\n| 11 | John | john@gmail.com |\n| 12 | Jane | jane@leetcode.org |\n| 13 | Mike | mike@leetcode.com.au |\n| 14 | Lisa | lisa.wong@leetcode.com |
โบ
Output:
| user_id | name | mail |\n|---------|------|------------------------|\n| 14 | Lisa | lisa.wong@leetcode.com |
๐ก Note:
Only Lisa's email has the correct domain '@leetcode.com'. John uses Gmail, Jane uses .org instead of .com, and Mike has extra '.au' at the end. All others have wrong domains despite having valid prefixes.
Constraints
- 1 โค Users table rows โค 1000
- 1 โค user_id โค 106
- 1 โค name.length โค 50
- 1 โค mail.length โค 100
- Email prefix must start with a letter [a-zA-Z]
- Email domain must be exactly '@leetcode.com'
Visualization
Tap to expand
Understanding the Visualization
1
Input Emails
Database contains mixed valid and invalid email addresses
2
Pattern Matching
REGEXP pattern checks each email against validation rules
3
Filter Results
Only emails matching the pattern are returned
4
Valid Emails
Final result contains only properly formatted emails
Key Takeaway
๐ฏ Key Insight: Regular expressions provide a powerful, single-operation solution for complex string pattern validation, making email filtering both efficient and maintainable.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code