EXPLAINED SOLUTION USING SQL (MSSQL)
IntuitionSo I feel tired to work on this small problem using SQL, since I want to find a solution that matches the full requirement.So to check if we have only
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
So I feel tired to work on this small problem using SQL, since I want to find a solution that matches the full requirement.

So to check if we have only one @ symbol, we have to compare the length of email with the length of the replaced @ symbol with empty character, so the difference should be equal to 1, that means we have replaced one character found in the email.

To check if it ends with .com we can use like %.com

To check for alphanumeric and underscores, we have to use PATINDEX the pattern from the substring if it's equal to 0, so we have to use the negative of the pattern (not the pattern).

Then we have to order by user_id.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Select from the user.
```
SELECT user_id, email
FROM Users
```
2. It contains exactly one @ symbol.
```
    LEN(email) - LEN(REPLACE(email, '@', '')) = 1 -- It contains exactly one @ symbol.
```
3. The part before the @ symbol contains only alphanumeric characters and underscores.
```
    email like '%.com' -- It ends with .com.
```
4. The part before the @ symbol contains only alphanumeric characters and underscores.
```
    PATINDEX('%[^a-z0-9_]%', SUBSTRING(email, 1, CHARINDEX('@', email) - 1)) <= 0  -- The part before the @ symbol contains only alphanumeric characters and underscores.
```
5. The part after the @ symbol and before .com contains a domain name that contains only letters.
```
    PATINDEX('[^a-z0-9]',SUBSTRING(email, CHARINDEX('@', email) + 1, CHARINDEX('.com', email) - CHARINDEX('@', email) - 1)) <= 0 -- The part after the @ symbol and before .com contains a domain name that contains only letters.
```
6. Return the result table ordered by user_id in ascending order.
```
ORDER BY user_id; -- Return the result table ordered by user_id in ascending order.
```



# Code
```mssql []
/* Write your T-SQL query statement below */
SELECT user_id, email
FROM Users
WHERE  
    LEN(email) - LEN(REPLACE(email, '@', '')) = 1 -- It contains exactly one @ symbol.
    AND
    email like '%.com' -- It ends with .com.
    AND
    PATINDEX('%[^a-z0-9_]%', SUBSTRING(email, 1, CHARINDEX('@', email) - 1)) <= 0  -- The part before the @ symbol contains only alphanumeric characters and underscores.
    AND
    PATINDEX('[^a-z0-9]',SUBSTRING(email, CHARINDEX('@', email) + 1, CHARINDEX('.com', email) - CHARINDEX('@', email) - 1)) <= 0 -- The part after the @ symbol and before .com contains a domain name that contains only letters.
ORDER BY user_id; -- Return the result table ordered by user_id in ascending order.


```
['Database', 'MS SQL Server']