SQL实验实验4至实验7的答案 .doc
《SQL实验实验4至实验7的答案 .doc》由会员分享,可在线阅读,更多相关《SQL实验实验4至实验7的答案 .doc(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验实验41.用select 语句查询departments和salary表中的所有数据:select salary.*, departments.* from salary ,departments 2、查询departments 中的departmentid:select departmentid from departments go 3、查询 salary中的 income,outcome:select income,outcome from salarygo4、查询employees表中的部门号,性别,要用distinct消除重复行:select distinct(departmen
2、tid), sexfrom employees 5、查询月收入高于2000元的员工号码:select employeeid from salarywhere income2000go6、查询1970年以后出生的员工的姓名和住址:select name ,address from employees where birthday1970go7、查询所有财务部的员工的号码和姓名:select employeeid ,namefrom employeeswhere departmentid in(select departmentid from departments where departmen
3、tname=财务部)go8、查询employees员工的姓名,住址和收入水平,2000元以下显示为低收入,20003000元显示为中等收入,3000元以上显示为高收入:select name ,address,case when income-outcome3000 then 高收入else 中等收入end as 收入等级from employees,salarywhere yeeid=yeeidgo9、计算salary表中员工月收入的评价数:select avg(income)as 平均收入 from salary10、查找employees表中最大的员工号码:select max(empl
4、oyeeid)as 最大员工号码 from employees11、计算salary表中的所有员工的总支出:select sum(outcome) as总支出 from salary12、查询财务部雇员的最高实际收入:select max(income-outcome) from salary ,employees,departmentswhere yeeid=yeeid and tmentid=tmentid and departmentname=财务部go13、查询财务部雇员的最低实际收入:select min(income-outcome) from salary ,employees,
5、departmentswhere yeeid=yeeid and tmentid=tmentid and departmentname=财务部go14、找出所用地址中含有“中山”的雇员的号码及部门号:select employeeid ,departmentid from employeeswhere address like%中山%go15、查找员工号码中倒数第二个数字为0的员工的姓名,地址和学历:select education,address,name from employees where employeeid like%0_go16、使用into字句,由表employees创建“男
6、员工1”表,包括编号和姓名:select employeeid,name into 男员工表from employees where sex=1go17、用子查询的方法查找收入在2500元以下的雇员的情况:select * from employees where employeeid in(select employeeid from salary where incomeALL(SELECT InCome FROM Salary WHERE EmployeeID IN ( SELECT EmployeeId FROM Employees WHERE DepartmentID IN (SEL
7、ECT DepartmentID FROM Departments WHERE DepartmentName=财务部 )19、 用子查询的方法查找所有年龄比研发部雇员都大的雇员的姓名:select name from employees where Birthday2500)26、按部门列出在该部门工作的员工的人数:select departmentid ,count(*) as 人数from employees group by departmentid27、按员工的学历分组:select education ,count(*) as 人数from employees group by ed
8、ucation28、按员工的工作年份分组,统计年份人数:select workyear ,count(*) as 人数from employees group by workyear29、按各雇员的情况收入由低到高排列:select employees.* ,efrom employees ,salary where yeeid=yeeidorder by income30、将员工信息按出生时间从小到大排列:select *from employees order by birthday31、在order by 字句中使用子查询,查询员工姓名,性别和工龄信息,要求按实际收入从大到小排列:sel
9、ect name ,sex,workyear,income-outcomefrom salary ,employeeswhere yeeid=yeeidorder by income-outcome desc视图部分1、创建view1:Create view view1 as select yeeid,name,departmentname,(income-outcome) as comefrom employees , departments , salary where tmentid=tmentid and yeeid=yeeid2、查询视图employeeid:3、向视图view1中插
10、入一行数据:insert into view1 values(111111,谎言,1,30000)4、查看视图(没有影响)基本表:实验51、 定义一个变量,用于描述YGGL数据库的salary表中000001号员工的实际收入,然后查询该变量:declare hy int set hy=(select income-outcome from salary where employeeid=000001)select hy2、 使用运算符“”:select name from employees where birthday1974-10-103、 判断姓名为“王林”的员工实际收入是否高于3000
11、元,如果是则显示“高收入”,否则显示“收入不高于3000”:if(select income from salary,employees where yeeid=yeeid and =刘明)3000) select income as 高收入 from salary,employees where yeeid=yeeid and =刘明else select收入不高于4、使用循环输出一个“*”三角形:declare i int declare j int set j=20set i=1while i1beginset j=j*iset i=i-1endreturn(j)end declare
12、h int exec h= 4select h as jiecheng7、/*生成随机数*/select rand()8、/*平方*/select square(12)9、/*求财务部收入最高的员工姓名*/select max(name) from employees where employeeid in(select employeeid from salary where employeeid in (select employeeid from employees where departmentid in (select departmentid from departments w
13、here departmentname=财务部)select avg(income) as 平均收入from salary/*聚合函数与group by 一起使用*/select workyear ,count(*) as 人数from employees group by workyear/*将字符组成字符串*/select char(123)/*返回字符串左边开始的个字符*/select left(abcdef,2)/*返回指定日期时间的天数*/select day(birthday)from employees where employeeid=010000/*获取当前时间*/selec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SQL实验实验4至实验7的答案 SQL 实验 答案
限制150内