SQL Quest › SQL Interview Questions › String Functions

Email Username Extract (SUBSTR + INSTR)

MediumFreeQuerying BasicsString Functions

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". Free accounts get 10 free challenge solves, and this can be one of them.

Schema

customers

customer_idnameemailsignup_datemembershiptotal_orders
1John Smithjohn.smith@email.com2023-01-15Gold15
2Emma Wilsonemma.wilson@email.com2023-03-20Silver8
3Michael Brownmichael.brown@email.com2023-02-10Gold12

Expected output: username = 'john.smith'

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for 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

Read the concept: SQL cheat sheet

Related questions

Year Code from Order Date (SUBSTR)Easy · FreePosition of '@' in Email (INSTR)Easy · FreeEmail Provider Customer AnalysisMedium · FreeNames That Don't Match Their EmailMedium · FreePassenger Family Survival AnalysisHard · ProHer Own First NameHard · Pro

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