SQL QuestSQL 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".

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

Related questions

Department Roster with GROUP_CONCATMedium · FreeTitle Social Survival AnalysisMedium · FreeEmail Provider Customer AnalysisMedium · FreeTrust and Savings BanksMedium · FreeLLC vs Individual BuyersMedium · FreeLong-Named PassengersEasy · Free

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