简单sql部分强化练习题


简单查询部分sql练习题


-- 选择部门30中的所有职工
select * from emp where deptno = 30;

-- 列出所有业务员(CLERK)的姓名,编号,和部门编号
select e.ename, e.empno, e.deptno from emp e where e.job = 'CLERK';

-- 找出奖金高于薪金的员工
select * from emp where comm > sal;

-- 找出奖金高于薪金的60%的员工
select * from emp where comm > sal * 0.6;

-- 找出部门10中所有经理(MANAGER)和部门20中所有业务员(CLERK)的详细资料
select * from emp e 
where e.deptno = 10 and e.job = 'MANAGER' 
    or e.deptno = 20 and e.job = 'CLERK';

select * from emp e 
where (e.deptno = 10 and e.job = 'MANAGER') 
    or (e.deptno = 20 and e.job = 'CLERK');

select * from emp e where e.deptno = 10 and e.job = 'MANAGER'
union 
select * from emp e where e.deptno = 20 and e.job = 'CLERK';

-- 找出部门10中所有经理(MANAGER),部门20中所有业务员(CLERK),既不是经理又不是业务员但其薪水大于等于2000的所有员工的详细资料
select * from emp e where e.deptno = 10 and e.job = 'MANAGER' 
union
select * from emp e where e.deptno = 20 and e.job = 'CLERK'
union 
select * from emp e where e.sal > 2000 and e.job not in('MANAGER', 'CLERK');

-- 找出收取奖金的员工的不同工作
select distinct e.job from emp e;

-- 找出不收取奖金或收取的奖金低于100的员工
select * from emp e where e.comm is null or e.comm < 100;

-- 找出各月倒数第3天受雇的所有员工
select * from emp e where e.hiredate between last_day(hiredate)-3 and last_day(hiredate);

-- 找出早于30年前受雇的员工
select * from emp e where (sysdate - e.hiredate)/365 > 30;

-- 以首字母大写的方式显示所有员工的姓名
select initcap(ename) from emp;

-- 显示正好为5个字符的员工姓名
select * from emp where length(ename) = 5;

-- 显示不带有”R”的员工姓名
select * from emp where ename not like '%K%';

-- 显示所有员工姓名的前三个字符
select substr(ename, 0, 3) from emp;

-- 显示所有员工的姓名,并用’a’替换所有’A’
select replace(ename, 'A', 'a') from emp;

-- 显示满30年服务年限的员工姓名和受雇日期
select * from emp where (sysdate - hiredate)/365 > 30;

-- 显示员工的详细资料,按姓名由大到小排序
select * from emp order by ename desc;

-- 显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面
select ename, hiredate from emp order by hiredate asc;

-- 显示所有员工的姓名,工作和薪金,按工作降序排列,若工作相同则按薪金升序排序
select ename, job, sal from emp order by job desc, sal asc;
select ename, job, sal from emp order by 2 desc, 3;

-- 显示所有员工的姓名,加入公司的年份和月份,按受雇日期所在的月排序,若月份相同,则将最早年份排在最前面
select ename, to_number(to_char(hiredate, 'yyyy')) Year, to_number(to_char(hiredate, 'mm')) from emp order by 3 desc, 2 asc;

-- 显示一个月为30天的情况所员工的日薪金,忽略余数
select round(sal/30) 日薪 from emp;

-- 找出在(任何年份)2月受聘的所有员工
select * from emp where to_number(to_char(hiredate, 'mm'))= 2;

-- 对每个员工,显示其加入公司的天数
select ename, round(sysdate - hiredate) Days from emp;

-- 显示姓名中任意位置包含“A”的所有员工姓名
select * from emp where upper(ename) like '%A%';

-- 以年月日方式显示所有员工的服务年限
select ename, hiredate, 
trunc(months_between(sysdate, hiredate) /12)  year  ,
trunc(mod(months_between(sysdate, hiredate) ,  12 ) ) months  , 
trunc(sysdate - add_months(hiredate,months_between(sysdate, hiredate))) day
from emp ;


你可能感兴趣的:(trunc,ADD_MONTHS,months_between)