【396天】我愛刷題系列155(2018.03.08)

@(躍遷之路)專欄code

叨叨兩句

  1. ~

SQL習題050

題目描述
查找排除當前最大、最小salary以後的員工的平均工資avg_salary。
CREATE TABLE salaries ( emp_no int(11) NOT NULL,
salary int(11) NOT NULL,
from_date date NOT NULL,
to_date date NOT NULL,
PRIMARY KEY (emp_no,from_date));date

輸出格式:
avg_salary
69462.5555555556專欄

本題邏輯有問題,在挑選當前最大、最小salary時沒加 to_date = '9999-01-01' 做條件限制,致使挑選出來的是全表最大、最小salary,而後對除去這兩個salary再做條件限制 to_date = '9999-01-01' ,求平均薪水,此時求出的平均薪水與題目邏輯要求的不一樣。

SELECT AVG(salary) AS avg_salary FROM salaries 
WHERE to_date = '9999-01-01' 
AND salary NOT IN (SELECT MAX(salary) FROM salaries)
AND salary NOT IN (SELECT MIN(salary) FROM salaries)
正確的邏輯應以下所示,但在本題OJ系統中通不過:

SELECT AVG(salary) AS avg_salary FROM salaries 
WHERE to_date = '9999-01-01' 
AND salary NOT IN (SELECT MAX(salary) FROM salaries WHERE to_date = '9999-01-01')
AND salary NOT IN (SELECT MIN(salary) FROM salaries WHERE to_date = '9999-01-01')
相關文章
相關標籤/搜索