Compute the average connections per user: total rows in Connections divided by the number of distinct user_id values. Return a single column avg_connections_per_user, rounded to 2 decimals.
Input Table(s)
Connections
| column | type | meaning |
|---|
| connection_id | INT | Connection identifier |
| user_id | INT | User who made the connection |
| connected_user_id | INT | User on the other side of the connection |
| connected_at | TIMESTAMP | When the connection was made |
Primary key: connection_id. All columns are NOT NULL.
Output Contract
| column | type | meaning |
|---|
| avg_connections_per_user | NUMERIC | Total rows divided by distinct user_id values, rounded to 2 decimal places |
The result is exactly one row, so no ordering is needed.
Example
Connections
| connection_id | user_id | connected_user_id | connected_at |
|---|
| 1 | 101 | 202 | 2024-01-15 10:00:00 |
| 2 | 101 | 203 | 2024-01-16 11:00:00 |
| 3 | 102 | 201 | 2024-01-17 09:30:00 |
| 4 | 103 | 204 | 2024-01-18 14:20:00 |
| 5 | 103 | 205 | 2024-01-19 15:45:00 |
| 6 | 103 | 206 | 2024-01-20 16:00:00 |
Expected output:
| avg_connections_per_user |
|---|
| 2.00 |
There are 6 rows and 3 distinct user_id values (101, 102, 103), and 6 / 3 = 2.00. If a seventh row for user 102 were added, the result would be 7 / 3 = 2.33.
Constraints and Clarifications
-
The table has 1 to 100,000 rows, so there is at least one distinct
user_id
and the denominator is never zero.
-
The numerator is the raw row count. Every row counts, including a repeated pair and both directions of a mutual connection.
-
Only
user_id
values count toward the denominator. A user who appears only in
connected_user_id
is not counted.
-
No column is NULL.
-
Round to 2 decimal places with standard rounding (half away from zero), as PostgreSQL
ROUND
does for a numeric value.