{ "data": { "question": { "questionId": "1882", "questionFrontendId": "1731", "boundTopicId": null, "title": "The Number of Employees Which Report to Each Employee", "titleSlug": "the-number-of-employees-which-report-to-each-employee", "content": "
Table: Employees
\n+-------------+----------+\n| Column Name | Type |\n+-------------+----------+\n| employee_id | int |\n| name | varchar |\n| reports_to | int |\n| age | int |\n+-------------+----------+\nemployee_id is the column with unique values for this table.\nThis table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null). \n\n\n
\n\n
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
\n\nWrite a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
\n\nReturn the result table ordered by employee_id
.
The result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nEmployees table:\n+-------------+---------+------------+-----+\n| employee_id | name | reports_to | age |\n+-------------+---------+------------+-----+\n| 9 | Hercy | null | 43 |\n| 6 | Alice | 9 | 41 |\n| 4 | Bob | 9 | 36 |\n| 2 | Winston | null | 37 |\n+-------------+---------+------------+-----+\nOutput: \n+-------------+-------+---------------+-------------+\n| employee_id | name | reports_count | average_age |\n+-------------+-------+---------------+-------------+\n| 9 | Hercy | 2 | 39 |\n+-------------+-------+---------------+-------------+\nExplanation: Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 374, "dislikes": 52, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "{\"headers\":{\"Employees\":[\"employee_id\",\"name\",\"reports_to\",\"age\"]},\"rows\":{\"Employees\":[[9,\"Hercy\",null,43],[6,\"Alice\",9,41],[4,\"Bob\",9,36],[2,\"Winston\",null,37]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef count_employees(employees: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"65.1K\", \"totalSubmission\": \"145.3K\", \"totalAcceptedRaw\": 65097, \"totalSubmissionRaw\": 145261, \"acRate\": \"44.8%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Employees\":[\"employee_id\",\"name\",\"reports_to\",\"age\"]},\"rows\":{\"Employees\":[[9,\"Hercy\",null,43],[6,\"Alice\",9,41],[4,\"Bob\",9,36],[2,\"Winston\",null,37]]}}", "metaData": "{\"mysql\": [\"Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)\"], \"mssql\": [\"Create table Employees(employee_id int, name varchar(20), reports_to int, age int)\"], \"oraclesql\": [\"Create table Employees(employee_id int, name varchar(20), reports_to int, age int)\"], \"database\": true, \"name\": \"count_employees\", \"pythondata\": [\"Employees = pd.DataFrame([], columns=['employee_id', 'name', 'reports_to', 'age']).astype({'employee_id':'Int64', 'name':'object', 'reports_to':'Int64', 'age':'Int64'})\"], \"postgresql\": [\"Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)\"], \"database_schema\": {\"Employees\": {\"employee_id\": \"INT\", \"name\": \"VARCHAR(20)\", \"reports_to\": \"INT\", \"age\": \"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)", "Truncate table Employees", "insert into Employees (employee_id, name, reports_to, age) values ('9', 'Hercy', 'None', '43')", "insert into Employees (employee_id, name, reports_to, age) values ('6', 'Alice', '9', '41')", "insert into Employees (employee_id, name, reports_to, age) values ('4', 'Bob', '9', '36')", "insert into Employees (employee_id, name, reports_to, age) values ('2', 'Winston', 'None', '37')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.
Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0
\"], \"postgresql\": [\"PostgreSQL\", \"PostgreSQL 16
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }