[SQL 문제 풀이] Find Valid Emails (유효한 이메일 찾기)

Stupefyee's avatar
Jun 25, 2025
[SQL 문제 풀이] Find Valid Emails (유효한 이메일 찾기)
Find Valid Emails - LeetCode
Can you solve this real interview question? Find Valid Emails - Table: Users +-----------------+---------+ | Column Name | Type | +-----------------+---------+ | user_id | int | | email | varchar | +-----------------+---------+ (user_id) is the unique key for this table. Each row contains a user's unique ID and email address. Write a solution to find all the valid email addresses. A valid email address meets the following criteria: * It contains exactly one @ symbol. * It ends with .com. * The part before the @ symbol contains only alphanumeric characters and underscores. * The part after the @ symbol and before .com contains a domain name that contains only letters. Return the result table ordered by user_id in ascending order.   Example: Input: Users table: +---------+---------------------+ | user_id | email | +---------+---------------------+ | 1 | alice@example.com | | 2 | bob_at_example.com | | 3 | charlie@example.net | | 4 | david@domain.com | | 5 | eve@invalid | +---------+---------------------+ Output: +---------+-------------------+ | user_id | email | +---------+-------------------+ | 1 | alice@example.com | | 4 | david@domain.com | +---------+-------------------+ Explanation: * alice@example.com is valid because it contains one @, alice is alphanumeric, and example.com starts with a letter and ends with .com. * bob_at_example.com is invalid because it contains an underscore instead of an @. * charlie@example.net is invalid because the domain does not end with .com. * david@domain.com is valid because it meets all criteria. * eve@invalid is invalid because the domain does not end with .com. Result table is ordered by user_id in ascending order.
Find Valid Emails - LeetCode
notion image
모든 유효한 이메일 주소를 찾기 위한 솔루션을 작성합니다. 유효한 이메일 주소는 다음 기준을 충족합니다: * 그것은 정확히 하나의 @ 기호를 포함하고 있습니다. * .com으로 끝납니다. * @ 기호 앞에는 영숫자 문자와 밑줄만 포함되어 있습니다. * @ 기호 뒤와 .com 앞의 부분에는 문자만 포함된 도메인 이름이 포함되어 있습니다.
 

내가 작성한 쿼리

MySQL, Oracle

MySQL

SELECT user_id, email FROM Users WHERE email REGEXP '^[a-zA-Z0-9_]+@[a-zA-Z]+\\.com$' ORDER BY user_id;

Oracle

SELECT user_id, email FROM Users WHERE REGEXP_LIKE(email, '^[a-zA-Z0-9_]+@[a-zA-Z]+\.com$') ORDER BY user_id;

차이점

기능
MySQL
Oracle
정규식 매칭 함수
REGEXP
REGEXP_LIKE(column, pattern)
이스케이프 문자 처리
백슬래시 \\ 필요
백슬래시 \ 하나로 충분
 
Share article

stupefyee