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

📢 Hello LinkedIn community! 🌟💼 🌟 Day 41/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 primary department for each employee. 📊👥 The combination of employee_id and department_id forms the primary key for this table. The primary_flag column is an ENUM (category) with values 'Y' or 'N'. If the primary_flag is 'Y', it indicates that the department is the primary department for the employee, and if it's 'N', it means the department is not the primary. Employees can belong to multiple departments, and when they join other departments, they need to specify their primary department. Note that when an employee belongs to only one department, their primary_flag is 'N'. Solution: SELECT employee_id, CASE WHEN COUNT(*) > 1 AND COUNT(CASE WHEN primary_flag = 'Y' THEN 1 END) = 1 THEN MAX(CASE WHEN primary_flag = 'Y' THEN department_id END) WHEN COUNT(*) = 1 THEN MAX(department_id) ELSE NULL END AS department_id FROM Employee GROUP BY employee_id HAVING department_id IS NOT NULL; we group the records by employee_id. We use a CASE statement to handle different scenarios. If an employee belongs to more than one department and has exactly one department marked as the primary (primary_flag = 'Y'), we select that department as their primary department. If an employee belongs to only one department, we select their only department as their primary department. If an employee belongs to multiple departments but doesn't have a primary department (primary_flag = 'Y' for any department), we return NULL for their department. The HAVING clause is used to filter out the employees who don't have a primary department (NULL department_id). Result: +-------------+---------------+ | employee_id | department_id | +-------------+---------------+ | 1 | 1 | | 2 | 1 | | 3 | 3 | | 4 | 3 | +-------------+---------------+ employee 1 has only one department (department 1), and it is considered their primary department. Employee 2 belongs to departments 1 and 2, but department 1 is marked as their primary department. Employee 3 belongs to department 3, and it is their primary department. Employee 4 belongs to departments 2, 3, and 4, but department 3 is marked as their primary department. #SQL hashtag #Databases hashtag #DataAnalysis hashtag #LeetCode hashtag #SQL hashtag #DataAnalysis hashtag #DataManipulation hashtag #ContinuousLearning hashtag #Temotec hashtag #TemotecAcademy hashtag #TamerAhmed hashtag #50DaysOFSQL hashtag #50daysofsqls hashtag #SQLChallenges hashtag #DataAnalysis hashtag #SQLQueries hashtag #DataManipulation hashtag #ProblemSolving 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