SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed
SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog
Back to Blog
2026-01-08
2 min read

Data Masking and Anonymization Techniques in SQL

sqlsecurityprivacygdprpii

As developers and analysts, we often need real data to test features or run reports. But using production data with real names, emails, and phone numbers (PII) is a massive security risk and a violation of privacy laws like GDPR and CCPA.

Partially redacted PII with privacy shield
Partially redacted PII with privacy shield

In this guide, we'll cover SQL techniques to "Mask" or "Sanitize" data so you can use it safely.

1. Partial Redaction (Masking)

Useful for customer support UI—showing enough to verify (e.g., "ends in 4432") without revealing the whole value.

-- Convert 'john.doe@email.com' -> 'j*******@email.com'
SELECT
    SUBSTR(email, 1, 1) || '*******' || SUBSTR(email, INSTR(email, '@'))
FROM users;

2. Hashing (Pseudo-Anonymization)

If you need to join tables (e.g., orders to users) without exposing the user ID, you can use a cryptographic hash.

-- SHA2 returns a hex string. Same input always gives same output.
SELECT SHA2(email, 256) as user_hash FROM users;

Note: This is "pseudonymized", not fully anonymized, as rainbow tables can crack it. For better security, add a "salt" (random string) before hashing.

3. Data Generalization

Instead of showing exact values, show ranges.

  • Age 24 -> "20-30"
  • Zip 90210 -> "90xxx"

Interactive Playground

Here is a production_users table full of sensitive PII. Let's create a "Safe View" for the analytics team.

Interactive SQL
Loading...

Conclusion

Data privacy is not optional. By building these masking transformations into your ETL pipelines or database views, you ensure that analysts and developers can do their jobs without becoming a liability.

Share this article:

Related Articles

sqlsecurity

Dynamic SQL: Best Practices and Risks

Dynamic SQL is a double-edged sword. It offers incredible flexibility but opens the door to security nightmares. Learn how to use it safely.

Read more
securitysql

Preventing SQL Injection: A Developer's Guide

Learn how SQL injection attacks work and how to protect your applications with parameterized queries and best practices.

Read more
sql

SQL for Data Analysis: The Ultimate Guide

Move beyond basic SELECTs. Master the core SQL techniques for real-world data analysis: Data Cleaning, Time-Series Analysis, Window Functions, and Cohort Analysis.

Read more
Previous

Calculating Customer Lifetime Value (CLV) in SQL

Next

Mastering Slowly Changing Dimensions (SCD Type 2) in SQL

Comments

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed