Find Valid Emails - Problem
You are given a table called Users that contains user information including their email addresses.
Write a SQL query to find all valid email addresses that meet the following criteria:
- Contains exactly one @ symbol
- Ends with .com
- The part before @ contains only alphanumeric characters and underscores
- The part after @ and before .com contains only letters (domain name)
Return the result table ordered by user_id in ascending order.
Table Schema
Users
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | Unique identifier for each user |
email
|
varchar | Email address of the user |
Primary Key: user_id
Note: Each row contains a user's unique ID and email address
Input & Output
Example 1 — Mixed Valid and Invalid Emails
Input Table:
| user_id | |
|---|---|
| 1 | alice@abc.com |
| 2 | bob@.com |
| 3 | test@gmail.com |
| 4 | invalid@test |
| 5 | user@123domain.com |
Output:
| user_id | |
|---|---|
| 1 | alice@abc.com |
| 3 | test@gmail.com |
💡 Note:
Valid emails: alice@abc.com (alphanumeric before @, letters in domain), test@gmail.com (follows all rules). Invalid emails: bob@.com (no domain name), invalid@test (doesn't end with .com), user@123domain.com (numbers in domain name).
Example 2 — Special Characters and Underscores
Input Table:
| user_id | |
|---|---|
| 1 | user_123@example.com |
| 2 | test-user@domain.com |
| 3 | valid@site.com |
| 4 | double@@email.com |
Output:
| user_id | |
|---|---|
| 1 | user_123@example.com |
| 3 | valid@site.com |
💡 Note:
Valid: user_123@example.com (underscore allowed before @), valid@site.com (meets all criteria). Invalid: test-user@domain.com (hyphen not allowed before @), double@@email.com (has two @ symbols).
Constraints
-
1 ≤ user_id ≤ 1000 -
emailcontains only printable ASCII characters - Each email is at most 100 characters long
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code