Reviewed by Aditya Kumar · Last reviewed 2026-08-08
Use REPLACE for literal substrings and a regex function when the pattern varies. Dialect differences matter here REPLACE is portable and behaves the same nearly everywhere. The regex function is not:…
This easy-level SQL question appears frequently in data engineering interviews at companies like Incedo. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (bigquery, sql) will help you answer variations of this question confidently.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
Use REPLACE for literal substrings and a regex function when the pattern varies.
-- Literal: strip dashes from phone numbers
SELECT REPLACE(phone, '-', '') AS phone_clean
FROM customers;
-- Regex: collapse any run of whitespace to a single space
SELECT REGEXP_REPLACE(address, '\s+', ' ', 'g') AS address_clean
FROM customers;
REPLACE is portable and behaves the same nearly everywhere. The regex function is not:
REGEXP_REPLACE(col, pattern, replacement, 'g'), where the g flag is required or only the first match changes.REGEXP_REPLACE(col, pattern, replacement) and replace all matches by default.regexp_replace(col, pattern, replacement), also global by default.Naming the dialect you are targeting is worth a point on its own.
Both functions return NULL when the input is NULL rather than an empty string. If the column is nullable and downstream logic expects text, wrap it: COALESCE(REPLACE(col, 'x', 'y'), ''). Forgetting this is how NULLs leak into a supposedly cleaned column.
REPLACE calls nest, and they evaluate inside out:
SELECT REPLACE(REPLACE(REPLACE(phone, '-', ''), '(', ''), ')', '')
FROM customers;
Order matters if one replacement can create text that a later replacement then matches. When you are only deleting or swapping individual characters, TRANSLATE does it in a single pass and is much easier to read.
Neither function is sargable. Wrapping a column in REPLACE inside a WHERE clause disables any index on that column and forces a full scan. If you filter on the cleaned value regularly, store it as a generated or materialised column and index that instead of cleaning on every read. Regex is also substantially more expensive than a literal replace, so do not reach for it when REPLACE suffices.
In the interview, also mention that repeated cleaning at query time is a signal the transformation belongs upstream in the pipeline.
Red Flag: Regex when simple REPLACE suffices—overhead. Pro-Move: 'REPLACE for known patterns; regex only in transform layer with validation.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.