Applicants With a Valid College E-mail Address — SQL Easy Problem
The placement portal of Sahyadri Institute accepts only the institute's own e-mail addresses.
- Difficulty: Easy
- Topics: Strings
- Dialect: MySQL
- Problem: #42
Problem statement
The placement portal of Sahyadri Institute accepts only the institute's own e-mail addresses. An address is valid when:
- the part before the
@starts with a letter and contains only letters, digits, underscores_, periods.and hyphens-; - there is exactly one
@, followed by the domainsahyadri.eduand nothing else.
Return applicant_id, name and email for every applicant whose address is valid, in any order. An applicant with no address (email is NULL) is not valid.
Tables
Table: Applicant
| Column | Type |
|---|---|
| applicant_id | int |
| name | varchar |
| varchar |
Primary key: applicant_id.
email is the address the applicant typed, or NULL if they signed up with a phone number. Domains are always typed in small letters.
Examples
Example 1
Applicant
| applicant_id | name | |
|---|---|---|
| 1 | Aisha | aisha.khan@sahyadri.edu |
| 2 | Rohan | rohan_22@sahyadri.edu |
| 3 | Pooja | 2021pooja@sahyadri.edu |
| 4 | Karan | karan#m@sahyadri.edu |
| 5 | Mia | mia-d@sahyadri.edu.in |
| 6 | Dev | Dev-Rao@sahyadri.edu |
| 7 | Ira | NULL |
| 8 | Liam | liam@sahyadri-edu |
Output
| applicant_id | name | |
|---|---|---|
| 1 | Aisha | aisha.khan@sahyadri.edu |
| 2 | Rohan | rohan_22@sahyadri.edu |
| 6 | Dev | Dev-Rao@sahyadri.edu |
How to solve Applicants With a Valid College E-mail Address
Validation rules like these are a regular expression: email REGEXP pattern (or REGEXP_LIKE(email, pattern)) is true when the address matches the pattern. Build it piece by piece:
^anchors the match at the start, so nothing may come before the first letter;[a-zA-Z]is the required first letter —2021poojaand.devfail here;[a-zA-Z0-9_.-]*allows any number of the permitted characters. Inside brackets.is a literal period, and a-placed last is a literal hyphen;@sahyadri[.]eduis the domain, with its dot written as[.]so it means a period and not "any character" — a bare.would acceptsahyadri-edu;$anchors the end, rejectingsahyadri.edu.in.
@ is not in the allowed set, so a second @ can never sneak into the part before the domain. NULL addresses give a NULL match and drop out.
A more step-by-step alternative splits the address with SUBSTRING_INDEX: count the @s (CHAR_LENGTH(email) - CHAR_LENGTH(REPLACE(email, '@', '')) must be 1), compare the part after it with 'sahyadri.edu', and check the part before it with a smaller, anchored pattern. It is longer but easier to debug one rule at a time. Either way each address is scanned a constant number of times, so the query is linear in the total length of the addresses.
Reference solution (MySQL)
SELECT applicant_id, name, email
FROM Applicant
WHERE email REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@sahyadri[.]edu$'Another way
SELECT applicant_id, name, email FROM Applicant WHERE REGEXP_LIKE(email, '^[a-zA-Z][a-zA-Z0-9_.-]*@sahyadri[.]edu$')Another way
SELECT applicant_id, name, email FROM Applicant
WHERE CHAR_LENGTH(email) - CHAR_LENGTH(REPLACE(email, '@', '')) = 1
AND SUBSTRING_INDEX(email, '@', -1) = 'sahyadri.edu'
AND SUBSTRING_INDEX(email, '@', 1) REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*$'← Students Registered for a Machine Learning Elective · Canteen Items Sold on Each Day →