SQL与HQL练习题

中的所有员工。

select * from emp where deptno = 30

from  Emp  e  where e.deptno = 30

2. 列出所有办事员(CLERK)的姓名、编号和部门编号。

select t.ename,t.empno,t.deptno from emp t where t.job = ‘CLERK’

select t.ename,t.empno,t.deptno from Emp t where t.job = ‘CLERK’

3. 找出佣金高于薪金的员工。

Select * from emp where comm>sal

From Emp where comm > sal

4. 找出佣金高于薪金的60%的员工。

Select * from emp where comm > (sal*0.6)

From Emp where comm > (sal*0.6)

5. 找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料。

select * from emp where (job = ) or (job = );

From Emp where (job = ) or (job = );

6. 找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK),既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料。

select * from emp  where (job = ) or (job = )or(job <> );

select * from Emp where (job = ) or (job = )or(job <> );

7. 找出收取佣金的员工的不同工作。

select distinct t.job from emp t where t.comm >

select distinct t.job from Emp t where t.comm >

8. 找出不收取佣金或收取的佣金低于100的员工。

select * from emp t where t.comm < or t.comm is null

select * from Emp t where t.comm < or t.comm is null

9. 查询所有部门名称和员工姓名,包括没有员工的部门名称也显示。

select d.dname,e.ename from dept d  left join emp e on e.deptno = d.deptno;

select d.dname,e.ename from Dept d  left join Emp e with e.deptno = d.deptno;

10. 查询工资高于公司平均工资的所有员工信息。

select * from emp  where sal > (select avg(sal) from emp);

from Emp t where t.sal in(select avg(e.sal) from Emp e)

11. 查询工资高于部门平均工资的所有员工。

select * from emp t where t.sal >(select avg(e.sal) from emp e where t.deptno = e.deptno)

from Emp t where t.sal >(select avg(e.sal) from Emp e where t.deptno = e.deptno)

12. 查询emp表的第1~3行。

select * from emp t where rownum <=

session.createQuery(from Emp).setFirstResult(0).setMaxResults(3).list()

13. 把员工姓名和工作类型连接在一起,中间用“-”分割显示(concat函数)。

select CONCAT(ENAME||个字符和第4个字符以后的内容显示。

select substr(ENAME,,)||) from emp

select substring(ename,,)||) from Emp

15. 查询员工编号,姓名和所在部门的名称。

select EMPNO,ENAME,DNAME from emp t,dept d where t.deptno = d.deptno

select empno||’’||ename||’’||dname from Emp t,Dept d where t.deptno = d.deptno

16. 查询部门的名称以及该部门的人数,要求没有员工的部门也要显示。

select DNAME,count(e.EMPNO) from dept t left join emp e on t.deptno = e.deptno group by DNAME

select dname,count(e.empno) from Dept t left join Emp e on t.deptno = e.deptno group by dname

Sting = “i”

17. 查询员工姓名及其直接上级的姓名。

select e.ENAME,t.ENAME from emp e,emp t where e.mgr = t.empno

select e.ename||’的上级’||t.ename from Emp e,Emp t where e.mgr = t.empno

18. 查询工资高于平均工资的员工姓名。

select e.ENAME from emp e where e.sal > (select avg(sal) from emp);

select e.ename from Emp e where e.sal >(select avg(t.sal) from Emp t)

19. 查询工资高于本部门平均工资的员工。

select e.ENAME from emp e where e.SAL > (select avg(t.SAL) from emp t where e.DEPTNO = t.DEPTNO);

select e.ename from Emp e where e.sal> (select avg(t.sal) from Emp t where e.deptno = t.deptno)

20. 查询每个部门中拥有最高工资的员工的信息。

select e.ENAME from emp e where e.SAL = (select max(t.SAL) from emp t)

select e.ename from Emp e where e.sal = (select max(t.sal) from Emp t)

21. 统计“JONES”所带领的团队的工资总额。

