Use it when you want to begin data transformation work without writing the first draft from scratch.
Data Cleaning in SQL AI Prompt
Write SQL to clean and standardize this dirty dataset. Issues identified: {{issues}} (nulls, duplicates, format inconsistencies, outliers, invalid values) Table: {{table}} Datab... Copy this prompt template, run it in your AI tool, and use related prompts to continue the workflow.
Write SQL to clean and standardize this dirty dataset.
Issues identified: {{issues}} (nulls, duplicates, format inconsistencies, outliers, invalid values)
Table: {{table}}
Database: {{database}}
1. NULL handling:
COALESCE(column, default_value): return first non-null value
NULLIF(column, 0): convert 0 (or empty string) to NULL
IS NULL / IS NOT NULL: never use = NULL
Replace NULL with 'Unknown':
COALESCE(status, 'Unknown') AS status
NULL-safe comparison:
WHERE status IS DISTINCT FROM 'cancelled'
-- equivalent to: WHERE status != 'cancelled' OR status IS NULL
2. String standardization:
Remove extra whitespace: REGEXP_REPLACE(name, '\s+', ' ', 'g')
Normalize email: TRIM(LOWER(email))
Extract only digits: REGEXP_REPLACE(phone, '[^0-9]', '', 'g')
Standardize state codes: UPPER(TRIM(state))
3. Date standardization:
-- Handle multiple date formats in one column (PostgreSQL):
CASE
WHEN raw_date ~ '^\d{4}-\d{2}-\d{2}$' THEN raw_date::date
WHEN raw_date ~ '^\d{2}/\d{2}/\d{4}$' THEN TO_DATE(raw_date, 'MM/DD/YYYY')
ELSE NULL
END AS clean_date
4. Outlier detection and flagging:
-- Flag values more than 3 standard deviations from the mean
SELECT *,
ABS(amount - AVG(amount) OVER ()) / NULLIF(STDDEV(amount) OVER (), 0) AS z_score
FROM transactions
WHERE ABS(amount - AVG(amount) OVER ()) / NULLIF(STDDEV(amount) OVER (), 0) > 3;
5. Invalid value replacement:
CASE
WHEN age < 0 OR age > 120 THEN NULL
ELSE age
END AS cleaned_age
6. Data profiling queries:
-- Column completeness
SELECT
COUNT(*) AS total_rows,
COUNT(email) AS non_null_email,
COUNT(DISTINCT email) AS unique_emails,
SUM(CASE WHEN email ~ '^[^@]+@[^@]+\.[^@]+$' THEN 0 ELSE 1 END) AS invalid_emails
FROM users;
Return: SQL cleaning queries for each identified issue, data profiling queries for quality assessment, and update statements to persist the cleaned values.When to use this prompt
Use it when you want a more consistent structure for AI output across projects or datasets.
Use it when you want prompt-driven work to turn into a reusable notebook or repeatable workflow later.
Use it when you want a clear next step into adjacent prompts in Data Transformation or the wider SQL Developer library.
What the AI should return
The AI should return a structured result that covers the main requested outputs, such as NULL handling:, String standardization:, Date standardization:. The final answer should stay clear, actionable, and easy to review inside a data transformation workflow for sql developer work.
How to use this prompt
Open your data context
Load your dataset, notebook, or working environment so the AI can operate on the actual project context.
Copy the prompt text
Use the copy button above and paste the prompt into the AI assistant or prompt input area.
Review the output critically
Check whether the result matches your data, assumptions, and desired format before moving on.
Chain into the next prompt
Once you have the first result, continue deeper with related prompts in Data Transformation.
Frequently asked questions
What does the Data Cleaning in SQL prompt do?+
It gives you a structured data transformation starting point for sql developer work and helps you move faster without starting from a blank page.
Who is this prompt for?+
It is designed for sql developer workflows and marked as intermediate, so it works well as a guided starting point for that level of experience.
What type of prompt is this?+
Data Cleaning in SQL is a single prompt. You can copy it as-is, adapt it, or use it as one step inside a larger workflow.
Can I use this outside MLJAR Studio?+
Yes. The prompt text works in other AI tools too, but MLJAR Studio is the best fit when you want local execution, visible Python code, and reusable notebooks.
What should I open next?+
Natural next steps from here are Pivoting and Unpivoting Data, String and Date Manipulation.