查找同类型的最大工资

来源:互联网 发布:网络技术员面试题 编辑:程序博客网 时间:2024/04/27 15:20

今天逛csdn看到一个查同类型的最大工资的问题。

表结构及数据
ID  姓名  工资  时间
1   a     134   2007
2   b     23    2007
3   c     76    2007
4   a     1351  2007
5   b     424   2007

要求查出a,b,c分别的最大工资

试验了一下,感觉还有点意思:

--创建表结构

create table tb1
(
id int,
姓名 varchar(10),
工资 varchar(10),
时间 varchar(10)
)

--插入数据

insert into tb1
select 1,'a',134,2007
union all select 2,'b',23,2007
union all select 3,'c',76,2007
union all select 4,'a',120, 2007
union all select 5,'b',424,2007
union all select 6,'a',1340,2007
union all select 7,'b',510  ,2007

--得到查询结果

select * from tb1 a
where a.工资=(select max(工资) from tb1 where 姓名=a.姓名)
order by a.姓名 

select * from tb1
where 工资 in (SELECT MAX(工资) AS '工资' FROM tb1 GROUP BY 姓名)
order by 姓名

下面一个可能好理解一点*-*