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.

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 `connections` | column | type | meaning | |---|---|---| | connection_id | INTEGER | Connection identifier | | user_id | INTEGER | User who made the connection | | connected_user_id | INTEGER | 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. ### 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. ### Example With 6 rows and 3 distinct `user_id` values (101, 102, 103), the result is 6 / 3 = 2.00. If a seventh row for user 102 were added, the result would be 7 / 3 = 2.33.

Tables

connections(connection_id INTEGER, user_id INTEGER, connected_user_id INTEGER, connected_at TIMESTAMP)

Hints

  1. Look at what type each count returns before you divide them.
  2. Only `user_id` decides who counts as a user, and every row counts toward the total, repeats included.
LinkedInData ScientisteasyData Manipulation (SQL/Python)

Average Connections per User

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

connections

columntypemeaning
connection_idINTEGERConnection identifier
user_idINTEGERUser who made the connection
connected_user_idINTEGERUser 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.

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.

Example

With 6 rows and 3 distinct user_id values (101, 102, 103), the result is 6 / 3 = 2.00. If a seventh row for user 102 were added, the result would be 7 / 3 = 2.33.

Expected Output

Tables

connections
connections
ColumnType
connection_idINTEGER
user_idINTEGER
connected_user_idINTEGER
connected_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