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 email
1 alice@abc.com
2 bob@.com
3 test@gmail.com
4 invalid@test
5 user@123domain.com
Output:
user_id email
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 email
1 user_123@example.com
2 test-user@domain.com
3 valid@site.com
4 double@@email.com
Output:
user_id email
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
  • email contains only printable ASCII characters
  • Each email is at most 100 characters long

Visualization

Tap to expand
Find Valid Emails - SQL Validation INPUT: Users Table user_id email 1 john@gmail.com 2 bad@@test.com 3 user_1@site.com 4 no.com 5 test@abc123.com 6 mary_j@mail.com 7 bad@test.org Validation Rules: - Exactly one @ symbol - Must end with .com ALGORITHM STEPS 1 Check @ count LENGTH - LENGTH(REPLACE) email - REPLACE(@,'') = 1 2 Check .com ending Use LIKE pattern email LIKE '%.com' 3 Validate local part Alphanumeric + underscore REGEXP '^[A-Za-z0-9_]+@' 4 Validate domain Letters only before .com REGEXP '@[A-Za-z]+\.com$' SQL Query: SELECT * FROM Users WHERE email REGEXP '^[A-Za-z0-9_]+@ [A-Za-z]+\.com$' FINAL RESULT user_id email 1 john@gmail.com 3 user_1@site.com 6 mary_j@mail.com Rejected Emails: bad@@test.com 2x @ no.com No @ test@abc123.com Digits bad@test.org .org 3 Valid Emails Found - OK Key Insight: REGEXP provides powerful pattern matching for email validation. The pattern ^[A-Za-z0-9_]+@[A-Za-z]+\.com$ ensures: alphanumeric/underscore local part, exactly one @, letters-only domain, and .com suffix in a single expression. TutorialsPoint - Find Valid Emails | Optimal Solution using REGEXP
Asked in
Amazon 15 Facebook 12 Google 8
25.0K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen