sql查询练习题

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

select * from emp e where e.deptno=20;

--(2)   查询奖金(COMM)高于工资(SAL)的员工信息。

select * from emp where comm>sal;

--(3)   查询奖金高于工资的20%的员工信息。

select * from emp where comm>sal*0.2;

--(4)   查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息。

select * from emp e

where (e.deptno=10 and e.job=‘MANAGER‘)

or (e.deptno=20 and e.job=‘CLERK‘)

--(5)   查询所有工种不是MANAGER和CLERK,

--且工资大于或等于2000的员工的详细信息。

select * from emp

where job not in(‘MANAGER‘,‘CLERK‘) and sal>=2000;

--(6)   查询有奖金的员工的不同工种。

select * from emp where comm is not null;

--(7)   查询所有员工工资和奖金的和。

select (e.sal+nvl(e.comm,0)) from emp e;

--(8)   查询没有奖金或奖金低于100的员工信息。

select * from emp where comm is null or comm<100;

--(9)   查询员工工龄大于或等于10年的员工信息。

select * from emp where (sysdate-hiredate)/365>=10;

--(10) 查询员工信息,要求以首字母大写的方式显示所有员工的姓名。

select initcap(ename) from emp;

select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp;

--(11) 显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序,

--若月份相同则按入职的年份排序。

select ename,to_char(hiredate,‘yyyy‘) year,to_char(hiredate,‘MM‘) month

from emp

order by month,year;

--(12) 查询在2月份入职的所有员工信息。

select * from emp where to_char(hiredate,‘MM‘)=‘02‘

--(13) 查询所有员工入职以来的工作期限,用“**年**月**日”的形式表示。

select e.ename,floor((sysdate-e.hiredate)/365)||‘年‘

||floor(mod((sysdate-e.hiredate),365)/30)||‘月‘

||floor(mod(mod((sysdate-e.hiredate),365),30))||‘日‘

from emp e;

--(14) 查询从事同一种工作但不属于同一部门的员工信息。

select a.ename,a.job,a.deptno,b.ename,b.job,b.deptno

from emp a,emp b

where a.job=b.job and a.deptno<>b.deptno;

--(15) 查询各个部门的详细信息以及部门人数、部门平均工资。

select d.deptno,count(e.empno),avg(e.sal),d.dname,d.loc

from emp e ,dept d

where e.deptno=d.deptno

group by d.deptno,d.dname,d.loc

--(16) 查询10号部门员工以及领导的信息。

select * from emp where empno in(

select mgr from emp where deptno=10) or deptno=10;

--(17) 查询工资为某个部门平均工资的员工信息。

select * from emp

where sal in(select avg(sal) from emp group by deptno);

--(18) 查询工资高于本部门平均工资的员工的信息。

select * from emp e1

where sal >(select avg(sal) from emp e2 where e2.deptno=e1.deptno);

--(19) 查询工资高于本部门平均工资的员工的信息及其部门的平均工资。

select e.*,a.avgsal

from emp e,

(select deptno,avg(sal) as avgsal from emp group by deptno) a

where a.deptno=e.deptno and e.sal>a.avgsal;

--(20) 统计各个工种的人数与平均工资。

select count(*),e.job,avg(e.sal) from emp e

group by e.job

--(21) 统计每个部门中各个工种的人数与平均工资。

select deptno,job,count(empno),avg(sal) from emp e

group by e.deptno,e.job

--(22) 查询所有员工工资都大于1000的部门的信息。

select * from dept where deptno in

(select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000));

--(23) 查询所有员工工资都大于1000的部门的信息及其员工信息。

select * from emp e join dept d

on d.deptno

in (select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000))

and d.deptno=e.deptno;

--(24) 查询所有员工工资都在900~3000之间的部门的信息。

select * from dept

where deptno not in(

select deptno from emp

where sal not between 900 and 3000);

--(25) 查询所有工资都在900~3000之间的员工所在部门的员工信息。

select * from emp a

where a.deptno in

(select distinct e.deptno from emp e

where e.sal between 900 and 3000);

