SQL函数知识点

SQL题目(一)

1、查询部门编号为10的员工信息

select*from emp where empno=10;

2、查询年薪大于3万的人员的姓名与部门编号

select ename,sal from emp where sal*12>30000

3、查询佣金为null的人员姓名与工资

select*from emp where comm is null

4 查询工资大于1500 且 and 含有佣金的人员姓名

select*from emp where comm >0 and sal>1500

5 查询工资大于1500 或 or含有佣金的人员姓名

select*from emp where comm >0 or sal>1500

6查询姓名里面含有 S 员工信息 工资、名称

select SAL,ENAME from emp where ename like '%S%'

7求姓名以J开头第二个字符O的员工姓名的与工资

select ENAME, SAL,empno from emp where ename like '%J%'and ename like '_O%'

8、求包含%的雇员姓名

(1)insert into emp(empno,ename) values(9527,'huan%an');

(2)select * from emp where ename like '%\%%' escape '\'

9 使用in查询部门名称为 SALES 和 RESEARCH 的雇员姓名、工资、部门编号

select * from emp where job in('SALES','CLERK')

10 使用exists查询部门名称为SALES和RESEARCH 的雇员姓名、工资、部门编号。

select * from emp where exists(select * from dept where job in('SALES','CLERK'))

select empno,ename,job,sal from emp e where exists(select* from dept d where job in('SALES','CLERK') and e.epstno=d.epstno)

SQL题目(二)

1、查询10号部门中编号最新入职的员工,工龄最长的员工的个人信息。

2、从“software”找到‘f’的位置,用‘*’左右填充到15位,去除其中的‘a’。

3、查询员工的奖金,如果奖金不为NULL显示‘有奖金’,为null则显示无奖金

4、写一个查询显示当前日期,列标题显示为Date。再显示六个月后的日期,下一个星期 日的日期,该月最后一天的日期。

5、查询EMP表按管理者编号升序排列,如果管理者编号为空则把为空的在最前显示

6、求部门平均薪水

7、按部门求出工资大于1300人员的 部门编号、平均工资、最小佣金、最大佣金,并且最大佣金大于100

8、找出每个部门的平均、最小、最大薪水

9、查询出雇员名,雇员所在部门名称, 工资等级。

*/

--1、查询10号部门中最新入职的员工,工龄最长的员工的个人信息。

select e.* from emp e where e.hiredate in (

(select max(e1.hiredate) from emp e1 where e1.deptno = 10),

(select min(e2.hiredate) from emp e2 where e2.deptno = 10));

--2、从“software”找到‘f’的位置,用‘*’左右填充到15位,去除其中的‘a’。

select instr('software','f') from dual;

select lpad('software',15,'*') from dual;

select rpad('software',15,'*') from dual;

select replace('software','a') from dual;

--3、查询员工的奖金,如果奖金不为NULL显示‘有奖金’,为null则显示无奖金

select decode(e.comm,null,'无奖金','有奖金') from emp e;

--4、写一个查询显示当前日期,列标题显示为Date。

--再显示六个月后的日期,下一个星期 日的日期,该月最后一天的日期。

select sysdate "Date", add_months(sysdate,6) 六个月后 ,next_day(sysdate,'星期日') 下个星期日, last_day(sysdate) 该月最后一天 from dual;

--5、查询EMP表按管理者编号升序排列,如果管理者编号为空则把为空的在最前显示 (nulls first)

select * from emp e order by e.mgr asc nulls first;

--6、求部门平均薪水

select e.deptno ,avg(e.sal) from emp e group by e.deptno;

--7、按部门求出工资大于1300人员的 部门编号、平均工资、最小佣金、最大佣金,并且最大佣金大于100

select e.deptno,avg(e.sal),min(e.comm),max(e.comm) from emp e where e.sal>1300 group by e.deptno having max(nvl(e.comm,0))>100;

--8、找出每个部门的平均、最小、最大薪水

