LeetCode in Kotlin

3436. Find Valid Emails

Easy

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:

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:

Result table is ordered by user_id in ascending order.

Solution

# Write your MySQL query statement below
select user_id, email from users
where email regexp '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9_]*\.com$'
order by user_id