ALTER TABLE Customers
ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()');
Topics
43
Aggregate Functions & GROUP BY
Always On Availability Groups
Backup & Recovery
Change Data Capture (CDC)
Columnstore Indexes
Common Table Expressions (CTEs)
Constraints (Primary Key, Foreign Key, Check & Unique)
Cursors
Data Types & Schema Design
Deadlocks
Dynamic Data Masking
Dynamic SQL
Error Handling with TRY CATCH
Full Text Search
Hash Indexes & Hash Join Operations
Indexes
In-Memory OLTP (Memory-Optimized Tables)
Isolation & Locking
Joins
JSON Support in SQL Server
Linked Servers
Merge Statement (Upsert)
Normalization
Partitioning
Pivoting & Unpivoting Data
Query Optimization & Plans
Query Store
Replication
SQL Server Agent & Job Scheduling
SQL Server Architecture & Editions
SQL Server Profiler & Extended Events
SQL Server Security & Permissions
Stored Procedures & Functions
Subqueries
Temporary Tables & Table Variables
Transactions & ACID
Transparent Data Encryption
Triggers
T-SQL Fundamentals & Syntax
User Defined Functions
Views
Window Functions
XML Data Type & Querying
Dynamic Data Masking
7 questions found
Dynamic data masking automatically hides sensitive information, like a full credit card number or an email address, from users who query a table but do not have special permission to see the unmasked value, without actually changing the real data stored in the database, protecting sensitive information from being casually viewed by everyone with query access.
Real-world example
A customer support team can look up customer records to help with orders, but sees a masked version of each customer's email address instead of the real value, protecting customer privacy while still letting them do their job.
SQL Server Security & Permissions;Transparent Data Encryption
SQL Server provides several built-in masking functions including default, which fully hides a value based on its data type, email, which partially masks an email address, random, which replaces a numeric value with a random number within a specified range, and partial, which lets you define a custom pattern showing only certain characters.
ALTER TABLE Customers
ALTER COLUMN Phone ADD MASKED WITH (FUNCTION = 'partial(0,"XXX-XXX-",4)');
Real-world example
A company masks customer phone numbers so that support staff without special permission only see the last four digits, following a partial masking pattern that hides most of the number while still allowing basic identification.
SQL Server Security & Permissions;Data Types & Schema Design
How do you grant a specific user or role permission to see the real, unmasked data despite dynamic data masking being applied?
IntermediateYou grant the UNMASK permission to a specific user or role, either at the database level to allow them to see all masked columns, or in more recent versions, at a more granular column level, letting only trusted individuals like database administrators or compliance officers view the actual sensitive values.
GRANT UNMASK TO ComplianceOfficer;
Real-world example
A financial company grants the UNMASK permission only to its compliance team, ensuring that regular customer service staff continue to see masked sensitive data while auditors can view the real values when legally required.
SQL Server Security & Permissions;Data Types & Schema Design
Does dynamic data masking prevent a determined user from working around it to see the real underlying values?
IntermediateDynamic data masking is designed to prevent casual exposure of sensitive data in typical query results, but it is not a strong security boundary on its own, since a user with sufficient query permissions might still be able to infer real values through techniques like brute force guessing combined with a WHERE clause, so it should be combined with proper permission management and encryption for genuinely sensitive data.
-- A user could potentially guess values using WHERE clauses
-- if they have enough query attempts and permissions
SELECT * FROM Customers WHERE Email = 'guessedvalue@example.com';
Real-world example
A security team recognizes that dynamic data masking alone would not stop a malicious insider with query access from guessing values through repeated queries, so they also implement row level security and strict permission controls as additional protection.
Row Level Security;SQL Server Security & Permissions
How would you apply dynamic data masking to an existing table with sensitive columns as part of a broader compliance initiative, such as meeting data privacy regulations?
AdvancedYou would first identify all columns containing sensitive information such as national identification numbers, emails, or phone numbers, apply the most appropriate masking function to each one based on its data type and sensitivity, carefully audit who currently has UNMASK permission, and document the masking strategy as part of your organization's overall compliance evidence.
ALTER TABLE Customers
ALTER COLUMN NationalId ADD MASKED WITH (FUNCTION = 'default()');
ALTER TABLE Customers
ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()');
Real-world example
A healthcare company applies dynamic data masking across several sensitive columns in its patient database as part of a broader compliance effort, carefully documenting which roles have UNMASK permission for an upcoming audit.
SQL Server Security & Permissions;Row Level Security
How does dynamic data masking behave differently when a masked column is used in a JOIN, WHERE clause, or exported through an application, compared to a simple SELECT query?
AdvancedDynamic data masking only affects the data returned in the actual result set displayed to an unauthorized user, meaning the real underlying values are still used internally for operations like joins and filtering, which is important to understand since masking does not prevent the real data from influencing query logic, only from being visually displayed.
-- The real email value is used for the join and filter,
-- but the displayed result will still be masked
SELECT c.Name, c.Email
FROM Customers c
JOIN Orders o ON c.CustomerId = o.CustomerId
WHERE c.Email = 'realvalue@example.com';
Real-world example
A developer is surprised to find a WHERE clause filtering on a masked email column still works correctly using the real value, learning that masking only affects what is displayed, not the underlying query logic.
SQL Server Security & Permissions;Joins
You use an ALTER TABLE statement with the ALTER COLUMN clause and the DROP MASKED option, which removes the masking function from that column so all users with normal query permission will once again see the real, unmasked value.
ALTER TABLE Customers
ALTER COLUMN Email DROP MASKED;
Real-world example
A team removes dynamic data masking from an internal test database's email column, since the fake test data in that specific environment does not need to be protected the way real production customer data does.
SQL Server Security & Permissions;Data Types & Schema Design