Build a user-by-project access matrix with effective permissions in SQL

Quick Overview

Write a SQL query that builds an access matrix with one row for every user and project pair, showing each access level, the permissions it implies, and when it was granted, with a default for pairs that have no grant. It tests generating every combination of two tables, outer joins on a composite key, and mapping values with CASE and COALESCE.

Build a user-by-project access matrix with effective permissions in SQL

Company: Atlassian

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A permission system stores users, projects, and the access level each user has been granted on each project. Generate an access matrix with one row for every (user, project) combination, including pairs where the user was never granted anything. For each pair, show the access level, the effective permissions that level implies, and when the access was granted. ```hint Start from the row count Work out how many rows the output must have before you write any join, then choose joins that produce exactly that many. ``` ### Input Table(s) Table `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | User identifier | | username | VARCHAR | User name | | is_active | BOOLEAN | Whether the account is active | Primary key: `user_id`. Table `projects` | Column | Type | Meaning | |---|---|---| | project_id | INT | Project identifier | | project_name | VARCHAR | Project name | Primary key: `project_id`. Table `user_project_permissions` | Column | Type | Meaning | |---|---|---| | user_id | INT | User holding the access, references `users.user_id` | | project_id | INT | Project the access applies to, references `projects.project_id` | | access_level | VARCHAR | One of `'READ'`, `'WRITE'`, `'ADMIN'`, `'OWNER'` | | granted_at | TIMESTAMP | When the access was granted | `(user_id, project_id)` is unique. ### Output Contract - Columns, in this order: `user_id`, `username`, `project_id`, `project_name`, `access_level`, `effective_permissions`, `granted_at`. - One row for every (user, project) pair, so the row count is the number of users times the number of projects. - `effective_permissions` is derived from the access level: | access_level | effective_permissions | |---|---| | OWNER | read,write,delete,admin | | ADMIN | read,write,delete | | WRITE | read,write | | READ | read | - When a pair has no row in `user_project_permissions`: `access_level` is `'NO_ACCESS'`, `effective_permissions` is `'none'`, and `granted_at` is NULL. - `granted_at` is returned as stored, with no formatting. - Order by `user_id` ascending, then `project_id` ascending. That pair is unique in the output. ### Example Input `users`: | user_id | username | is_active | |---|---|---| | 1 | alice | true | | 2 | bob | false | | 3 | carol | true | Input `projects`: | project_id | project_name | |---|---| | 10 | Apollo | | 20 | Borealis | Input `user_project_permissions`: | user_id | project_id | access_level | granted_at | |---|---|---|---| | 1 | 10 | OWNER | 2024-01-05 09:00:00 | | 1 | 20 | READ | 2024-02-01 14:30:00 | | 2 | 20 | WRITE | 2024-03-10 08:15:00 | | 3 | 10 | ADMIN | 2024-03-12 16:45:00 | Expected result: | user_id | username | project_id | project_name | access_level | effective_permissions | granted_at | |---|---|---|---|---|---|---| | 1 | alice | 10 | Apollo | OWNER | read,write,delete,admin | 2024-01-05 09:00:00 | | 1 | alice | 20 | Borealis | READ | read | 2024-02-01 14:30:00 | | 2 | bob | 10 | Apollo | NO_ACCESS | none | NULL | | 2 | bob | 20 | Borealis | WRITE | read,write | 2024-03-10 08:15:00 | | 3 | carol | 10 | Apollo | ADMIN | read,write,delete | 2024-03-12 16:45:00 | | 3 | carol | 20 | Borealis | NO_ACCESS | none | NULL | Three users and two projects give six rows. Bob has no grant on Apollo and Carol has none on Borealis, so those rows show `NO_ACCESS`, `none` and a NULL `granted_at`. Bob is inactive and still appears. ### Constraints and Clarifications - `users` has at most 100 rows and `projects` at most 50, so the output has at most 5,000 rows. - Include every user regardless of `is_active`. - `(user_id, project_id)` is unique in `user_project_permissions`, so a pair has at most one access level. - Every `user_id` and `project_id` in `user_project_permissions` references an existing user and project. - `access_level` in the permissions table is always one of the four listed values and is never NULL. - The permission strings are lowercase, comma-separated, with no spaces.

Overview: Write a SQL query that builds an access matrix with one row for every user and project pair, showing each access level, the permissions it implies, and when it was granted, with a default for pairs that have no grant. It tests generating every combination of two tables, outer joins on a composite key, and mapping values with CASE and COALESCE.

Atlassian logo
Atlassian
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A permission system stores users, projects, and the access level each user has been granted on each project. Generate an access matrix with one row for every (user, project) combination, including pairs where the user was never granted anything. For each pair, show the access level, the effective permissions that level implies, and when the access was granted.

Input Table(s)

Table users

ColumnTypeMeaning
user_idINTUser identifier
usernameVARCHARUser name
is_activeBOOLEANWhether the account is active

Primary key: user_id.

Table projects

ColumnTypeMeaning
project_idINTProject identifier
project_nameVARCHARProject name

Primary key: project_id.

Table user_project_permissions

ColumnTypeMeaning
user_idINTUser holding the access, references users.user_id
project_idINTProject the access applies to, references projects.project_id
access_levelVARCHAROne of 'READ', 'WRITE', 'ADMIN', 'OWNER'
granted_atTIMESTAMPWhen the access was granted

(user_id, project_id) is unique.

Output Contract

  • Columns, in this order: user_id , username , project_id , project_name , access_level , effective_permissions , granted_at .
  • One row for every (user, project) pair, so the row count is the number of users times the number of projects.
  • effective_permissions is derived from the access level:
access_leveleffective_permissions
OWNERread,write,delete,admin
ADMINread,write,delete
WRITEread,write
READread
  • When a pair has no row in user_project_permissions : access_level is 'NO_ACCESS' , effective_permissions is 'none' , and granted_at is NULL.
  • granted_at is returned as stored, with no formatting.
  • Order by user_id ascending, then project_id ascending. That pair is unique in the output.

Example

Input users:

user_idusernameis_active
1alicetrue
2bobfalse
3caroltrue

Input projects:

project_idproject_name
10Apollo
20Borealis

Input user_project_permissions:

user_idproject_idaccess_levelgranted_at
110OWNER2024-01-05 09:00:00
120READ2024-02-01 14:30:00
220WRITE2024-03-10 08:15:00
310ADMIN2024-03-12 16:45:00

Expected result:

user_idusernameproject_idproject_nameaccess_leveleffective_permissionsgranted_at
1alice10ApolloOWNERread,write,delete,admin2024-01-05 09:00:00
1alice20BorealisREADread2024-02-01 14:30:00
2bob10ApolloNO_ACCESSnoneNULL
2bob20BorealisWRITEread,write2024-03-10 08:15:00
3carol10ApolloADMINread,write,delete2024-03-12 16:45:00
3carol20BorealisNO_ACCESSnoneNULL

Three users and two projects give six rows. Bob has no grant on Apollo and Carol has none on Borealis, so those rows show NO_ACCESS, none and a NULL granted_at. Bob is inactive and still appears.

Constraints and Clarifications

  • users has at most 100 rows and projects at most 50, so the output has at most 5,000 rows.
  • Include every user regardless of is_active .
  • (user_id, project_id) is unique in user_project_permissions , so a pair has at most one access level.
  • Every user_id and project_id in user_project_permissions references an existing user and project.
  • access_level in the permissions table is always one of the four listed values and is never NULL.
  • The permission strings are lowercase, comma-separated, with no spaces.