SQL练习(带答案).doc

上传人:大米 文档编号:563559034 上传时间:2023-10-24 格式:DOC 页数:4 大小:91.50KB
返回 下载 相关 举报
SQL练习(带答案).doc_第1页
第1页 / 共4页
SQL练习(带答案).doc_第2页
第2页 / 共4页
SQL练习(带答案).doc_第3页
第3页 / 共4页
SQL练习(带答案).doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《SQL练习(带答案).doc》由会员分享,可在线阅读,更多相关《SQL练习(带答案).doc(4页珍藏版)》请在金锄头文库上搜索。

1、SQL练习题表1 Employee表编号姓名地址邮编电话电子邮件部门出生日期性别NumNameAddrZipTelEmailDepnoBirthSex001王林武汉大学43007487598405Null21985-2-1男002王芳华中科大43007362534231Null11966-3-28男003张晓武汉理工大43007287596985Null11972-12-9男004王小燕武汉交大11950-7-30女005李华华中农大43007087569865Null51962-10-18男006李明华中师大51955-09-28男007田丽中南财大31968-08-10女008吴天武汉电力

2、51964-10-01男009刘备武汉邮科院43007869865231Null31967-04-02男010张飞武汉软通41958-09-20男011赵云学府家园43007168592312Null41968-11-18男012貂禅湖北工大43007465987654null41959-09-03女表2 Department编号部门名称备注DepnoDepNameRemark1财务部Null2人力资源部Null3经理办公室Null4研发部Null5市场部Null表3 salary编号收入支出NumInComeOutCome0012100.8123.090021582.6288.0300325

3、69.88185.650041987.0179.580052066.15108.00062980.7210.20073259.98281.520082860.01980092347.681800102531.98199.080112240.0121.00121980.0100.0练习1:SELECT语句的基本使用(1) 查询每个雇员的所有记录; select *from employee(2) 查询前5个会员的所有记录; select *from employee where num between 001 and 005Select top 5 * from employee(3) 查询每个

4、雇员的地址和电话;select num,addr,tel from employee(4) 查询num为001的雇员地址和电话; select num,addr,tel from employee where num=001(5) 查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话; select num 雇员号,addr 地址,tel 电话 from employee where sex=女 说明:oracle重新定义别名不是用as,而是空格(6) 计算每个雇员的实际收入; select num,income-outcome come from

5、 salary(7) 找出所有姓王的雇员的部门号(部门号不能重复显示); select distinct depno from employee where name like 王%(8) 找出所有收入在20003000元之间的雇员编号 select num from salary where income between 2000 and 3000练习2:子查询的使用(答案可以不唯一)(1) 查找在财务部工作的雇员情况;select * from employee where depno=(select depno from department where depname=财务部)也可用

6、inselect * from employee where depno in (select depno from department where depname=财务部)也可用 exists (所有的IN均可用Exists表示)select * from employee where exists (select * from department where employee.depno=department.depno and depname=财务部)exists 的特点:(2) 查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名; select name from emplo

7、yee where depno=(select depno from department where depname=财务部) and birth=(select min(birth) from employee where depno=(select depno from department where depname=研发部) (注意:年龄越大,生日越小)也可用select name from employee where depno=(select depno from department where depname=财务部) and birth(select max(income

8、) from salary where num in(select num from employee where depno=(select depno from department where depname=财务部) ) 练习3:连接查询的使用(1) 查找每个雇员的情况及薪水情况;select employee.*,income,outcome from employee,salary where employee.num=salary.num(2) 查找财务部收入在2200元以上的雇员姓名及其薪水详细情况; Select name,income,outcome from employ

9、ee,department,salary where employee.num=salary.num and employee.depno=department.depno and depname=财务部 and income2200练习4:数据汇总(1) 求财务部雇员的平均实际收入; select avg(income-outcome) avg_come from salary where num in(select num from employee where depno=(select depno from department where depname=财务部 )也可改成连结查询s

10、elect avg(income-outcome) avg_come from salary,employee,department where salary.num=employee.num and employee.departnum=department.departnum and department.depname=财务部 (2) 求财务部雇员的总人数; select count(*) 财务部人数 from employee where depno=(select depno from department where depname=财务部 )思考问题:如何改成连接查询练习5:GR

11、OUP BY 、ORDER BY 子句的使用(1) 求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数); select department.depno,depname,count(employee.depno) from employee,department where department.depno *= employee.depno group by department.depno,depname注意:统计列的要求思考问题,上题如改成按雇员数量降序排列,如何实现?增加Order by count(employee.depno) DESC (2) 求部门的平均薪水大于2500的部门信息(要求显示,部门号、部门名称和平均工资) select department.depno,depname,avg(income) from employee,department,salary where employee.depno=department.depno and employee.num=salary.num group by department.depno,depname having avg(income)2500

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号