Sponsored
Sponsored
This approach involves utilizing SQL queries to select the primary department for each employee effectively. The idea is to select the department_id directly if the primary_flag is 'Y'. For cases with a single department, we forcefully set the primary_flag to 'N' but choose that department.
Time Complexity: O(n), where n is the number of records in the Employee table. The query involves a single pass through the data for filtering and an additional pass for the group operation.
Space Complexity: O(n) for storing intermediate results during grouping.
1SELECT employee_id, department_id FROM Employee WHERE primary_flag = 'Y' OR (employee_id IN (
This solution makes use of a subquery to determine the employees who belong to only one department. We use a simple GROUP BY
and HAVING
clause to get such employees. The WHERE
clause merges this subquery result with employees who have their primary_flag set to 'Y'.
This approach leverages SQL subqueries to determine if an employee belongs to only one department and selects the sole department as the primary. For employees with more than one department, the department marked with 'Y' is considered primary.
Time Complexity: O(n), where n is the number of records in the Employee table. The inner subquery is efficient due to indexing and specific filtering.
Space Complexity: O(1) if indexing is used on employee_id and department_id.
1SELECT employee_id, (CASE WHEN COUNT(department_id) > 1 THEN (SELECT department_id FROM Employee e2 WHERE e1.employee_id = e2.employee_id AND e2.primary_flag = 'Y') ELSE MIN(department_id) END) as department_id FROM Employee e1 GROUP BY employee_id;
This query uses a CASE
statement to differentiate between employees with multiple departments and those with a single one. When there are multiple departments, the query picks the one with primary_flag = 'Y'
. For employees with only one department, it simply selects that department as primary.
This approach involves determining whether an employee has multiple departments or a single department by using aggregation. We can group the records by employee_id and use a COUNT function to find out the number of departments each employee belongs to. If an employee only belongs to one department, we select it regardless of the primary_flag. Otherwise, we pick the department where primary_flag is 'Y'.
Time Complexity: O(N), where N is the number of rows in the Employee table. This is due to the need to scan through all rows once for the aggregation.
Space Complexity: O(M), where M is the number of unique employee_ids due to the storage required for the result set.
1SELECT employee_id,
2 CASE WHEN COUNT(department_id) = 1
3 THEN MAX(department_id)
4 ELSE MAX(CASE WHEN primary_flag = 'Y' THEN department_id END)
5 END as department_id
6FROM Employee
7GROUP BY employee_id;
This SQL query uses a CASE statement, aggregation, and conditional max to determine which department an employee belongs to primarily. If the employee belongs to just one department, that department_id is selected. Else, it chooses the department with 'Y' as primary_flag.
This approach requires joining the Employee table with itself to identify employees with multiple department entries. We select the department where the primary_flag is 'Y'. For cases where all entries for an employee have primary_flag as 'N', the logic select the minimal department_id. This approach might involve more steps compared to the aggregation method but helps emphasize filtering techniques.
Time Complexity: O(N^2), where N is the number of rows in the Employee table due to the self-join operation.
Space Complexity: O(N) for storing results where N is the number of unique employee_ids.
1SELECT e1.employee_id,
2 COALESCE(e2.department_id, e1.department_id) as department_id
3FROM Employee e1
4LEFT JOIN Employee e2
5 ON e1.employee_id = e2.employee_id AND e2.primary_flag = 'Y'
6WHERE coalesce(e1.primary_flag, 'N') = 'N'
7 OR e2.department_id IS NOT NULL;
This SQL query performs a self-join on the Employee table to identify employees' primary departments. If no primary department is specified via the primary_flag, it defaults to any (here the minimal) department_id available for those employees. This takes advantage of the LEFT JOIN and COALESCE to ensure all employees are returned.