MySQL 1517.sql
SELECT user_id, name, mail
FROM Users
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'
Difficulty
and has an acceptance rate of about 36% (800k+ submissions).
Typical patterns to solve this problem:
- Database
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided SQL statement extracts three columns-user_id, name, and mail-from the Users table, restricting the result set to rows whose mail field conforms to a specific regular-expression pattern. The pattern enforces a strict format for email addresses belonging to the leetcode.com domain, ensuring that the local part begins with an alphabetic character and contains only permitted symbols thereafter.
Key Steps and Corresponding Snippets
The query can be broken down into three logical components: column selection, source specification, and row filtering.
The column selection is expressed by the SELECT clause:
SELECT user_id, name, mail
The source of the data is identified by the FROM clause:
FROM Users
Row filtering is performed by the WHERE clause that applies a regular-expression predicate:
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'
Together, these fragments compose the full statement:
SELECT user_id, name, mail
FROM Users
WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'
Edge Cases Handled by the Regular Expression
The regular expression ^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$ is anchored at both the start (^) and end ($) of the string, guaranteeing that the entire mail value must match the pattern without any leading or trailing characters.
The first character class [a-zA-Z] ensures that the local part cannot be empty and must start with an alphabetic character, thereby rejecting addresses that begin with a digit, underscore, hyphen, or period.
The subsequent character class [a-zA-Z0-9._-]* permits any combination of letters, digits, periods, underscores, and hyphens after the initial letter. Because the quantifier * allows zero or more occurrences, the local part may consist of a single letter followed directly by the domain separator.
The literal [@] matches the @ symbol explicitly, preventing accidental inclusion of similar characters.
The domain portion leetcode[.]com is fixed to the exact string leetcode.com. The use of [.] instead of a plain dot avoids any special meaning the dot might have in other regex contexts, ensuring that only a literal period is accepted.
By anchoring the pattern, the query excludes addresses that contain additional subdomains (e.g., mail@sub.leetcode.com) or extra characters after the domain (e.g., mail@leetcode.com.org). It also rejects case variations in the domain because the pattern specifies lowercase letters only for leetcode.com.
Performance Optimizations Observed
The regular expression is deliberately constructed to be deterministic and linear. The initial anchor ^ combined with a single mandatory alphabetic character eliminates the need for backtracking at the start of the string. The subsequent character class [a-zA-Z0-9._-]* uses a simple repetition without nested alternations, allowing the regex engine to process the local part in a single pass.
The final anchor $ ensures that the engine can stop scanning as soon as the domain portion is matched, avoiding unnecessary checks beyond the end of the string. By limiting the pattern to a fixed domain literal, the engine does not need to evaluate complex alternatives or lookaheads, which contributes to faster evaluation.
Because the pattern is anchored and does not contain costly constructs such as greedy quantifiers over ambiguous subpatterns, the database can often apply the predicate efficiently, especially when combined with an index on the mail column or a functional index that stores the result of the regex evaluation. This design minimizes full-table scans and leverages the database’s ability to short-circuit evaluation when a row fails early criteria.
ELI5 - Simplified Explanation
Imagine the computer has a giant treasure box filled with files about every person who visits a website. Each file has a name, a user identification number, and an email address written on it. This piece of code is just a way to go through all those files to pick out only the ones that match a special pattern. It looks through the stack and grabs the ID, the name, and the email for certain users. The complicated part at the end acts like a filter to make sure the email address follows a specific set of rules. It is checking to ensure the address starts with a letter and finishes exactly with at leetcode dot com. If the file follows that exact rule, the computer keeps it, but if it does not match, the computer just ignores it and keeps looking at the next file in the box.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||
|---|---|---|---|---|---|
| author | solutions | ||||
| www | AlgoMonster | C++ | Java | Python | |
| WalkCCC | SQL | ||||
| leetcode.ca | Python | SQL | |||
| Progiez | C++ | Java | Python | ||
| AlgoMonster | C++, Python, Java | ||||
| WalkCCC | SQL | ||||
| leetcode.ca | Python, SQL | ||||
| Progiez | C++, Java, Python |
Videos
Solved On The Same Day
- problem 415: Add Strings
- problem 463: Island Perimeter
- problem 541: Reverse String II
- problem 1211: Queries Quality and Percentage
- problem 1251: Average Selling Price
- problem 1280: Students and Examinations
- problem 1327: List the Products Ordered in a Period
- problem 1378: Replace Employee ID With The Unique Identifier
- problem 1407: Top Travellers
- problem 1484: Group Sold Products By The Date
- problem 1527: Patients With a Condition
- problem 1581: Customer Who Visited but Did Not Make Any Transactions
- problem 1587: Bank Account Summary II
- problem 1633: Percentage of Users Attended a Contest
- problem 1661: Average Time of Process per Machine
- problem 1667: Fix Names in a Table
- problem 1683: Invalid Tweets
- problem 1693: Daily Leads and Partners
- problem 1729: Find Followers Count
- problem 1731: The Number of Employees Which Report to Each Employee
- problem 1741: Find Total Time Spent by Each Employee
- problem 1757: Recyclable and Low Fat Products
- problem 1789: Primary Department for Each Employee
- problem 1795: Rearrange Products Table
- problem 1873: Calculate Special Bonus
- problem 1890: The Latest Login in 2020
- problem 1965: Employees With Missing Information
- problem 1978: Employees Whose Manager Left the Company
- problem 2356: Number of Unique Subjects Taught by Each Teacher
- problem 166: Fraction to Recurring Decimal
- problem 223: Rectangle Area
- problem 240: Search a 2D Matrix II
- problem 318: Maximum Product of Word Lengths
- problem 357: Count Numbers with Unique Digits
- problem 394: Decode String
- problem 1158: Market Analysis I
- problem 1341: Movie Rating
- problem 1492: The kth Factor of n
- problem 1498: Number of Subsequences That Satisfy the Given Sum Condition
- problem 1907: Count Salary Categories
- problem 1934: Confirmation Rate
- problem 2405: Optimal Partition of String
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality