Watch 10 video solutions for Find Users With Valid E-Mails, a easy level problem involving Database. This walkthrough by Learn With Chirag has 8,671 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Users
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | name | varchar | | mail | varchar | +---------------+---------+ user_id is the primary key (column with unique values) for this table. This table contains information of the users signed up in a website. Some e-mails are invalid.
Write a solution to find the users who have valid emails.
A valid e-mail has a prefix name and a domain where:
'_', period '.', and/or dash '-'. The prefix name must start with a letter.'@leetcode.com'.Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Users table: +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 2 | Jonathan | jonathanisgreat | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | | 5 | Marwan | quarz#2020@leetcode.com | | 6 | David | david69@gmail.com | | 7 | Shapiro | .shapo@leetcode.com | +---------+-----------+-------------------------+ Output: +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | +---------+-----------+-------------------------+ Explanation: The mail of user 2 does not have a domain. The mail of user 5 has the # sign which is not allowed. The mail of user 6 does not have the leetcode domain. The mail of user 7 starts with a period.
Problem Overview: Given a Users table with user_id, name, and mail, return users whose email addresses are valid. A valid email must start with a letter, contain only letters, digits, underscores, periods, or hyphens before the @, and must end exactly with @leetcode.com.
Approach 1: Regular Expression Filtering (O(n * m) time, O(1) space)
This approach uses SQL regular expression matching to validate the email format directly in the query. You apply a REGEXP pattern such as '^[A-Za-z][A-Za-z0-9_.-]*@leetcode\\.com$' to the mail column. The pattern enforces three rules: the first character must be a letter, the local part can contain letters, digits, underscores, periods, or hyphens, and the domain must match @leetcode.com exactly. The database scans each row and checks the regex against the email string. Time complexity is O(n * m), where n is the number of rows and m is the email length, since regex engines inspect characters in the string. Space complexity is O(1) because filtering happens during the query without additional storage. This method is concise and maps directly to the problem constraints, which makes it the most common solution in database interview questions involving regular expressions.
Approach 2: Parsing and Checking (O(n * m) time, O(1) space)
Instead of relying on regex, you can manually validate each email using string operations. Split the email around @ and confirm the domain equals leetcode.com. Then validate the local part: check that the first character is a letter and iterate through the remaining characters ensuring each belongs to the allowed set (letters, digits, underscore, period, or hyphen). This logic can be implemented in application code or through SQL string functions such as SUBSTRING, LIKE, or character checks. Time complexity remains O(n * m) because every character may be inspected, while space complexity stays O(1). This approach is useful when regex support is limited or when you want explicit control over validation logic using string processing.
Recommended for interviews: Regular expression filtering is the expected solution for this problem. It expresses the constraints in a single pattern and keeps the SQL query short and readable. Manual parsing shows deeper understanding of validation rules and works in environments without regex support, but interviewers typically expect the regex-based query because it directly models the problem statement.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Regular Expression Filtering | O(n * m) | O(1) | Best for SQL queries where regex support exists and validation rules map cleanly to a pattern |
| Parsing and Checking | O(n * m) | O(1) | Useful when regex is unavailable or when validation must be implemented using explicit string logic |