Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Conditional Masking Query

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Conditional Masking Query. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Virtusa's workforce analytics team needs a privacy-safe salary view for internal reporting. Write a PostgreSQL query that returns employee details, department names, and salary values masked according to gender.

Requirements

  1. Use a LEFT JOIN so employees remain in the output even when their department is missing or unknown.
  2. For Male, replace the final two salary digits with xx.
  3. For Female, replace the final three salary digits with xxx.
  4. For NULL or other gender values, return the salary as text without masking. Return NULL when salary is NULL, and order results by employee ID.

Schema

employees
ColumnTypeDescription
employee_idPKINTEmployee identifier
employee_nameVARCHAR(100)Employee full name
genderVARCHAR(20)Reported gender
salaryINTAnnual salary
department_idINTRelated department identifier
departments
ColumnTypeDescription
department_idPKINTDepartment identifier
department_nameVARCHAR(100)Department name
Tablesemployeesdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results