Dynamic data masking

On this page 7

Dynamic data masking (DDM) is a database security feature that obscures sensitive values in query results at read time, according to rules defined on columns and the privileges of the user running the query, while the data stored on disk remains unchanged. In database security it limits the exposure of personal and financial data to people and applications that need to work with records but do not need to see full values.

Typical rules show only the last four digits of a card number, replace an email address with a pattern such as aXX@XXXX.com, or return zero instead of a salary. Major databases offer the feature under various names: dynamic data masking in SQL Server and Azure SQL Database, Data Redaction in Oracle, masking policies in Snowflake, and extensions or enterprise plugins for PostgreSQL and MySQL.

How dynamic data masking works

Dynamic data masking works by rewriting the output of a query, not the stored data. An administrator attaches a masking rule to a column. When a user without the right to see real values runs a query, the database engine applies the masking function to that column in the result set just before returning it.

Who sees what is controlled by a dedicated privilege. In SQL Server, users with the UNMASK permission see real data, and since SQL Server 2022 the permission can be granted at the database, schema, table or column level. In Snowflake, a masking policy is a function that evaluates the current role and returns either the real or the masked value, which allows conditional logic per role.

Rules are defined as column metadata, for example with ALTER TABLE ... ALTER COLUMN ... ADD MASKED WITH (FUNCTION = 'partial(0,"XXXX-XXXX-XXXX-",4)') in SQL Server. Adding or removing a mask is therefore instant and requires no data migration, and existing applications keep issuing the same queries without code changes.

The table below shows the standard masking functions in SQL Server, which illustrate the options most products provide.

FunctionExample outputTypical column
DefaultXXXX for text, 0 for numbersSalaries, notes
EmailjXXX@XXXX.comEmail addresses
PartialXXXX-XXXX-XXXX-1234Card and phone numbers
RandomA random number in a set rangeNumeric test values
DatetimeOnly the year shownBirth dates
Common masking functions

Dynamic masking compared with other protection methods

Dynamic data masking is one layer among several controls and is often mistaken for encryption. It protects only what users see in results; it does not protect files, backups or users who hold elevated privileges.

ControlWhat it protects againstStored data changedMain limitation
Dynamic data maskingCasual exposure in query resultsNoCan be bypassed by inference
Static data maskingExposure in test and development copiesYes, permanentlyCopy is no longer real data
Column-level encryptionReading values without the keyYes, encryptedComplicates queries and indexing
Transparent data encryptionTheft of data files and backupsEncrypted at rest onlyNo effect on authorized queries
Row-level securityAccess to rows of other tenants or regionsNoDoes not hide columns
Column-level securityAny access to a columnNoColumn is completely unavailable
Dynamic data masking and neighboring controls

The practical difference from column-level security is that a masked column can still be queried, joined and displayed, only with altered values. That lets applications keep working with the same SQL for all users, which is the main reason organizations choose masking over denying access.

Where dynamic data masking fits

Dynamic data masking fits best where many users need records but few need sensitive fields. The decision table below shows typical situations and the control that usually suits them.

SituationRecommended controlReason
Support agents viewing customer recordsDynamic data maskingPartial values are enough to verify identity
Developers needing realistic test dataStatic data maskingCopies leave production entirely
Regulated card data at restEncryption or tokenizationMasking alone does not meet storage rules
Multi-tenant applicationRow-level securityUsers must not see other tenants at all
Analysts building aggregate reportsMasking plus restricted rolesTotals remain usable without identifiers
Choosing the right control

Masking supports privacy principles such as data minimization under GDPR and similar regulations, because people see only what their task requires. It is, however, a complement to proper access control, never a substitute, and auditors usually expect it to be combined with database audit logging.

Inference attacks and other limitations

The most important limitation is that masking does not prevent inference. Filtering and comparison operate on real values, so a user who can run ad hoc queries can deduce hidden data, for example with WHERE Salary > 90000 repeated with different thresholds until the value is narrowed down.

WeaknessExampleMitigation
Inference through filtersRange queries on a masked salaryBlock ad hoc query access
Privileged usersAdministrators see real valuesSeparate duties and audit
Copying to unmasked tablesSELECT INTO by a user with write rightsRestrict write permissions
Application-level accountsOne service account with UNMASKPass the end user role to the database
Backups and exportsFull values in backup filesEncrypt backups and exports
Known weaknesses and mitigations

For these reasons, vendors themselves describe dynamic data masking as a way to reduce accidental exposure, not as protection against a determined attacker with query access. It works best behind an application that issues only parameterized queries and controls what users can ask.

Implementation practices

Successful deployments follow a few consistent practices.

  • Classify sensitive columns first, so masking rules follow a data inventory rather than guesswork.
  • Grant unmasking to roles, never to individual accounts, and review those roles regularly.
  • Test application behavior, because masked values may break validation or formatting logic.
  • Check performance on large result sets, where masking functions add a small processing cost.
  • Log access by unmasking roles to detect misuse early.

In simple terms

Think of a bank statement where only the last four digits of the account number are printed. The full number still exists at the bank. You simply see a partly hidden version.

Dynamic data masking does the same inside a database. Each person sees only as much of a sensitive value as their job requires. The real data stays untouched, and people with special permission can still see it in full.

A practical example

An insurance company runs a customer service application on SQL Server, used by about 300 call center agents. Agents need to find customers and confirm their identity, but they should not see full card numbers, birth dates or email addresses.

The database team applies a partial mask that shows the last four card digits, a datetime mask that shows only the birth year and an email mask. Agents verify callers by asking for those last four digits. Only the fraud investigation role receives UNMASK. The application code stays unchanged, and an internal privacy review confirms that routine screens no longer display full personal data.