标签:共享资源 block err outer 找不到 lint net 设置 推荐
笔记是在听郝斌老师的Sql Service教程所做
把工资在1500到3000之间(包括1500和3000)的员工中工资最高的前四个的信息输出
select top 4 *
from emp
where sal between 1500 and 3000
order by sal desc;
null不能参与任何数据运算的解决方法:
select enam,sal*12+isnull(comm,0) "年薪"
from emp;
通配符:
% 表示任意0个或者多个字符
select *
from emp
where ename like ‘%A%‘;
select *
from emp
where ename like ‘A%‘;
_ 表示任意单位字符
select *
from emp
where ename like ‘_A%‘;
[a-f] 表示a到f的任意单个字符,只能是a、b、c、d、e、f中的任意一个字符
select *
from emp
where ename like ‘_[A-F]%‘
例子: 输出部门平均工资大于2000的部门的部门编号,和平均工资
select deptno ,avg(sal)
from emp
group by deptno
having avg(sal) > 2000;
注意:having只能出现分组后的整体信息,不能出现组内的详细信息
统计输出部门平均工资大于2000的部门的部门的编号,部门的平均工资
select deptno,avg(sal) "平均工资"
from emp
where ename not like ‘%A%‘
group by deptno
having avg(sal) > 2000;
select deptno,avg(sal) "平均工资"
from emp g
roup by deptno
having avg(sal) > 2000
where ename not like ‘%A%‘;
select … from A,B的用法【笛卡尔集】
select *
from emp,dept
select … from A,B where … 的用法
select *
from emp, dept
where empno = 7369
select "E".ename "员工名称","D".dname "部门名称"
join dept "D"
on "E".deptno = "D".deptno;
select … from A,B where 与 select … from A join B on … 的比较
前者是sql92标准,后者是sql99标准,输出结果是一样的,推荐使用SQL99标准
select "E".ename, "D".dname, "S".grade
where "E".sal > 2000
join "D"
on "E".deptno = "D".deptno
join salgrade "S"
on "E".sal >= "S".local and "E".sal <= "S".hisal;
select、from、where、join、on、group、order、top
select *
from emp
join dept
on emp.deptno = dept.deptno
having dept.deptno = 10;
1.求出每个员工的姓名,部门编号,薪水和薪水的等级
select "E".ename,"E".sal,"S".grade
from emp "E"
join salgrade "S"
on "E".sal >= "S".losal and "E".sal <= "S".hisal;
2.查找每个部门的编号,该部门所有员工的平均工资,平均工资的等级
select "T".deptno,"T"."avg_sal" "部门平均工资","S".grade "工资等级"
from (
select deptno,avg(sal) "avg_sal"
from emp
group by deptno
) "T"
join salgrade "S"
on "T".avg_sal between "S".losal and "S".hisal;
或者:
select "T".deptno,"T"."avg_sal" "部门平均工资","S".grade "工资等级"
from salgrade "S"
join (
select deptno,avg(sal) "avg_sal"
from emp
group by deptno
) "T"
on "T".avg_sal between "S".losal and "S".hisal;
3.查找每个部门的编号,部门名称,该部门所有员工的平均工资,平均工资的等级
select "T".deptno,"T"."avg_sal" "部门平均工资","S".grade "工资等级","D".dname "部门名称"
from (
select deptno,avg(sal) "avg_sal"
from emp
group by deptno
) "T"
join salgrade "S"
on "T".avg_sal between "S".losal and "S".hisal
join dept "D"
on "T".deptno = "D".deptno;
4.考虑下面语句的实际含义 以及这样写是否恰当?
select * from emp
join dept
on emp.deptno = dept.deptno and emp.sal>2000
on中既可以写连接条件,也可以写过滤条件,但是不推荐,应该分开写。on中只写连接条件,where中写过滤条件。
等价于
select * from emp
join dept
on emp.deptno = dept.deptno
where emp.sal>2000
等价于
select * from dept
join emp
on dept.deptno = emp.deptno
where emp.sal>2000
等价于
select *
from dept,emp
where dept.deptno = emp.deptno and emp.sal>2000
下面例子:error 有join就必须的有on
select *
from emp
join dept
5.求出emp表中所有领导的信息
select * from emp
where empno in (select mgr from emp)
思考题
求出emp表中所有非领导的信息
select * from emp
where empno not in (select mgr from emp)
in与null的组合所带来的问题
6.求出平均薪水最高的部门的编号和部门的平均工资
第一种写法
select top 1 deptno "部门的编号",avg(sal)"平均工资"
from emp
group by deptno
order by avg(sal) desc
第二种写法
select "E".*
from (
select deptno, avg(sal) "avg_sal"
from emp
group by deptno
) "E"
where "E"."avg_sal" = (
select max("avg_sal")
from (
select deptno, avg(sal) "avg_sal"
from emp
group by deptno
) "T"
)
7.有一个人工资最低,把这个人排除掉;剩下的人中工资最低的前3个人的姓名、工资、部门编号、部门名称、工资等级输出
select top 3 "T".ename, "T".sal, "T".deptno, "D".dname, "S".grade
from (
select *
from emp "E" --153行
where sal > (
select min(sal)
from emp
)
) "T" --这里的"E"与153行的"E"是没有冲突的,因为范围不一样
join dept "D"
on "T".deptno = "D".deptno
join salgrade "S"
on "T".sal between "S".losal and "S".hisal
order by "T".sal asc
8.查询的顺序
select top ...
from A
join B
on ...
join C
on ...
where ...
group by ...
having ...
order by ...
select * from emp
cross join dept
等价于
select *
from emp,dept
select *
from emp
where empno not in (
select dintinct "E1".empno
from emp "E1"
join emp "E2"
on "E1".sal < "E2".sal
)
注意:
输出所有员工信息,及其老板的名字,如果员工没有上司。则输出已经是最大老板
select * ,‘已经是最大老板了‘
from employee
where eId = 0
union -- 联合改变的是行
select E1.*,E2.ename ‘老板名字‘
from employee E1,employee E2
where E1.boss = E.eid
注意:
表示该字段的值会自动更新,不需要我们维护,通常情况下我们不可以直接给identity修饰的字符赋值,否则编译时会报错。
create view 视图的名字
as
– select的前面不能添加begin
select语句
– select的后面不能添加end
事务主要用来保证数据的合理性和并发处理的能力
一旦事物提交或者回滚,则事务结束
事物必须具备以下四个属性,简称为ACID属性
select top 3 *
from emp
order by sal desc
select top 3 *
from emp
where empno not in (
select top 3 empno
from emp
order by sal desc
)
order by sal desc
select top 3 *
from emp
where empno not in (
select top 6 empno
from emp
order by sal desc
)
order by sal desc
假设每页显示n条记录,当前要显示的是第m页,表名是A,主键是AId
select top n *
from A
where AId not in (
select top (m-1)*n AId
from emp
)
说实在,郝斌老师算是我程序猿道路上的启蒙恩师,老师讲的课都很好,在这里,附上老师的视频地址:
标签:共享资源 block err outer 找不到 lint net 设置 推荐
原文地址:http://blog.csdn.net/notzuonotdied/article/details/52831729