sql语句基础(emp的40个问题及答案)

来源:互联网 发布:尖头平底鞋 知乎 编辑:程序博客网 时间:2024/06/05 22:41

这里包括40个基础的sql查询题目,看完可对sql语句的一些基本应用有个大概的了解,利用oracle中自带的emp和dept的数据表进行sql语句学习(用Scott/tiger登录或去下载),表结构为:

1. EMP表结构:
EMPNO    NUMBER(4)                员工号

ENAME    VARCHAR2(10) Y           员工名

JOB      VARCHAR2(9)  Y           职位

MGR      NUMBER(4)    Y           对应的领导员工号 

HIREDATE DATE         Y           入职日期

SAL      NUMBER(7,2)  Y           基本工资

COMM     NUMBER(7,2)  Y           奖金

DEPTNO   NUMBER(2)    Y 部门号

ps:部分名词解释,CLERK:    店员,职员;MANAGER:  经理;SALESMAN:推销员;ANALYST: 分析家,化验员;PRESIDENT:董事长;RESEARCH:研究员;

2. dept表结构

Name   Type         Nullable Default Comments 
------ ------------ -------- ------- -------- 
DEPTNO NUMBER(2)                  部门号
DNAME  VARCHAR2(14) Y             部门名 
LOC    VARCHAR2(13) Y 部门所在位置

(1) 查询 20 号部门的所有员工信息。

 select * from emp where deptno=20

(2) 查询所有工种为检测员的员工的工号、员工名和部门名。

  select e.empno, e.ename, d.dname from emp e, dept d where d.deptno=e.deptno and e.job='ANALYST'
(3) 查询奖金( COMM)高于工资( SAL)的员工信息。
select * from emp where comm>sal
(4) 查询奖金高于工资的 20%的员工信息。

select * from emp where comm>sal*0.2
(5) 查询 10 号部门中工种为项目经理和 20 号部门中工种为检测员的员工的信息。
select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='RESEARCH')
(6) 查询所有工种不是项目经理和检测员,且工资大于或等于 2000 的员工的详细信息。
select * from emp e,dept d where e.deptno = d.deptno and sal >= 2000 and job not in( 'MANAGER', 'ANALYST' )
(7) 查询有奖金的员工的不同工种。
select distinct job from emp where comm is not null
(8) 查询所有员工工资和奖金的和。
select sum(sal)+sum(comm) as total from emp
(9) 查询没有奖金或奖金低于 100 的员工信息。
select * from emp where comm<100 or comm is null
(10) 查询各月倒数第 2 天入职的员工信息。
select * from emp where  hiredate in (select  last_day(hiredate)-2 from emp)--这个hiredate含有星期,好难处理
(11) 查询员工工龄大于或等于 10 年的员工信息。
select * from emp where (sysdate-hiredate) > 10*365
(12) 查询员工信息,要求以首字母大写的方式显示所有员工的姓名。
 select initcap(ename) from emp
(13) 查询员工名正好为 6 个字符的员工的信息。
select * from emp where ename like '______' --6个下划线
(14) 查询员工名字中不包含字母“a”员工。
select ename from emp where ename not like '%A%'
(15) 查询员工姓名的第 2 个字母为“张”的员工信息。
select ename from emp where ename like '_张%'
(16) 查询所有员工姓名的前 3 个字符。
 select substr(ename,0,3) from emp
(17) 查询所有员工的姓名,如果包含字母“b”,则用“B”替换。
select translate(ename, 'b', 'B') from emp
(18) 查询员工的姓名和入职日期,并按入职日期从先到后进行排列。
select ename, HIREDATE from emp order by hiredate asc
(19) 显示所有的姓名、工种、工资和奖金,按工种降序排列,若工种相同则按工资升序排列。
select ename, job, sal, comm from emp order by job desc, sal asc
(20) 显示所有员工的姓名、入职的年份和月份,若入职日期所在的月份排序,若月份相同则按入职的年份排序。
select ename, to_char(hiredate, 'yyyy')as year, to_char(hiredate, 'MM')as month from emp order by year desc,month desc
(21) 查询在 2 月份入职的所有员工信息。
select * from emp where to_char(hiredate, 'MM') = '02'
(22) 查询至少有一个员工的部门信息。
select * from dept where deptno in (select deptno from emp where ename is not null)
(23) 查询工资比ALLEN员工工资高的所有员工信息。
select * from emp where sal>(select sal from emp where ename='ALLEN')
(24) 查询最低工资大于 2500 的各种工作。
select job from emp where sal>2500
(25) 查询在销售部门工作的员工的姓名信息。
select e.ename from emp e,dept d where e.deptno = d.deptno and d.dname = 'SALES'
(26) 查询工资高于公司平均工资的所有员工信息。
select * from emp where sal >(select avg(sal) from emp) order by sal asc
(27) 查询与 a 张三员工从事相同工作的所有员工信息。
select * from emp where deptno = (select deptno from emp where ename='CLARK')
(28) 列出工资等于30号部门中某个员工工资的所有员工的姓名和工资。
select * from emp where sal = (select sal from emp where deptno=30 and ename='JAMES')
(29) 查询工资高于30号部门中工作的所有员工的工资的员工姓名和工资。
select * from emp where sal > (select max(sal) from emp where deptno=30 )
(30) 查询从事同一种工作但不属于同一部门的员工信息。
select distinct a.* from emp a, emp b where a.job=b.job and a.deptno != b.deptno order by a.empno asc
(31) 查询各种工作的最低工资。
select job,min(sal) from emp  group by job
(32) 查询各个部门中的不同工种的最高工资。
select d.dname, e.job, max(e.sal) from emp e, dept d where d.deptno = e.deptno  group by d.dname,e.job 
(33) 查询 10 号部门员工以及领导的信息。
select * from emp where deptno = 10 order by job desc
(34) 查询各个部门的人数及平均工资。
select d.dname, count(e.empno) as 人数,avg(e.sal) as 平均工资 from emp e, dept d where e.deptno = d.deptno group by d.dname
(35) 查询工资为某个部门平均工资的员工信息。
select * from emp where sal in (select avg(sal) from emp group by deptno )
(36) 查询工资高于本部门平均工资的员工的信息。
select * from emp e,(select deptno, avg(sal) as sal  from emp group by deptno) b where e.deptno=b.deptno and e.sal >b.sal order by e.deptno
(37) 查询工资高于 20 号部门某个员工工资的员工的信息。
select *  from emp where sal > (select sal from emp where ename='JONES')
(38) 统计各个工种的人数与平均工资。
select job,count(empno),avg(sal) from emp group by job
(39) 统计每个部门中各个工种的人数与平均工资。
select deptno,job,count(empno),avg(sal) from emp group by job,deptno order by deptno asc
(40) 查询部门人数大于5的部门的员工的信息。
select * from emp where deptno in (select deptno from emp group by deptno having count(empno) > 5)


0 0
原创粉丝点击