文档介绍:数据查询深入
一、目的
掌握用SQL对数据库进行简单查询和多表数据查询;
进一步理解连接查询和嵌套查询的实际意义;
进一步熟悉SQL Server2005新增的语法及其使用方法。
二、指南
请附加数据库yggl。
三、内容
第一部分:
1、对员工表选择姓名、部门编号,只返回结果集的前5行。
select top 5 name,departmentid
from employees
2、将员工信息按部门编号排序,并产生一个汇总行,汇总各部门人数。
select * from employees
order by departmentid
compute count(employeeid) by departmentid
3、利用临时表,在员工表中查找姓李的员工的姓名,分别使用别名‘new_name’。
4、使用intersect查询性别为男但是不姓“李”的员工的信息。
select * from employees where sex=1
intersect
select * from employees where name not like '李%'
5、使用union查询姓张和姓王的员工的信息。
select * from employees where name like '张%'
union all
select * from employees where name like '王%'
6、使用except查询性别为男而且姓李的员工的信息。
select * from employees where sex=1
except
select * from employees where name not like '李%'
7、先建立全文索引,再使用CONTAINS谓词搜索员工表中电子邮件列中包含字符“@”的所有行。
8、将工资表的信息按照如下规则转换后输入。收入超过2500改为“高收入”,收入介于2000和2500之间改为“中等收入”,收入低于2000改为“低收入”。
select *,
case
when e>=2500 then '高收入'
when e between 2000 and 2500 then '中等收入'
else
'低收入'
end
as '转换结果'
from salary
第二部分:
1、查询所有地址中含有“中山”的男员工的电话。
select phonenumber
from employees
where sex=1 and address like '%中山%'
2、查询每个雇员的实际收入(收入—支出)。
select employeeid,e-e as 实际收入
from salary
3、查询所有在财务部或研发部的所有员工的信息。
select *
from employees
where departmentid in (select departmentid from departments where departmentname='财务部' or departmentname='研发部')
4、查询收入在2000—3000之间的员工的信息。
select *
from em