Temotec Data Science, ML & Data Engineering: Interview Notes - Projects - Courses.’s Post

📢 Hello LinkedIn community! 🌟💼 🌟 Day 40/50 Days of SQL Challenge 🌟 SQL Course 2024: SQL for Data Analysis and Data Science. https://lnkd.in/g45cbiXa SQL Bootcamp 2024: Master SQL & PostgreSQL - Hands-On Course https://lnkd.in/gaHnijmg SQL Introduction Course 2024: SQL Crash Course. https://lnkd.in/dFyqBHBB Today, let's solve a SQL challenge that involves reporting the number of employees who report to each manager, along with the average age of those reports. 📊👥 Table: Employees +-------------+----------+ | Column Name | Type     | +-------------+----------+ | employee_id | int      | | name        | varchar  | | reports_to  | int      | | age         | int      | +-------------+----------+ We have a table called Employees with several columns: employee_id, name, reports_to, and age. The employee_id column has unique values, and the reports_to column represents the manager's ID to whom the employee reports. Some employees may not report to anyone, indicated by a NULL value in the reports_to column. Here's the SQL query that will provide the desired results: SELECT e1.employee_id, e1.name, COUNT(*) AS reports_count, ROUND(AVG(e2.age)) AS average_age FROM Employees e1 JOIN Employees e2 ON e1.employee_id = e2.reports_to GROUP BY e1.employee_id, e1.name ORDER BY e1.employee_id; In this query, we perform a self-join on the Employees table to match each employee with their respective manager using the employee_id and reports_to columns. Then, we use the GROUP BY clause to group the records by the manager's employee_id and name. Within each group, we use the COUNT(*) function to count the number of reports for each manager and the AVG(e2.age) function to calculate the average age of the reports. The ROUND function is used to round the average age to the nearest integer. Finally, we order the result by the manager's employee_id in ascending order. Example result: +-------------+-------+---------------+-------------+ | employee_id | name | reports_count | average_age | +-------------+-------+---------------+-------------+ | 9 | Hercy | 2 | 39 | +-------------+-------+---------------+-------------+ In this example, employee Hercy with ID 9 has 2 direct reports (Alice and Bob). The average age of the reports is calculated as (41+36)/2 = 38.5, rounded to 39. #SQL hashtag #Databases hashtag #DataAnalysis hashtag #LeetCode hashtag #SQL hashtag #DataAnalysis hashtag #DataManipulation hashtag #LinkedInLearning hashtag #ContinuousLearning hashtag #Temotec hashtag #TemotecAcademy hashtag #TamerAhmed hashtag #50DaysOFSQL hashtag #50daysofsqls hashtag #SQLChallenges hashtag #DataAnalysis hashtag #SQLQueries hashtag #DataManipulation hashtag #ProblemSolving hashtag #DataDrivenInsights hashtag

SQL Course 2024: SQL for Data Analysis and Data Science.

SQL Course 2024: SQL for Data Analysis and Data Science.

udemy.com

To view or add a comment, sign in

Explore topics