--(26) 查询每个员工的领导所在部门的信息。

select d.* from dept d

where d.deptno in

(select distinct e2.deptno from emp e1,emp e2

where e1.empno=e2.mgr);

--(27) 查询人数最多的部门信息。

select * from dept

where deptno in

(select deptno from (select count(*) count,deptno from emp group by deptno)

where count in(select max(count)

from (select count(*) count ,deptno from emp group by deptno)));

--(28) 查询30号部门中工资排序前3名的员工信息。

select * from

(select sal from emp where deptno=30 order by sal desc) e

where rownum<4

--(29) 查询‘JONES‘员工及所有其直接、间接下属员工的信息。

select e.* from emp e

start with ename=‘JONES‘

connect by prior empno=mgr;

---(30)       查询SCOTT员工及其直接、间接上级员工的信息。

select e.* from emp e

start with ename=‘SCOTT‘

connect by prior mgr=empno;

--(31) 以树状结构查询所有员工与领导之间的层次关系。

select substr(sys_connect_by_path(ename,‘->‘),3),level

from emp

start with mgr is null

connect by prior empno=mgr;

--(32)向emp表中插入一条记录,员工号为1357,员工名字为oracle,

--工资为2050元,部门号为20,入职日期为2002年5月10日。

--(33)将各部门员工的工资修改为该员工所在部门平均工资加1000。

update emp e set sal=

1000+(select avg(sal) from emp where deptno=e.deptno);

--(34)查询工作等级为2级,1985年以后入职的工作地点为DALLAS的员工编号、

--姓名和工资。

select e.ename,e.empno,e.sal from emp e,salgrade s,dept d

where (e.sal between s.losal and s.hisal)

and (s.grade=2)

and to_char(e.hiredate,‘yyyy‘)>1985

and e.deptno=d.deptno

and d.loc=‘DALLAS‘;

--35.部门平均薪水最高的部门编号

select * from(

select avg(sal) avgsal,deptno

from emp group by deptno order by avgsal desc)

where rownum=1;

select deptno,avg(sal) from emp group by deptno having avg(sal)=(

select max(avg(sal)) avgsal

from emp group by deptno)

--36,部门平均薪水最高的部门名称

select d.* from dept d where deptno  in(

select deptno from emp group by deptno having avg(sal)=(

select max(avg(sal)) avgsal

from emp group by deptno))

--37.平均薪水最低的部门的部门名称

select d.* from dept d where deptno  in(

select deptno from emp group by deptno having avg(sal)=(

select  min(avg(sal)) avgsal

from emp group by deptno))

--38.平均薪水等级最低的部门的部门名称

select d.dname from dept d

where d.deptno in

(select a.deptno from

(select e.deptno from emp e,salgrade s

where (e.sal between s.losal and s.hisal)

group by e.deptno order by avg(s.grade)) a

where rownum=1);

--39.部门经理人中,薪水最低的部门名称

select dname from dept where deptno=

(select deptno from

(select deptno from emp where job=‘MANAGER‘ group by deptno

order by min(sal)) where rownum=1)

--40.比普通员工的最高薪水还要高的经理人名称

select ename from emp where sal>

(select max(sal) from emp where job not in

(‘MANAGER‘,‘PRESIDENT‘)) and job=‘MANAGER‘ or job=‘PRESIDENT‘

--41.删除重复部门,但是留下一项

insert into dept values(70,‘RESEARCH‘,‘DALLAS‘)

select deptno,dname,rowid from dept

delete from dept d

where rowid<>

(select min(rowid) from dept where dname=d.dname and d.loc=loc)

--42.更新员工工资为他的主管的工资,奖金

update emp e set sal=(select sal from emp where empno=e.mgr),

comm=(select comm from emp where empno=e.mgr)

update emp e set (sal,comm)=(select sal,comm from emp where empno=e.mgr)

rollback;

select * from emp;

sql查询练习题

时间: 2024-08-25 23:59:10

sql查询练习题的相关文章

MySQL-注释-Navicat基本使用-复杂查询练习题-解题思路-pymysql操作数据库-SQL注入-05

