我认为测试应该掌握的SQL语句
最近在学习Oracle,对测试人员而言必须掌握两种语言:第一种是DML,数据操纵语言 (Data Manipulation Language) 是SQL语言中,负责对数据库对象运行数据访问工作的指令集,以INSERT、UPDATE、DELETE三种指令为核心,分别代表插入、更新与删除。第二种是:DQL,数据查询语言 (Data Query Language) 是SQL语言中,负责进行数据查询而不会对数据本身进行修改的语句,这是最基本的SQL语句。核心指令为SELECT,以及一些辅助指令,如FROM、WHERE等,FROM:表示来源,可以搭配JOIN做链接查询; WHERE:过滤条件;GROUP BY:在使用聚合函数时用到,如SUM,COUNT,MAX,AVG;HAVING:对聚合结果进行筛,这是和WHERE的不同点;ORDER BY:排序。
一下是必须掌握的SQL习题:
1、列出至少有一个员工的所有部门
select d.*,ed.cou
from dept d,(select deptno,count(empno) cou from emp
group by deptno having count(empno)>1) ed
where d.deptno=ed.deptno;
2、列出薪金比“SMITH”多的所有员工。
·求出SMITH的薪金
select sal from emp where ename='SMITH';
·求所有
select * from emp
where sal>(select sal from emp where ename='SMITH');
3、列出所有员工的姓名及其直接上级的姓名
select e.ename,m.ename
from emp e,emp m
where e.mgr=m.empno(+);
4、列出受雇日期早于其直接上级的所有员工的编号,姓名,部门名称
select e.empno,e.ename,d.dname
from emp e,emp m,dept d
where e.mgr=m.empno and e.hiredate<m.hiredate and e.deptno=d.deptno;
5、列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门,
select d.deptno,d.dname,e.empno,e.ename
from dept d,emp e
where d.deptno=e.deptno(+);
6、列出所有“CLERK”人员的姓名及其部门名称,部门的人数
select e.ename,d.dname,ed.cou
from emp e,dept d,(select deptno,count(empno) cou from emp group by deptno) ed
where job='CLERK'and e.deptno=d.deptno and ed.deptno=e.deptno;
7、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数
·按工作分组,分组条件是最低薪金大于1500
select job,min(sal)
from emp
group by job having min(sal)>1500;
·求全部的雇员人数
select count(e.empno),e.job
from emp e
where e.job in(select job
from emp
group by job having min(sal)>1500)
group by e.job;
8、列出在部门“SALES”工作的员工姓名,假定不知道销售部的部门编号
·通过dept表查询出销售部的部门编号
select deptno from dept where dname='SALES';
·将之前的查询作为子查询
select ename
from emp where deptno=(select deptno from dept where dname='SALES');
9、列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级。
·公司的平均工资
select avg(sal) from emp;
·列出薪金高于平均薪金
select * from emp
where sal>(select avg(sal) from emp);
·与部门表关联查询出所在部门的信息
select e.*,d.loc
from emp e,dept d
where sal>(select avg(sal) from emp)and e.deptno=d.deptno;
·与自身关联查询上级领导
select e.ename,e.empno,m.empno,m.ename,d.deptno,d.dname,d.loc
from emp e,dept d,emp m
where e.sal>(select avg(sal) from emp)
and e.deptno=d.deptno
and e.mgr=m.empno(+);
·求出雇员的工资等级
select e.ename,e.empno,s.grade,m.empno,m.ename,d.deptno,d.dname,d.loc
from emp e,dept d,emp m,salgrade s
where e.sal>(select avg(sal) from emp)
and e.deptno=d.deptno
and e.mgr=m.empno(+)
and e.sal between s.losal and s.hisal;
10、列出与scott从事相同工作的所有员工及部门名称
·找到Scott的工作
select job from emp where ename='SCOTT';
·找出与其工作相同的雇员
select ename,empno,job,sal
from emp
where job=(select job from emp where ename='SCOTT');
·这是不应该出现SCOTT
在加个条件;and ename!='SCOTT';
select ename,empno,job,sal
from emp
where job=(select job from emp where ename='SCOTT')
and ename!='SCOTT';
·部门名称
select e.ename,e.empno,e.job,e.sal,d.dname,d.loc
from emp e,dept d
where job=(select job from emp where ename='SCOTT')
and ename!='SCOTT'
and e.deptno=d.deptno;
11、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金
·列出30部门员工薪金
select sal from emp where deptno=30;
·以上作为子查询
select ename,sal
from emp
where sal in(select sal from emp where deptno=30)
and deptno!=30;
12、列出薪金高于部门30中员工的薪金的所有员工的姓名和薪金、部门名称
·使用>ALL
select ename,sal
from emp
where sal >all(select sal from emp where deptno=30)
and deptno!=30;
·使用表关联
select e.ename,e.sal,d.dname,d.loc
from emp e,dept d
where e.sal >all(select sal from emp where deptno=30)
and e.deptno!=30 and d.deptno=e.deptno;
13、列出在每个部门工作的员工数量,平均工资和平均服务期限
select d.dname,count(e.empno),avg(sal),avg(months_between(sysdate,e.hiredate)/12)year
from emp e,dept d
where e.deptno=d.deptno
group by d.dname;
14、列出所有员工的姓名,部门名称和工资
select e.ename,d.dname,e.sal
from emp e,dept d
where e.deptno=d.deptno;
15、列出所有部门的详细信息和部门人数
select d.*,nvl(ed.count,0)
from dept d,(select deptno,count(empno) count from emp group by deptno) ed
where d.deptno=ed.deptno(+);
16、列出各种工作的最低工资及从事此工作的雇员姓名
·最低工资的工作
select min(sal) from emp group by job;
·按工资查询
select * from emp
where sal in(select min(sal) from emp group by job);
17、列出各部门经理的最低薪金
select deptno,min(sal)
from emp
where job='MANAGER'
group by deptno;
18、列出所有员工的年薪,按年薪降序排列
select ename,(sal+nvl(comm,0))*12 income
from emp
order by income;
19、查出某个员工的上级主管,并求出这些主管薪水超过3000的
select distinct m.ename,m.sal
from emp e,emp m
where m.sal>3000 and e.mgr=m.empno;
20、求出部门名称中带‘S’字符的部门员工的工资合计,部门人数
·求部门名称带‘S’的部门
select deptno,sum(sal),count(empno)
from emp
where deptno in(select deptno from dept where dname like '%S%')
group by deptno;
21、给任职超过10年的人加薪10%
update emp set sal=sal+sal*0.1
where months_between(sysdate,hiredate)/12>10;
我认为测试应该掌握的SQL语句的更多相关文章
- mybatis执行test07测试类却显示test05测试类调用的sql语句出错
1.测试类 @Test public void test07() { IStudentDao studentDao = new IStudentDaoImpl(); Student student = ...
- 测试常用的sql语句总结
测试中常用的sql语句,排名部分先后 1. 查询 SELECT * FROM 表名称 SELECT COUNT(DISTINCT column_name) FROM table_name 指定列的不同 ...
- Oracle批量插入数据SQL语句太长出错:无效的主机/绑定变量名
Oracle数据库,用mybatic批量插入数据: <insert id="saveBatch" parameterType="io.renren.entity.N ...
- c# 分析SQL语句中的表操作
最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...
- 测试mysql的sql语句预编译效果
玩Oracle的都比较关注shared pool,特别是library cache,在使用了绑定变量(预编译sql)之后确实能得到很大的性能提升.现在在转Mysql之后特别是innodb很多东西都还能 ...
- sqlserver 测试sql语句执行时间
查看sql语句执行时间/测试sql语句性能 写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置S ...
- 如何测试sql语句性能,提高执行效率
有时候我们经常为我们的sql语句执行效率低下发愁,反复优化后,可还是得不到提高 那么你就用这条语句找出你sql到底是在哪里慢了 示例: SET STATISTICS io ON SET ...
- 查看SQL语句执行时间与测试SQL语句性能
查看SQL语句执行时间与测试SQL语句性能 写程序的人,往往需要分析所写的SQL语句是否够优化.是否能提升执行效率,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了 ...
- Loadrunner测试数据库性能,测试SQL语句的脚本例子
Loadrunner与SQL Server的操作可以通过录制的方式来实现,但本文还是通过直接调用loadrunner本身的function来实现sql语句的操作, 主要用到的是lr_db_connec ...
随机推荐
- Asp.Net_Web身份验证
百度一下”asp.net身份认证“,你会得到很多相关的资料,这些资料通常上来就会介绍诸如”Form认证“”Windows认证“等内容,而没有给出一个完整的流程.初学者对此往往一头雾水,我也曾经被坑过很 ...
- 犯过错误的C语言问题
1 memcpy函数: 函数原型:void *memcpy(void *dest, void *src, unsigned int count); 函数源码: void *memcpy1(void * ...
- android 开发 常用工具类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...
- 在Android上使用Google V8 JS 引擎
在cantk-runtime中直接使用的webview,通过JAVA扩展接口把Canvas的2d Context的API定向到JNI,通过OpenGL来图形加速,渲染速度大大提高.后来测试发现在大部分 ...
- 本地化Model Factory
即让其生成中文的测试数据 先简单介绍Model Factory两个常用方法: 进入tinker页面生成测试数据: Factory(User::class,10)->make():make()方法 ...
- jquery函数
1.ready函数 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. $(document).ready(function (){alert('11 ...
- CentOS中输入yum报错:sudo: unable to execute /bin/yum: No such file or directory
今天尝试更新了下虚拟机CentOS中的python版本后. 运行“yum”命令,就报错:“sudo: unable to execute /bin/yum: No such file or direc ...
- Mybatis 学习-2
创建基于session的util类,在线程中共享SqlSession package cn.smartapp.blogs.pojo; import java.io.Serializable; impo ...
- hdu 3853LOOPS (概率DP)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...