SQL & Python Interview Q&A
SQL & Python Interview Q&A
1. Find the employee(s) with the second highest salary in the company
2. Find employees who earn more than the average salary of their department
Pandas: avg_salary_by_dept =
employees.groupby('department_id')['salary'].transform('mean')
higher_than_avg = employees[employees['salary'] > avg_salary_by_dept]
3. Identify the employees who have the longest tenure in each department
Pandas: longest_tenure =
employees.loc[employees.groupby('department_id')['hire_date'].idxmin()]
Pandas: highest_salary_per_dept =
employees.loc[employees.groupby('department_id')['salary'].idxmax()]
Pandas: avg_salary_by_dept =
employees.groupby('department_id')['salary'].mean()
10.List employees who have been with the company for more than 5 years
11.Find employees who have not received a salary increase in the last year
13.Find employees who have been in the company the longest in each
department
SQL: SELECT department_id, employee_id, hire_date
FROM employees e
WHERE hire_date = (SELECT MIN(hire_date)
FROM employees
WHERE department_id = e.department_id)
Pandas: longest_in_company_per_dept =
employees.loc[employees.groupby('department_id')['hire_date'].idxmin()]