SQL函数知识点
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函数知识点的更多相关文章
- sql注入知识点整理(基础版)
sql注入知识点整理(基础版) 基本步骤 判断是否报错 判断闭合符号 判断注入类型 构建payload 手工注入或者编写脚本 基本注入类型 报错型注入 floor公式(结果多出一个1):and (se ...
- Oracle 中的sql函数以及分页
SELECT LPAD(,'*.') "LPAD example" FROM DUAL; 1.分页查询 (1)方法一:使用 between and 来实现分页 select * ...
- SQL函数说明大全
一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...
- [转]字符型IP地址转换成数字IP的SQL函数
使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助. /**//*--调用示例 sele ...
- 常用的Sql 函数
常用的Sql 函数 1: replace 函数,替换字符. 语法 replace (original-string, search-string, replace-string ) 第一个参数你的字符 ...
- 常用的 SQL 函数
SQL 函数 聚合函数(针对数字列): AVG:求平均分 COINT: 计算个数 MAX: 求最大值 MIN: 求最小值 SUM: 求和 数学函数(): ABS: 绝对值 CEIL ...
- Oracle数据库--SQL函数
Oracle SQL函数 1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii( ...
- 常用的sql函数
常用的sql函数 concat('hello','world') 结果:helloworld 作用:拼接 substr('helloworld',1,5) hello ...
随机推荐
- python scipy 求解简单线性方程组和fmin求函数最小值
###这是一个利用内置函数求最小值#####def func(x): return x ** 2 - 2 *x x = 1 func(x) opt.fmin(func ,x)## 用scipy求解线性 ...
- 关于新创公司所需的icp,网文,软著和备案的申请
刚从一个集团离职来到了创业团队,前期是什么都没有,甚至是公司名字都不知道,哈哈.所以就有了后面的坑踩了一遍又一遍.刚开始是在霍尔果斯注册,结果办icp费了半年的时间,东找西找还没下证.又碰上新疆严查不 ...
- cmd运行乱码或显示编码GBK的不可映射字符解决方法
出现这样的错误,一般是因为代码中含有中文字符,注释中的中文字符也算.由于使用cmd运行java程序的时候,系统默认的编码格式是gbk.而包含中文字符的代码一般是Unicode格式,所以直接运行含有中文 ...
- select * from 多张表的用法
select * from 多张表的用法 其实就是 inner join select * from Class c,Student s where c.ClassID=s.ClassID ...
- Keras使用多个GPU并行
model = Model(inputs=[v_i, v_j], outputs=output_list) model = multi_gpu_model(model,4) model.compile ...
- react状态管理器之mobx
react有几种状态管理器,今天先来整理一下mobx状态管理器,首先了解一下什么是mobx 1.mobx成员: observable action 可以干嘛: MobX 的理念是通过观察者模式对数据做 ...
- Access-Control-Allow-Headers等基础常识
跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种机制,该机制使用附加的 HTTP 头来告诉浏览器,准许运行在一个源上的Web应用访问位于另一不同源选定的资源. 当一个Web应用发起一个与 ...
- vue-cli Cannot find module 'less'
首先,今天下午对OneloT项目进行试图修改,我向在在线的时候先将接口中的数据缓存到本地,通过文件写入的方式,但是没有成功,捣鼓了一会.重新启动浏览器,发现命令行报错,error cannot fin ...
- Netty tcnative boringssl windows 32-bit 编译
1 问题 在使用Netty SSL时,我们往往会采用netty-tcnative-boringssl组件.但是netty-tcnative-boringssl在Windows上仅有64位版本的,没有3 ...
- elasticsearch基本概念和基本语法
Elasticsearch是基于Json的分布式搜索和分析引擎,是利用倒排索引实现的全文索引. 优势: 横向可扩展性:增加服务器可直接配置在集群中 分片机制提供更好的分布性:分而治之的方式来提升处理效 ...