Dynamic Data Masking
This lesson explains the masking functions, granular UNMASK permissions and the limits of masking.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a masking function for a display requirement.
- Explain granular UNMASK permissions.
- Identify why Dynamic Data Masking is not a security boundary.
Masking functions
A mask is defined on a column with MASKED WITH (FUNCTION = '...'), at creation or with ALTER COLUMN. Four functions are available, as the following table shows.
| Function | Result for users without UNMASK |
|---|---|
| default() | XXXX for strings, 0 for numbers, 1900-01-01 for dates, empty for binary |
| email() | First letter, then [email protected] |
| random(start, end) | A random number in the range, for numeric columns |
| partial(prefix, "padding", suffix) | The first prefix and last suffix characters with custom padding between |
ALTER TABLE Sales.Customer ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()');
ALTER TABLE Sales.Customer ALTER COLUMN Phone ADD MASKED WITH (FUNCTION = 'partial(0,"XXX-XXX-",4)');
ALTER TABLE Sales.Customer ALTER COLUMN CreditLimit ADD MASKED WITH (FUNCTION = 'random(1, 10)');Masking functions
UNMASK and administration
Users with the UNMASK permission see real values. In Azure SQL Database, SQL database in Fabric and SQL Server 2022 and later, UNMASK is granular: it can be granted on the database, a schema, a table or a single column, for example GRANT UNMASK ON Sales.Customer(Email) TO SupportLead. ALTER ANY MASK lets a principal add, change or remove masks. Administrators and the database owner always see unmasked data. Masks apply to query results, so a masked user who copies data with SELECT INTO or INSERT ... SELECT stores the masked values.
Who sees real values?
Limits of masking
Dynamic Data Masking is not encryption: data is stored and transmitted inside the engine in plaintext, and it is not a complete security boundary. A user who can run ad hoc queries can infer masked values with predicates, such as WHERE Salary BETWEEN 90000 AND 90010, because filtering uses real values. DDM is designed to limit exposure in applications and to support staff, combined with permissions that block ad hoc querying, Row-Level Security and auditing. Change event streaming sends unmasked values, and masks cannot be defined on external table columns or Always Encrypted columns.
What masking does not do
Support staff and card numbers
Support agents must see only the last four digits of customers' card numbers, while the fraud team must see the full number. Both groups use the same application, which connects with each user's Microsoft Entra identity. What should the developer configure?
Add a partial(0,"XXXX-XXXX-XXXX-",4) mask to the card number column and grant UNMASK on that column to the fraud team's database role. Support agents then see masked values without application changes. The default() function would hide all digits, and granting UNMASK at database level would also expose other masked columns to the fraud team. If agents could run ad hoc queries, card data would also need encryption, because masking does not stop inference.
A masked user runs SELECT INTO to copy a masked column. What ends up in the new table?
Show answer
The masked values, because masking is applied to the query results the user receives.
Exam Tips
Remember for the exam
- Show only the last digits: partial(); hide fully: default(); emails: email().
- Granular UNMASK at database, schema, table or column level.
- DDM does not stop inference through WHERE clauses; it is not encryption.
- Manage masks: ALTER ANY MASK.