select e.deptno,avg(e.sal),min(e.sal),max(e.sal) from emp e group by e.deptno;

--9、查询出雇员名,雇员所在部门名称,工资等级。

select * from emp e ,dept d where e.deptno = d.deptno;

select * from salgrade;

select e.ename,d.dname from emp e, dept d where e.deptno = d.deptno;

select e.ename ,sg.grade from emp e,salgrade sg where e.sal between sg.losal and sg.hisal;

select e.ename, d.dname, sg.grade

from emp e, dept d, salgrade sg

where e.deptno = d.deptno

and e.sal between sg.losal and sg.hisal;

/*

1、查询82年员工

2、查询32年工龄的人员

3、显示员工雇佣期 6 个月后下一个星期一的日期

4、找没有上级的员工,把mgr的字段信息输出为 "boss"

5、为所有人长工资,标准是:10部门长10%;20部门长15%;30部门长20%其他部门长18%

*/

select * from emp;

--1、查询82年员工

select e.ename from emp e where to_char(e.hiredate,'yy') = '82';

--2、查询32年工龄的人员

select e.*,(months_between(sysdate,e.hiredate)/12) from emp e where (months_between(sysdate,e.hiredate)/12) >= 32;

--3、显示员工雇佣期 6 个月后下一个星期一的日期

select next_day(add_months(e.hiredate,6),'星期一') from emp e;

--4、找没有上级的员工,把mgr的字段信息输出为 "boss"

select e.ename,nvl(to_char(e.mgr),'boss') from emp e;

--5、为所有人长工资,标准是:10部门长10%;20部门长15%;30部门长20%,其他部门长18%

select e.ename,e.sal,e.deptno,(case e.deptno when 10 then e.sal*1.1

when 20 then e.sal*1.15

when 30 then e.sal*1.2

else e.sal*1.18 end ) from emp e;

select e.,decode(e.deptno,10,e.sal1.1,20,e.sal1.15,30,e.sal1.2,e.sal*1.18) from emp e;

select * from emp;

需要注意的点

--分组可以按多个列分组

select e.job,e.sal from emp e group by e.job,e.sal;

--group by分组的列可以不出现在select里,检索字段(select后面跟着的字段)必须出现在分组列表里。

--错误的:select e.job,e.detpno from emp e group by e.job;

--正确的:select e.deptno from emp e group by e.deptno,e.sal,e.job;

--如果分组列中具有null值,则null将作为一个分组返回。如果列中有多行null值,他们将分为一组。

select comm from emp e group by e.comm;

--group by 子句必须出现在where子句之后,order by 子句之前。

select e.deptno from emp e where e.sal>800 group by e.deptno having e.deptno=20 order by e.deptno desc

--where过滤行记录 having 过滤组记录

--select 后面只能跟列( group by 出现的列)或组函数

--不能在 WHERE 子句中使用组函数

--错误的:select e.deptno from emp e where avg(e.sal) group by e.deptno;

--求部门下雇员的平均工资>2000 人数

select e.deptno ,count(1),avg(e.sal) from emp e group by e.deptno having avg(e.sal) > 2000;