目录 mysql语句注释 navicat 的基本使用 特色(个人总结) 与数据服务器建立连接 创建&打开数据库.表 创建 打开 修改操作表结构 修改表结构 查询修改操作表数据 基本语句对应的操作 模型 ***** 特色功能 从数据库建立模型 模型页面基本操作 用模型设计数据库并导出 结构.数据导入导出 导出 导入 附属小功能 刷新小按钮 查看操作对应sql语句 执行时间查看 手动筛选数据 练习 数据准备 使用SQL语句导入表结构与数据 如何验证答案是否正确 题目 部分参考答案(只放了两题的) 少

Spring Data JPA原生SQL查询

package com.wanda.cms.dao.repository;import org.springframework.stereotype.Repository;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.Query;import java.math.BigInteger;import java.util.*; /*

oracle新建用户执行sql查询语句出现错误ORA-00942:表或视图不存在

oracle创建新用户后客户端执行SQL查询后出现错误提示如下: 执行查询语句如下: select * from sm_sales_order; ORA-00942:表或视图不存在 创建新用户并指定表空间和临时表空间 CREATE USER xxx IDENTIFIED BY xxxx DEFAULT TABLESPACE LMS TEMPORARY TABLESPACE TEMP; 授予系统权限connect grant connect to xxx; 授予对象权限,只限查询 grant se

提高SQL查询效率

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设置默认值0,确保表中num列没有null值,然后这样查询: select id from t where num=0 3.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放

Hibernate 关于执行sql查询语句(转)

原文  http://www.yshjava.cn/post/543.html 主题 SQLHibernate Hibernate对原生SQL查询的支持和控制是通过SQLQuery接口实现的.通过Session接口,我们能够很方便的创建一个SQLQuery(SQLQuery是一个接口,在Hibernate4.2.2之前,默认返回的是SQLQuery的实现类--SQLQueryImpl对象,在下文中出现的SQLQuery如非注明,都是指该子类)对象来进行原生SQL查询: session.creat

笔记:Hibernate SQL 查询

Hibernate 支持使用原生的SQL查询,使用原生SQL查询可以利用某些数据库特性,原生SQL查询也支持将SQL语句放在配置文件中配置,从而提高程序的解耦,命名SQL查询还可以用于调用存储过程. SQL查询是通过SQLQuery接口来表示的,SQLQuery接口是Query接口的子接口,完全可以使用Query接口的方法,SQLQuery增加了二个重载方法 addEntity():将查询到的记录与特定的实体关联 addScalar():江查询的记录关联标量值 执行SQL的步骤如下: 获取Hib

13.hibernate的native sql查询(转自xiaoluo501395377)

hibernate的native sql查询 在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hibernate推荐的HQL查询语句呢?这是因为HQL查询语句虽然方便我们查询,但是基于HQL的查询会将查询出来的对象保存到hibernate的缓存当中,如果在我们的一个大型项目中(数据量超过了百万级),这个时候如果使用hibernate的HQL查询的话,会一次将我们查询的对象查询出来后放到缓存中,这个时

Hibernate之SQL查询

Hibernate支持使用原生的SQL语句进行查询.使用原生的SQL的好处是:可以利用某些数据库的特性(不同的数据库SQL 语法会有所差异), 将原有的使用JDBC作为持久层技术的应用 ,迁移到使用Hibernate作为持久层技术. 我们知道 使用HQL查询 主要牵涉的是 Query接口,而使用SQL查询 主要牵涉的 是 SQLQuery接口  . 而SQLQuery接口 是 Query接口的子接口. 所以 Query接口中的方法 ,均可以在SQLQuery接口中使用 ,SQLQuery接口还多

Spark编程实现SQL查询的实例

1.Oracle中的SQL select count(1) from a_V_PWYZL_CUSTACCT_PSMIS t where not exists (select 1 from tb_show_multi_question q WHERE q.dqmp_rule_code = '仅比对系统有' and q.dqmp_role_id = '105754659' and q.DQMP_target_id = t.dqmp_mrid) AND NOT EXISTS (select /*+ i