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.

A permission system stores users, projects, and the access level each user has been granted on each project. Write a query that generates 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. Tables: - `users(user_id INTEGER PRIMARY KEY, username VARCHAR(100), is_active BOOLEAN)`: one row per user. `is_active` says whether the account is active. - `projects(project_id INTEGER PRIMARY KEY, project_name VARCHAR(100))`: one row per project. - `user_project_permissions(user_id INTEGER, project_id INTEGER, access_level VARCHAR(20), granted_at TIMESTAMP)`: the access a user holds on a project. `user_id` references `users.user_id`, `project_id` references `projects.project_id`, `access_level` is one of `'READ'`, `'WRITE'`, `'ADMIN'`, `'OWNER'`, and `granted_at` is 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: `OWNER` gives `'read,write,delete,admin'`, `ADMIN` gives `'read,write,delete'`, `WRITE` gives `'read,write'`, and `READ` gives `'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. 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 `user_project_permissions` is always one of the four listed values and is never NULL. - The permission strings are lowercase, comma-separated, with no spaces.

Tables

users(user_id INTEGER, username VARCHAR(100), is_active BOOLEAN)

projects(project_id INTEGER, project_name VARCHAR(100))

user_project_permissions(user_id INTEGER, project_id INTEGER, access_level VARCHAR(20), granted_at TIMESTAMP)

Hints

  1. 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.
AtlassianData ScientistmediumData Manipulation (SQL/Python)

User Project Access Matrix

A permission system stores users, projects, and the access level each user has been granted on each project. Write a query that generates 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.

Tables:

  • users(user_id INTEGER PRIMARY KEY, username VARCHAR(100), is_active BOOLEAN): one row per user. is_active says whether the account is active.
  • projects(project_id INTEGER PRIMARY KEY, project_name VARCHAR(100)): one row per project.
  • user_project_permissions(user_id INTEGER, project_id INTEGER, access_level VARCHAR(20), granted_at TIMESTAMP): the access a user holds on a project. user_id references users.user_id, project_id references projects.project_id, access_level is one of 'READ', 'WRITE', 'ADMIN', 'OWNER', and granted_at is 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: OWNER gives 'read,write,delete,admin', ADMIN gives 'read,write,delete', WRITE gives 'read,write', and READ gives '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.

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 user_project_permissions is always one of the four listed values and is never NULL.
  • The permission strings are lowercase, comma-separated, with no spaces.

Expected Output

Tables

usersprojectsuser_project_permissions
users
ColumnType
user_idINTEGER
usernameVARCHAR(100)
is_activeBOOLEAN
projects
ColumnType
project_idINTEGER
project_nameVARCHAR(100)
user_project_permissions
ColumnType
user_idINTEGER
project_idINTEGER
access_levelVARCHAR(20)
granted_atTIMESTAMP

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions