SQL Quest › SQL Interview Questions › String Functions
Email Username Extract (SUBSTR + INSTR)
Marketing wants the username portion of each email (the part before the @) for first-name guessing and personalization.
Use SUBSTR(email, 1, INSTR(email, '@') - 1) AS username. Show customer_id, email, username. Order by customer_id.
The -1 is critical: INSTR returns the position OF the @, so you need everything BEFORE it. Off-by-one here gives you john.smith@ instead of john.smith — the classic SUBSTR+INSTR bug.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
customers
| customer_id | name | signup_date | membership | total_orders | |
|---|---|---|---|---|---|
| 1 | John Smith | john.smith@email.com | 2023-01-15 | Gold | 15 |
| 2 | Emma Wilson | emma.wilson@email.com | 2023-03-20 | Silver | 8 |
| 3 | Michael Brown | michael.brown@email.com | 2023-02-10 | Gold | 12 |
Expected output: username = 'john.smith'
Hint
SELECT, SUBSTR, INSTR, and open the hint there if you stall.Concepts
SELECT SUBSTR INSTR String Functions
Practise the topic: SQL practice questions · String function practice
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test