【tratascratch】SQL+Python刷题笔记——10308:Salaries Differences【Easy】

ID 10308——Salaries Differences【题目链接】

难度:Easy【简单】
公司:Dropbox【多宝箱】
考察方式:Interview Questions【面试题目】

问题描述

Salaries Differences【薪资差异】

Write a query that calculates the difference between the highest salaries found in the marketing and engineering departments. Output just the difference in salaries.

编写一个查询,计算marketing部门和engineering 部门的最高工资之间的差异。
输出:工资的差异。

原始表

table1:(db_employee)
【tratascratch】SQL+Python刷题笔记——10308:Salaries Differences【Easy】_第1张图片

table2:(db_dept)
【tratascratch】SQL+Python刷题笔记——10308:Salaries Differences【Easy】_第2张图片

解决方案

SQL方法

SELECT
    (SELECT MAX(SALARY)
    FROM db_employee de
    LEFT JOIN db_dept dd
    ON de.department_id = dd.id
    WHERE department = 'marketing') // 找出marketing部门的最高薪
    -
    (SELECT MAX(SALARY)
    FROM db_employee de
    LEFT JOIN db_dept dd
    ON de.department_id = dd.id
    WHERE department = 'engineering') // 找出engineering部门的最高薪
AS SALARIES_DIFFERENCES  //相减结果存入SALARIES_DIFFERENCES 表

Python方法

# Import your libraries
import pandas as pd
import numpy  as np

# Start writing code
df = pd.merge(db_employee, db_dept, how = 'left',left_on = ['department_id'], right_on=['id'])
df1=df[df["department"]=='engineering']
df_eng = df1.groupby('department')['salary'].max().reset_index(name='eng_salary')
df2=df[df["department"]=='marketing']
df_mkt = df2.groupby('department')['salary'].max().reset_index(name='mkt_salary')
result = pd.DataFrame(df_mkt['mkt_salary'] - df_eng['eng_salary'])
result.columns = ['salary_difference']
result

输出结果

【tratascratch】SQL+Python刷题笔记——10308:Salaries Differences【Easy】_第3张图片

你可能感兴趣的:(面试,python,sql)