Compute the average number of connections per user

Quick Overview

Write a SQL query that computes the average number of connections per user, defined as total connection rows divided by distinct users, rounded to two decimals. It tests aggregate counts, COUNT DISTINCT, and avoiding integer division in PostgreSQL.

Compute the average number of connections per user

Company: LinkedIn

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Check the types Look at what type each count returns before you divide them. ``` ### 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.

Overview: Write a SQL query that computes the average number of connections per user, defined as total connection rows divided by distinct users, rounded to two decimals. It tests aggregate counts, COUNT DISTINCT, and avoiding integer division in PostgreSQL.

LinkedIn logo
LinkedIn
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

columntypemeaning
connection_idINTConnection identifier
user_idINTUser who made the connection
connected_user_idINTUser on the other side of the connection
connected_atTIMESTAMPWhen the connection was made

Primary key: connection_id. All columns are NOT NULL.

Output Contract

columntypemeaning
avg_connections_per_userNUMERICTotal 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_iduser_idconnected_user_idconnected_at
11012022024-01-15 10:00:00
21012032024-01-16 11:00:00
31022012024-01-17 09:30:00
41032042024-01-18 14:20:00
51032052024-01-19 15:45:00
61032062024-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.