关于group by 的一个SQL题目
2010-08-13 14:45
197 查看
获得部门最高工资比别的部门平均工资还低的部门及平均公司信息。
表如下: employee table
+----+--------+--------+------+
| id | name | salary | dept |
+----+--------+--------+------+
| 1 | test1 | 99 | dep1 |
| 2 | test2 | 89 | dep1 |
| 3 | test3 | 73 | dep1 |
| 4 | test4 | 87 | dep1 |
| 5 | test5 | 85 | dep1 |
| 6 | test6 | 84 | dep2 |
| 7 | test7 | 82 | dep2 |
| 8 | test8 | 80 | dep2 |
| 9 | test9 | 86 | dep2 |
| 10 | test10 | 79 | dep2 |
| 11 | test11 | 83 | dep3 |
| 12 | test12 | 80 | dep3 |
+----+--------+--------+------+
12 rows in set (0.00 sec)
有两个sql解决方案:
1,select a.*,b.* from
(select dept,max(salary) maxsalary from test group by dept) a,
(select dept,avg(salary) avgsalary from test group by dept) b
where a.maxsalary<b.avgsalary
2,select dept, max(salary) from employee group by dept having max(salary) in (select max(salary) from employee group by dept having max(salary) < some (select avg(salary) from employee group by dept))
第二题, 写一个SQL语句用来检查某个字段中是否存在重复的值,并输出重复的值和次数。
select dept, count(dept) from test.employee group by dept
第三题 横表变竖表
第四题,oracle如何更新一个表的sequence
alter seq increment by 1000;
select seq.nextval from seq;
alter seq increment by 1;
第五题, 如果更新一个表中的时间字段
update employee set start_date = SYSDATE - 5 where id = 1234;
or update employee set start_date = to_date('2003-06-13 15:18','YYYY-MM-DD hh:mm');
表如下: employee table
+----+--------+--------+------+
| id | name | salary | dept |
+----+--------+--------+------+
| 1 | test1 | 99 | dep1 |
| 2 | test2 | 89 | dep1 |
| 3 | test3 | 73 | dep1 |
| 4 | test4 | 87 | dep1 |
| 5 | test5 | 85 | dep1 |
| 6 | test6 | 84 | dep2 |
| 7 | test7 | 82 | dep2 |
| 8 | test8 | 80 | dep2 |
| 9 | test9 | 86 | dep2 |
| 10 | test10 | 79 | dep2 |
| 11 | test11 | 83 | dep3 |
| 12 | test12 | 80 | dep3 |
+----+--------+--------+------+
12 rows in set (0.00 sec)
有两个sql解决方案:
1,select a.*,b.* from
(select dept,max(salary) maxsalary from test group by dept) a,
(select dept,avg(salary) avgsalary from test group by dept) b
where a.maxsalary<b.avgsalary
2,select dept, max(salary) from employee group by dept having max(salary) in (select max(salary) from employee group by dept having max(salary) < some (select avg(salary) from employee group by dept))
第二题, 写一个SQL语句用来检查某个字段中是否存在重复的值,并输出重复的值和次数。
select dept, count(dept) from test.employee group by dept
第三题 横表变竖表
第四题,oracle如何更新一个表的sequence
alter seq increment by 1000;
select seq.nextval from seq;
alter seq increment by 1;
第五题, 如果更新一个表中的时间字段
update employee set start_date = SYSDATE - 5 where id = 1234;
or update employee set start_date = to_date('2003-06-13 15:18','YYYY-MM-DD hh:mm');
相关文章推荐
- 一个关于SQL语句的练习题目
- 一个关于SQL的笔试题目及解答
- 关于求一个表中相同板块的总点击数(sql题目)
- 一个关于group by的sql
- 朋友问的一个关于group by的sql
- 一个关于SQL的笔试题目及解答
- DB2数据库中关于 要求一个字段的值不能为null的sql
- 一个关于位运算的小题目,基础差真可怕
- 一个题目涉及到的50个Sql语句
- 一个关于in关键字的奇怪的SQL语句
- 一个题目涉及到的50个SQL语句
- 一个关于SQL Server memory计数器的系列讲座(很详细)
- 开发中遇到的一个关于 SQL 语句执行的问题
- 一个排序的SQL题目
- 关于360笔试题的一个题目的以下代码执行后输出结果为()思考
- 一个关于支付的SQL-Procedure
- 一个sql提问,引起的研究。关于union all 中,嵌套查询和直接查询的效率比较。
- 关于SQL优化的一个小试例子
- 关于一个sql,六一儿童节快乐~
- 关于enum ,调用webservice,用户控件与主页面之间的交互,datsource属性,net面试题,反射类生成sql语句,URl重写一个小实例