select sum(e.SAL) from emp e where e.MGR = (select t.MGR from emp t where t.ENAME = 号部门中有而20号部门中没有的工作。

select e.job from emp e where e.deptno =  and e.job not in(select t.job from emp t where t.deptno = )

select e.job from Emp e where e.deptno =  and e.job not in(select t.job from Emp t where t.deptno = )

23. 记录并集(union)

select * from emp where EMPNO =  union select * from emp where EMPNO =

union select * from Emp where EMPNO =

,+年前参加工作的员工。

select * from emp t where t.hiredate <(select  sysdate - interval  = hiredate

11. 查询每个员工的工作天数。

select sysdate-hiredate as days from emp

天计算员工的日薪金,要求计算结果四舍五入到小数点后2位。

select  trunc(t.SAL/,)  from emp t

select  trunc(t.sal/,)  from Emp t

14. 计算每个员工已经工作了多少个月,要求忽略小数部分。

select trunc((sysdate-hiredate)/) from emp

select trunc((sysdate-hiredate)/) from Emp

15. 按年和月的格式显示员工参加工作的时间。

select to_char(hiredate,年2月到1987年5月之间(包括2月和5月)参加工作的员工。

select * from emp t where t.hiredate between to_date(的部门和最低工资。

select a.deptno, (select min(sal) from emp where deptno=a.deptno) minSal 
from emp a group by a.deptno

select a.deptno, (select min(sal) from Emp where deptno=a.deptno) minSal 
from Emp a group by a.deptno

19. 统计每个部门工资在1400元以上的所有员工的工资总额。

select e.deptno,(select sum(sal) from emp where deptno = e.deptno) as sumsal from emp e  where e.sal >  group by deptno

select e.deptno,(select sum(sal) from Emp where deptno = e.deptno)  from Emp e  where e.sal >  group by deptno

20. 统计不同工作的个数。

select count(distinct job) from emp

select count(distinct job) from Emp

21. 找出各月倒数第3天受雇的所有员工。

select * from emp  where hiredate = last_day(hiredate)-

22. 找出早于12()年前受雇的员工。

select * from emp where hiredate < (select  sysdate - interval 个字符的员工的姓名。

select ename from emp where LENGTH(ename)=

25. 显示不带有“R”的员工的姓名。

select ename  from emp where ename not like ,) from emp

select substring(ename,0,3) from Emp

27. 显示所有员工的姓名,用“a”替换所有“A”。

select replace(ename,)年服务年限的员工的姓名和受雇日期。

select hiredate from emp where hiredate < (select  sysdate - interval 天的情况所有员工的日薪金,忽略余数。

select ename,floor(sal/) from emp where to_char(last_day(hiredate),

34. 找出在(任何年份的)2月受聘的所有员工。

select * from emp where to_char(hiredate,

35. 对于每个员工显示其加入公司的天数。

select sysdate - hiredate from emp

36. 显示姓名字段的任何位置包含“A”的所有员工的姓名。

select ename from emp where ename like )||,)*))||,)*),)*)||的各种工作。

select job from emp e where(select min(sal) from emp t where t.job = e.job)> group by e.job

select job from Emp e where(select min(sal) from Emp t where t.job = e.job)> group by e.job

45. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。

