Recent Interview Question for Data Analyst Role
Question 1) You have two tables:
Employee:-
Columns: EID (Employee ID), ESalary (Employee Salary)
empdetails:-
Columns: EID (Employee ID), EDOB (Employee Date of Birth)
Your task is to:
1) Identify all employees whose salary (ESalary) is an odd number?
2) Retrieve the date of birth (EDOB) for these employees from the empdetails table.
How would you write a SQL query to achieve this?
SELECT e.EID, ed.EDOB
FROM (
SELECT EID
FROM Employee
WHERE ESalary % 2 <> 0
) e
JOIN empdetails ed ON e.EID = ed.EID;
Explanation of the query :-
Filter Employees with Odd Salaries:
The subquery SELECT EID FROM Employee WHERE ESalary % 2 <> 0 filters out Employee IDs (EID) where the salary (ESalary) is an odd number. The modulo operator % checks if ESalary divided by 2 leaves a remainder (<>0).
Merge with empdetails:
The main query then takes the filtered Employee IDs from the subquery and performs a join with the empdetails table using the EID column. This retrieves the date of birth (EDOB) for these employees.
Hope this helps you 😊
Post #736
2.05K
- 👍 3