You are given a table user_permissions that contains user IDs and their permissions encoded as integers. Each bit in the permissions integer represents a different access level or feature that a user has.
Write a SQL query to calculate:
- common_perms: The access level granted to all users. This is computed using a bitwise AND operation on all permissions.
- any_perms: The access level granted to any user. This is computed using a bitwise OR operation on all permissions.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | Primary key, unique identifier for each user |
permissions
|
int | Integer encoding user permissions where each bit represents a different access level |
Input & Output
| user_id | permissions |
|---|---|
| 1 | 5 |
| 2 | 12 |
| 3 | 7 |
| 4 | 3 |
| common_perms | any_perms |
|---|---|
| 0 | 15 |
Binary Analysis:
- User 1: 5 = 0101 (binary)
- User 2: 12 = 1100 (binary)
- User 3: 7 = 0111 (binary)
- User 4: 3 = 0011 (binary)
common_perms (BIT_AND): 0101 & 1100 & 0111 & 0011 = 0000 = 0
any_perms (BIT_OR): 0101 | 1100 | 0111 | 0011 = 1111 = 15
| user_id | permissions |
|---|---|
| 1 | 7 |
| 2 | 3 |
| 3 | 11 |
| common_perms | any_perms |
|---|---|
| 3 | 15 |
Binary Analysis:
- User 1: 7 = 0111 (binary)
- User 2: 3 = 0011 (binary)
- User 3: 11 = 1011 (binary)
common_perms (BIT_AND): 0111 & 0011 & 1011 = 0011 = 3 (bits 0 and 1 are common to all users)
any_perms (BIT_OR): 0111 | 0011 | 1011 = 1111 = 15 (at least one user has each permission)
| user_id | permissions |
|---|---|
| 1 | 9 |
| common_perms | any_perms |
|---|---|
| 9 | 9 |
Single User Case: When there's only one user, both BIT_AND and BIT_OR return the same value - the user's permissions (9 = 1001 in binary). The common permissions and any permissions are identical.
Constraints
-
1 ≤ user_id ≤ 1000 -
0 ≤ permissions ≤ 2^31 - 1 - At least one user exists in the table