select ename from emp where deptno = (select deptno from dept where dname = 中员工的薪金的所有员工的姓名和薪金。

select e.ename,e.sal from emp e where e.sal in (select sal from emp t where t.deptno = )

select e.ename,e.sal from Emp e where e.sal in (select sal from Emp t where t.deptno = )

48. 列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。

select e.ename,e.sal from emp e where e.sal > (select max(sal) from emp t where t.deptno = )

select e.ename,e.sal from Emp e where e.sal > (select max(sal) from Emp t where t.deptno = )

49. 列出在每个部门工作的员工数量、平均工资和平均服务期限。

select deptno ,count(*) as employeeNum,avg(sal) as avgSal,avg(sysdate-hiredate) as workDays from emp group by deptno

50. 列出所有员工的姓名、部门名称和工资。

select e.ename,(select d.dname from dept d where d.deptno = e.deptno) as deptName ,e.sal from emp e;

select e.ename,(select d.dname from Dept d where d.deptno = e.deptno),e.sal from Emp e;

51. 列出所有部门的详细信息和部门人数。

select d.deptno,d.dname,d.loc,(select count(*) as empnum from emp  e where e.deptno = d.deptno) from dept d

select d.deptno,d.dname,d.loc,(select count(*) from Emp  e where e.deptno = d.deptno) from Dept d

52. 列出各种工作的最低工资。

select job,min(sal) from emp group by job

select job,min(sal) from Emp group by job

53. 列出各个部门的MANAGER(经理)的最低薪金。

select e.deptno,(select min(t.sal) from emp t where  t.deptno = e.deptno) from emp e group by e.deptno

select e.deptno,(select min(t.sal) from Emp t where  t.deptno = e.deptno) from Emp e group by e.deptno

54. 列出所有员工的年工资,按年薪从低到高排序。

select sal* from emp order by sal desc

select sal* from Emp order by sal desc

55. 列出工资最高的员工信息。

select * from emp where sal = (select max(sal) from emp )

select * from Emp where sal = (select max(sal) from Emp )

hql和sql练习题的更多相关文章

  1. SQL练习题完整(做完你就是高手)

    SQL 练习题答案 一.补充作业一.   设有三个关系:                S(SNO, SNAME, AGE, SEX,Sdept)                SC(SNO, CNO ...

  2. 二。Hibernate 查询 HQL、SQL方式

    hibernate的查询1.HQL方式:所有查询都是根据java对象名来完成,对象名替换表名2.SQL方式:保留原来的sql查询风格3.可以通过设置第一条和最大条数来实现各种数据库的分页查询4.通过B ...

  3. HQL和SQL查询

     转自http://blog.csdn.net/aaa1117a8w5s6d/article/details/7757097 HQL和SQL的区别 标签: sqlhibernatejavasessio ...

  4. 50道sql练习题和答案

    最近两年的工作没有写过多少SQL,感觉水平下降十分严重,网上找了50道练习题学习和复习 原文地址:50道SQL练习题及答案与详细分析 1.0数据表介绍 --1.学生表 Student(SId,Snam ...

  5. 深入HQL学习以及HQL和SQL的区别

    HQL(Hibernate Query Language) 是面向对象的查询语言, 它和 SQL 查询语言有些相似. 在 Hibernate 提供的各种检索方式中, HQL 是使用最广的一种检索方式. ...

  6. SQL练习题汇总(Sqlserver和Mysql版本)

    所需表及数据执行脚本: CREATE TABLE STUDENT (SNO ) NOT NULL, SNAME ) NOT NULL, SSEX ) NOT NULL, SBIRTHDAY DATET ...

  7. Hibernate 的HQL和sql有什么区别

    转自:https://blog.csdn.net/haozhugogo/article/details/54575802sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 ...

  8. 面试题: 数据库 sql优化 sql练习题 有用 学生表,课程表,成绩表,教师表 练习

    什么是存储过程?有哪些优缺点? 什么是存储过程?有哪些优缺点? 存储过程就像我们编程语言中的函数一样,封装了我们的代码(PLSQL.T-SQL). 存储过程的优点: 能够将代码封装起来 保存在数据库之 ...

  9. HQL和SQL的区别

    1.hql与sql的区别 sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 类名+类对象 where 后 用 对象的属性做条件 sql: from 后面跟的是表名    ...

随机推荐

  1. 【调试技巧】 Fiddler高级用法之url映射请求

    问题场景: 已发布线上APP出现接口错误,如何测试线上APP访问本地请求? 已发布线上H5页面,静态资源或js调试,如何映射本地js? 一般解决方案: 猜测(一般明显问题). 找到原发布包,修改请求资 ...

  2. 从源码的角度看 React JS 中批量更新 State 的策略(下)

    这篇文章我们继续从源码的角度学习 React JS 中的批量更新 State 的策略,供我们继续深入学习研究 React 之用. 前置文章列表 深入理解 React JS 中的 setState 从源 ...

  3. 如何利用Android Studio打包React Native APK

    ok!百度出来的东西很杂,所以,这里介绍一种最简单,最合适我们(新手,应该是吧)的APK的打包方式! 当然!这种打包是基于Android Studio的,所以,注意喽!!!! 废话不多说开始吧! 首先 ...

  4. linux-阿里云仓库搭建-搭建本地仓库-yum

    以上是同步元数据信息 安装完成———————————————————————————— 本地库的搭建 是建立在rpm之上的封装 可用的包 把仓库信息链接到本地 使用中文显示, 我们平时用的是oracl ...

  5. 《linux内核设计与实现》读书笔记——第三章

  6. 团队项目 NABCD分析java音乐播放器

    NABCD分析java音乐播放器 程设计题目:java音乐播放器 一.课程设计目的 1.编程设计音乐播放软件,使之实现音乐播放的功能. 2.培养学生用程序解决实际问题的能力和兴趣. 3.加深java中 ...

  7. 剑指offer:复杂链表的复制

    题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用, ...

  8. 虚拟机Linux(centos)系统能ping通主机,主机无法ping通Linux解决方案

    本文引用:https://blog.csdn.net/clean_water/article/details/53023308 三个步骤: 第一步:虚拟机网络连接方式选择Nat 第二步.关闭liunx ...

  9. The import * cannot be resolved

    背景 使用eclipse jee做练习的时候,下载了老师的项目源码.考虑到老师用的时myeclipse,目录结构略有不同,所有不想直接导入项目,又考虑到,可能环境不一样,会出现这样那样的问题,所以我的 ...

  10. TCP的TIME_WAIT

    http://www.cnblogs.com/dadonggg/p/8778318.html http://www.firefoxbug.com/index.php/archives/2795/ ht ...