我最终遇到了一个奇怪的问题。我无法找出问题的根本原因。请帮忙。
请执行以下脚本
创建一个表格CREATE TABLE Employee_salary (ID INT,emp_name VARCHAR(50),Salary INT)
INSERT INTO Employee_salary VALUES(1,'Dolu',15000)
INSERT INTO Employee_salary VALUES(2,'Bolu',15000)
INSERT INTO Employee_salary VALUES(3,'Kalia',10000)
INSERT INTO Employee_salary VALUES(4,'Bheem',50000)
INSERT INTO Employee_salary VALUES(5,'Krishna',40000)
INSERT INTO Employee_salary VALUES(6,'Chutki',30000)
SELECT * FROM Employee_salary
ID emp_name Salary
1 Dolu 15000
2 Bolu 15000
3 Kalia 10000
4 Bheem 50000
5 Krishna 40000
6 Chutki 30000
我想要最高薪水的员工,所以我找到了bwlow查询
SELECT Top 1 emp_name,MAX(salary) from Employee_salary Group by emp_name
我试图找到最低薪水员工并尝试通过将'MAX'更改为'MIN'来执行以下查询。令人惊讶的是,以下查询无论如何都不起作用。它仍显示'Bheem,50000',这是一个错误的答案。任何人都可以告诉我这个问题的原因或原因。
SELECT Top 1 emp_name,MIN(salary) from Employee_salary Group by emp_name
答案 0 :(得分:4)
请从最低薪水中尝试以下查询:
SELECT Top 1 emp_name, salary from Employee_salary order by salary
和
SELECT Top 1 emp_name, salary from Employee_salary order by salary desc
获得最高薪水。
如果有多个员工的薪水相同,请尝试:
SELECT Top 1 WITH TIES emp_name, salary from Employee_salary order by salary desc
答案 1 :(得分:1)
尝试下面查询最低和最高工资记录。
-- For Max
SELECT emp_name, salary from Employee_salary where salary = (SELECT Max(salary) from Employee_salary)
-- For Min
SELECT emp_name, salary from Employee_salary where salary = (SELECT Min(salary) from Employee_salary)
答案 2 :(得分:0)
可能有多个行具有相同的最高工资和最低工资。 所以下面的查询就可以实现这个
select s1.emp_name, s1.Salary from Employee_salary as s1 where not exists(select * from Employee_salary as s2 where s2.Salary > s1.Salary);
select s1.emp_name, s1.Salary from Employee_salary as s1 where not exists(select * from Employee_salary as s2 where s2.Salary < s1.Salary);
答案 3 :(得分:-1)
薪水最高的员工
SELECT MAX(salary) from Employee_salary
这将显示员工的最高薪水
如果您想查看员工详细信息,请
SELECT MAX(salary), Employee_salary.* from Employee_salary
如果您需要最低工资,则需要添加MIN()
MAX()