表结构

SQL> desc emp
Name Type Nullable Default Comments
-------- ------------ -------- ------- --------
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

查询语句

--查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行,
--并指定列别名为"年收入"。(NVL(comm,0) comm取空值时用0替代)

select ename,12*(sal+nvl(comm,0)) 年收入 from emp;

--)查询EMP表显示工资超过2850的雇员姓名和工资
select ename,sal from emp where sal > 2850;

--查询EMP表显示工资不在1500~2850之间的所有雇员及工资
select ename,sal from emp where sal not between 1500 and 2850;

--查询EMP表显示代码为7566的雇员姓名及所在部门代码
select ename,deptno from emp where mgr=7566;

--查询EMP表显示部门10和30中工资超过1500的雇员名及工资

select ename,sal,deptno from emp where deptno in(10,30) and sal > 1500;
select ename,sal,deptno from emp where (deptno=10 or deptno=30) and sal>1500;

--查询EMP表显示第2个字符为"A"的所有雇员名其工资。
select ename,sal from emp where ename like '_A%';

select * from emp;

insert into emp(empno,ename,sal,deptno) values(8888,'zhang%san',8888,30);

select ename from emp where ename like '%x%%' escape('x');

--查询EMP表显示补助非空的所有雇员名及其补助。
select ename,comm from emp where comm is not null;

--查询EMP表显示所有雇员名、工资、雇佣日期,并以雇员名的升序进行排序。
select ename,sal,hiredate from emp order by ename asc;

--查询EMP表显示在1981年2月1日到1981年5月1日之间雇佣的雇员名、岗位及雇佣日期,并
--以雇佣日期进行排序。
select ename, job, hiredate
from emp
where hiredate between to_date('1981-2-1', 'yyyy-mm-dd') and
to_date('1981-5-1', 'yyyy-mm-dd')
order by hiredate desc;

--查询EMP表显示获得补助的所有雇员名、工资及补助,并以工资升序和补助降序排序
select ename,sal,comm from emp where comm is not null order by sal asc,comm desc;

--查询82年员工
select ename, hiredate
from emp
where hiredate between to_date('1982-1-1', 'yy-mm-dd') and
to_date('1982-12-31', 'yy-mm-dd');

--查询32年工龄的人员
select ename,hiredate,Months_between(sysdate,hiredate)/12 from emp where Months_between(sysdate,hiredate)/12 between 31 and 32;

--显示员工雇佣期 6 个月后下一个星期一的日期
select ename,hiredate,next_day(add_months(hiredate,6),'星期一') from emp;

--找没有上级的员工,把mgr的字段信息输出为 "boss"
select mgr,nvl(to_char(mgr),'boss') from emp;

--为所有人长工资,标准是:10部门长10%;20部门长15%;30
--部门长20%其他部门长18%
select ename,
sal,
deptno,
case
when deptno = 10 then
(sal * 0.1 + sal)
when deptno = 20 then
(sal * 0.15 + sal)
when deptno = 30 then
(sal * 0.2 + sal)
else
(sal * 0.18 + sal)
end
from emp;

--查询10号部门中编号最新入职的员工,工龄最长的员工的个人信息。
select *from emp where hiredate in
((select max(hiredate)from emp where deptno = 10),
(select min(hiredate) from emp where deptno = 10
));

select * from emp where deptno =10;

--从“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;

--查询员工的奖金,如果奖金不为NULL显示‘有奖金’,为null则显示无奖金
select ename,comm,decode(comm,null,'无奖金','有奖金') from emp;

--写一个查询显示当前日期,列标题显示为Date。再显示六个月后的日期,下一个
--星期 日的日期,该月最后一天的日期。
select sysdate "Date",add_Months(sysdate,6),next_day(sysdate,'星期日'),last_day(sysdate) from dual;

-- 查询EMP表按管理者编号升序排列,如果管理者编号为空则把为空的在最前显示
select * from emp order by mgr asc nulls first;

--求部门平均薪水
select deptno,avg(sal) from emp group by deptno;

--找出每个部门的平均、最小、最大薪水
select deptno,avg(sal),max(sal),min(sal) from emp group by deptno;
select * from emp;