/*

Sql语句执行过程

1.读取from子句中的基本表、视图的数据,[执行笛卡尔积操作]。

2.选取满足where子句中给出的条件表达式的元组

3.按group子句中指定列的值分组,同时提取满足Having子句中组条件表达式的那些组

4.按select子句中给出的列名或列表达式求值输出

5.Order by子句对输出的目标表进行排序。

Sql语句执行顺序

from - where - group by - having - select - order by

组函数

--1.min() 求一组数据中的最小值

select min(e.sal) from emp e;

--2.max() 求一组数据中的最大值

select max(e.sal) from emp e;

--3.avg() 求一组数据中的平均值

select avg(e.sal) from emp e;

--4.sum() 求一组数据中的和

select sum(e.sal) from emp e;

--5.count() 求一组数据有多少行

select count(1) from emp e;

SQL函数知识点的更多相关文章

  1. sql注入知识点整理(基础版)

    sql注入知识点整理(基础版) 基本步骤 判断是否报错 判断闭合符号 判断注入类型 构建payload 手工注入或者编写脚本 基本注入类型 报错型注入 floor公式(结果多出一个1):and (se ...

  2. Oracle 中的sql函数以及分页

    SELECT LPAD(,'*.') "LPAD example" FROM DUAL; 1.分页查询 (1)方法一:使用  between  and 来实现分页 select * ...

  3. SQL函数说明大全

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  4. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  5. [转]字符型IP地址转换成数字IP的SQL函数

    使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助.      /**//*--调用示例       sele ...

  6. 常用的Sql 函数

    常用的Sql 函数 1: replace 函数,替换字符. 语法 replace (original-string, search-string, replace-string ) 第一个参数你的字符 ...

  7. 常用的 SQL 函数

    SQL 函数 聚合函数(针对数字列): AVG:求平均分  COINT: 计算个数  MAX: 求最大值  MIN: 求最小值  SUM: 求和 数学函数():  ABS:     绝对值  CEIL ...

  8. Oracle数据库--SQL函数

    Oracle SQL函数  1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii( ...

  9. 常用的sql函数

    常用的sql函数 concat('hello','world') 结果:helloworld  作用:拼接 substr('helloworld',1,5)      hello           ...

随机推荐

  1. Python利用openpyxl带格式统计数据(2)- 处理mysql数据

    上一篇些了openpyxl处理excel数据,再写一篇处理mysql数据的,还是老规矩,贴图,要处理的数据截图: 再贴最终要求的统计格式截图: 第三贴代码: 1 ''' 2 #利用openpyxl向e ...

  2. [.NET] - 在Create一个RSA密钥的是要注意的长度问题

    有时候我们需要自己手动的创建RSA密钥,但是在密钥创建之后,在使用的时候会有类似密钥长度不正确的错误信息被抛出,那可能就是在创建一个RSA密钥的时候,对于的elements长度没设置正确,所以的ele ...

  3. 2.mysql explain命令详解

    EXPLAIN详解 SQL编写和解析 编写过程 select-distinct-from-join-on-where-group by-having-order by-limit- 解析过程 from ...

  4. 为了搞清楚类加载,竟然手撸JVM!

    作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...

  5. 你的项目中使用过哪些JSTL标签?

    项目中主要使用了JSTL的核心标签库,包括< c:if>.< c:choose>.< c: when>.< c: otherwise>.< c:f ...

  6. 所有CSS字体属性

    font(在一个声明中设置所有的字体属性) font-family(指定文本的字体系列) font-size(指定文本的字体大小) font-style(指定文本的字体样式) font-variant ...

  7. 算法图解第一章_二分查找_python

    什么是二分查找? 我们先玩一个游戏. 在1至100之间我写下一个数,由你来猜测这个数是多少.我会告诉你高了还是低了. 最简单的办法就是每次取一半. 例如 "50""低了& ...

  8. Redis底层数据结构实现

    REDIS  较宽泛的支持5种数据结构  分别为 字符串 列表 集合 散列 有序集合 关于这几种数据结构的使用 相信网上有很多资料,查看官网API 也很详细了  读者可以自己随意翻阅 很方便 . 接下 ...

  9. k8s之HTTP请求负载分发

    一.导读 对于基于HTTP的服务来说,不同的URL地址经常对应不同的后端服务或者虚拟服务器,通常的做法是在应用前添加一个反向代理服务器Nginx,进行请求的负载转发,在Spring Cloud这个微服 ...

  10. RMI之由浅入深(一)

    0x01.什么是RMI RMI(Remote Method Invocation)即Java远程方法调用,RMI用于构建分布式应用程序,RMI实现了Java程序之间跨JVM的远程通信.顾名思义,远程方 ...