0% found this document useful (0 votes)
16 views

SQL & Python Interview Q&A

Uploaded by

David Mramba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SQL & Python Interview Q&A

Uploaded by

David Mramba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL & Python Interview Q&A

1. Find the employee(s) with the second highest salary in the company

SQL: SELECT employee_id, salary


FROM employees
WHERE salary = (SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1)

Pandas: second_highest_salary = employees['salary'].nlargest(2).iloc[-1]


second_highest_salary_employees = employees[employees['salary'] ==
second_highest_salary]

2. Find employees who earn more than the average salary of their department

SQL: SELECT employee_id, department_id, salary


FROM employees e
WHERE salary > (SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id)

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

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_tenure =
employees.loc[employees.groupby('department_id')['hire_date'].idxmin()]

4. Determine the top N percent of employees based on salary

SQL: WITH ranked_employees AS (


SELECT employee_id, salary,
PERCENT_RANK() OVER (ORDER BY salary DESC) AS percentile
FROM employees
)
SELECT employee_id, salary
FROM ranked_employees
WHERE percentile <= 0.1 -- Adjust this value for top N percent

Pandas: top_n_percent = 0.1


salary_threshold = employees['salary'].quantile(1 - top_n_percent)
top_employees = employees[employees['salary'] >= salary_threshold]

5. Find employees who joined after a specific date

SQL: SELECT employee_id, hire_date


FROM employees
WHERE hire_date > '2020-01-01'

Pandas: recent_hires = employees[employees['hire_date'] > '2020-01-01']

6. Find the employee with the highest salary in each department


SQL: SELECT department_id, employee_id, salary
FROM employees e
WHERE salary = (SELECT MAX(salary)
FROM employees
WHERE department_id = e.department_id)

Pandas: highest_salary_per_dept =
employees.loc[employees.groupby('department_id')['salary'].idxmax()]

7. Find the average salary of employees by department

SQL: SELECT department_id, AVG(salary) AS avg_salary


FROM employees
GROUP BY department_id

Pandas: avg_salary_by_dept =
employees.groupby('department_id')['salary'].mean()

8. Find the total number of employees in each department

SQL: SELECT department_id, COUNT(*) AS total_employees


FROM employees
GROUP BY department_id

Pandas: total_employees_per_dept = employees.groupby('department_id').size()

9. Find employees whose salary is greater than the average salary

SQL: SELECT employee_id, salary


FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
Pandas: avg_salary = employees['salary'].mean()
higher_than_avg = employees[employees['salary'] > avg_salary]

10.List employees who have been with the company for more than 5 years

SQL: SELECT employee_id, hire_date


FROM employees
WHERE hire_date <= CURRENT_DATE - INTERVAL '5 years'

Pandas: employees_5_years_plus = employees[employees['hire_date'] <=


pd.to_datetime('today') - pd.DateOffset(years=5)]

11.Find employees who have not received a salary increase in the last year

SQL: SELECT employee_id, last_salary_increase_date


FROM employees
WHERE last_salary_increase_date <= CURRENT_DATE - INTERVAL '1 year'

Pandas: one_year_ago = pd.to_datetime('today') - pd.DateOffset(years=1)


no_salary_increase = employees[employees['last_salary_increase_date'] <=
one_year_ago]

12.Count the number of employees who report to each manager

SQL: SELECT manager_id, COUNT(*) AS num_employees


FROM employees
GROUP BY manager_id

Pandas: employees_per_manager = employees.groupby('manager_id').size()

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()]

14.Find employees whose department has the highest average salary

SQL: SELECT employee_id, department_id, salary


FROM employees e
WHERE department_id = (SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1)

Pandas: dept_avg_salary = employees.groupby('department_id')['salary'].mean()


highest_avg_salary_dept = dept_avg_salary.idxmax()
high_salary_employees = employees[employees['department_id'] ==
highest_avg_salary_dept]

15.Get the number of employees who joined each month

SQL: SELECT EXTRACT(MONTH FROM hire_date) AS hire_month,


COUNT(*) AS num_employees
FROM employees
GROUP BY hire_month

Pandas: employees['hire_month'] = employees['hire_date'].dt.month


employees_per_month = employees.groupby('hire_month').size()
16.Find employees with the highest number of projects assigned

SQL: SELECT employee_id, COUNT(*) AS project_count


FROM employee_projects
GROUP BY employee_id
ORDER BY project_count DESC
LIMIT 1

Pandas: project_count = employee_projects.groupby('employee_id').size()


highest_project_count = project_count.idxmax()
employees_with_highest_projects =
employee_projects[employee_projects['employee_id'] == highest_project_count]

17.Find employees who belong to a specific department and have a salary


above a threshold

SQL: SELECT employee_id, department_id, salary


FROM employees
WHERE department_id = 3 AND salary > 60000

Pandas: specific_dept_salary = employees[(employees['department_id'] == 3) &


(employees['salary'] > 60000)]

18.Find employees who do not belong to any department

SQL: SELECT employee_id


FROM employees
WHERE department_id IS NULL

Pandas: no_dept_employees = employees[employees['department_id'].isna()]


19.Find employees who are younger than 30 years old

SQL: SELECT employee_id, birth_date


FROM employees
WHERE birth_date > CURRENT_DATE - INTERVAL '30 years'

Pandas: young_employees = employees[employees['birth_date'] >


pd.to_datetime('today') - pd.DateOffset(years=30)]

20.Find employees with a salary within a specific range

SQL: SELECT employee_id, salary


FROM employees
WHERE salary BETWEEN 50000 AND 70000

Pandas: salary_range = employees[(employees['salary'] >= 50000) &


(employees['salary'] <= 70000)]

You might also like