--按部门求出工资大于1300人员的 部门编号、平均工资、最小佣金、最大佣金,并
--且最大佣金大于100
select deptno,avg(sal),min(comm),max(comm) from emp where sal>1300 group by deptno having max(nvl(comm,0))>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;

--查询出雇员名,雇员所在部门名称, 工资等级。
select * from emp e ,dept d where e.deptno = d.deptno;
select * from salgrade;
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;

sql语句基本查询操作的更多相关文章

  1. sql语句之查询操作

    语法顺序: select distinct 字段1,字段2,字段3 from 库.表 where 条件 group by 分组条件 having 过滤 # 执行顺序的话,到这步会返回运行select语 ...

  2. 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询

    /**     * 使用sql语句进行查询操作     * @param sql     * @return     */    public List queryWithSql(final Stri ...

  3. Statement执行DQL语句(查询操作)

    import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.T ...

  4. [Beego模型] 四、使用SQL语句进行查询

    [Beego模型] 一.ORM 使用方法 [Beego模型] 二.CRUD 操作 [Beego模型] 三.高级查询 [Beego模型] 四.使用SQL语句进行查询 [Beego模型] 五.构造查询 [ ...

  5. SQL语句关联查询

    一:连接类型: 关联查询:只有存在关联的表才能关联查询,完全独立的表之间无法关联 1.关联的类型:自关联,左关联,右关联,全关联(full join)两张表都是主表 2.关联的表:两张以上,以一张(或 ...

  6. SQL语句之表操作

    SQL语句系列 1.SQL语句之行操作 2.SQL语句之表操作 3.SQL语句之数据库操作 4.SQL语句之用户管理 写在前面 在上一篇博文里面我整理了“行”级别的操作,分别是“增(insert).删 ...

  7. SQL语句之行操作

    SQL语句系列 1.SQL语句之行操作 2.SQL语句之表操作 3.SQL语句之数据库操作 4.SQL语句之用户管理 关系型数据库的存储形式 在关系型数据库中,数据都是以类似于Excel表格的形式存储 ...

  8. MySQL的EXPLAIN命令用于SQL语句的查询执行计划

    MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...

  9. mysql 常用 sql 语句 - 快速查询

    Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互         1.1.1 mysql 连接             mysql.exe -hPup    ...

随机推荐

  1. Jump跳板机的搭建和部署

    Jump跳板机简绍   概括 Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. Jumpserver ...

  2. 安装grub到U盘分区,实现多系统引导

    目录 1.分区工具及分区类型 1.1 显示分区表和分区信息 1.1.1 fdisk -l 1.1.2 gdisk -l 1.1.3 parted -l 1.2 常见分区类型 1.3 分区样例 1.3. ...

  3. Mysql对用户的操作

    1.创建用户语法:CREATE USER 用户名@地址 IDENTIFIED BY '密码'; 2.给用户授权语法:GRANT 权限1, … , 权限n ON 数据库.* TO 用户名 3.撤销授权语 ...

  4. Temporary failure in name resolutionf的解决方法

    Linux有时还蛮烦的这个不能用那个不能用,只能多折腾了. 今天又是,ping z.cn的时候直接报错 Temporary failure in name resolutionf 这个一般都知道是DN ...

  5. H5 实现图片上传预览

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. 为什么要学习socket

    为什么要学习socket socket历史悠久.它们的使用始于 1971年的ARPANET,后来成为1983年发布的Berkeley Software Distribution(BSD)操作系统中的A ...

  7. SQL Server 快速大数据排序方法

    SQL Server 中虽然有 ORDER BY NewID() 方法,但对于数据量比较大的结果集来说,排序那慢的可不是一星半点. 微软官方给了一种方案,https://msdn.microsoft. ...

  8. 使用Python matplotlib做动态曲线

    今天看到“Python实时监控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也学习如何使用Python ma ...

  9. 生成用于ROM初始化的coe文件---使用matlab

    生成用于ROM初始化的coe文件---使用matlab t=0:2*pi/2^12:2*pi; y=0.5*sin(t)+0.5; r=ceil(y*(2^8-1)); fid = fopen('si ...

  10. AttributePriority

    还有AttributePriority,我们可以设置编译时优先级.如果我们对目标标记了多个aspect,这样postsharp就不确定注入先后顺序,这样不能确保正确性,在vs编译时候我们会看见警告:T ...