SQL入门,基础语句

Dept(部门表)

字段名

类型

描述

DEPTNO

NUMBER(2)

部门编号

DNAME

VARCHAR2(14)

部门名称

LOC

VARCHAR2(13)

办公地点

 

Emp(员工表)

字段名

类型

描述

EMPNO

NUMBER(4)

员工编号

ENAME

VARCHAR2(10)

员工姓名

JOB

VARCHAR2(9)

岗位

MGR

NUMBER(4)

该员工的直接上级

HIREDATE

DATE

入职日期

SAL

NUMBER(7,2)

基本工资

COMM

NUMBER(7,2)

提成

DEPTNO

NUMBER(2)

部门编号

 

SALGRADE(工资等级表)

字段名

类型

描述

GRADE

NUMBER

等级

LOSAL

NUMBER

该等级最低工资

HISAL

NUMBER

该等级最高工资

 

Bobus(工资条)

字段名

类型

描述

ENAME

VARCHAR2(10)

员工姓名

JOB

VARCHAR2(9)

岗位

SAL

NUMBER

工资

COMM

NUMBER

提成


SELECT
查询表中所有的数据
select * from dept;

查询某一列或某几列
select deptno from dept;
select deptno, dname from dept;

给查询结果的列起别名
select deptno dno, dname from dept;
select deptno 部门编号, dname from dept;
select deptno "部门 编号", dname from dept;

带计算式
select empno, ename, sal*12 from emp;

拼接字符串
select ename||'-entor' from emp;
select ename||' '||job from emp;

去除重复
select distinct deptno from emp;
select distinct deptno, job from emp;

WHERE
关系运算符 =, >, <, >=, <=, !=(<>)
select * from emp where empno=7934;

select * from emp where job='CLERK';

SELECT * FROM emp where sal > 1000;

select * from emp where deptno<>30;

select * from emp where hiredate>'02-4月 -81';

SELECT * FROM emp where sal*12 > 10000;

逻辑运算符 and, or, not
 select * from emp where deptno=20 and sal>1000;
select * from emp where deptno=20 or sal>1000;
select * from emp where not sal>1000;

select * from emp where (deptno=20 and sal>1000) or sal<1000;

范围运算符 between...and
select * from emp where sal>=1000 and sal<=2000;
select * from emp where sal between 1000 and 2000;

谓词运算符 in
select * from emp where empno=7499 or empno=7521 or empno=7654;
select * from emp where empno in (7499, 7521, 7654);

空判断 is null, is not null
select * from emp where comm is null;
select * from emp where comm is not null;

取出所有员工的月收入(工资+提成)
select empno, sal, comm, sal+comm from emp;  --计算式中包含null,结果一定是null

select empno, comm from emp where comm in (300, 500);
select empno, comm from emp where comm not in (300, 500); --in和not in的结果都不包含null
select empno, comm from emp where comm not in (300, 500, null);  --not in不能有null,否则所有行都被过滤

模糊查询Like
select ename from emp where ename like 'J%';
select ename from emp where ename like '%T%';

Order By
select empno, sal from emp order by sal;
select empno, sal from emp order by sal desc;

多个字段排序
select empno, sal from emp order by sal desc, empno asc;

select empno, sal from emp where sal>1000 order by sal desc;


组函数
sum, avg, max, min, count
select sum(sal) from emp;
select avg(sal) from emp;
select max(sal) from emp;
select min(sal) from emp;
select sum(sal) from emp where deptno=10;

count
select count(*) from emp;
select count(1) from emp;
select count(comm) from emp;  --针对某列计数,null不算有效行
select count(deptno) from emp;
select count(distinct deptno) from emp;  --先去重复,再计数

分组Group By
select deptno, sum(sal) from emp group by deptno;
select deptno, job, max(sal) from emp group by deptno, job;
select deptno, job,sal, max(sal) from emp group by deptno, job;  --会报错,sal字段不在group by 子句中,不能在select中出现

分组后的结果进行过滤Having
部门总工资高于7000的才显示
select deptno, sum(sal) from emp group by deptno having sum(sal)>7000;

多表查询
等值连接

--显示员工的姓名和部门名称
select ename,dname from dept, emp where dept.deptno=emp.deptno


不等值连接
--显示员工和他工资对应的工资等级
select emp.ename, sal, grade from emp, salgrade 
where emp.sal >= salgrade.losal
and emp.sal <= salgrade.hisal


表的别名
select d.deptno 
from dept d, emp e
where d.deptno=e.deptno;


--显示员工的姓名和部门名称和他工资对应的工资等级
select ename, dname, grade
from emp, dept, salgrade
where emp.deptno = dept.deptno
and emp.sal between salgrade.losal and salgrade.hisal


自连接
--显示员工姓名和他直接上级的姓名
select e1.ename, e2.ename
from emp e1, emp e2
where e1.mgr = e2.empno


JOIN
内连接 inner join
select * from emp e join dept d on d.deptno=e.deptno
where e.sal > 1000
and e.comm is not null

select * from emp e 
join dept d on d.deptno=e.deptno
join salgrade g on e.sal between g.losal and g.hisal
where e.sal > 1000
and e.comm is not null

select e1.ename, e2.ename
from emp e1
join emp e2 on e1.mgr = e2.empno


外连接
左外连接
select e.ename, d.dname from emp e  left join dept d on d.deptno=e.deptno


右外连接
select e.ename, d.dname from emp e  right join dept d on d.deptno=e.deptno


全外连接
select e.ename, d.dname from emp e  full join dept d on d.deptno=e.deptno


列出员工数量多于3人的部门名称及人数
select dept.dname,count(*) 
from emp 
join dept on emp.deptno=dept.deptno
group by dept.dname 
having count(*) > 3


列出各部门的平均工资和部门名称(没有员工的部门也要列出)
select dept.dname, avg(sal)
from emp 
right join dept on emp.deptno=dept.deptno
group by dept.dname 


子查询
列出工资最高的员工姓名及工资
select * from emp where sal=(select max(sal) from emp)


列出工资高于平均工资的员工信息
select * from emp where sal>(select avg(sal) from emp)


列出部门平均工资高于2000的所有部门的员工
select * from emp where deptno in 
(select deptno 
from emp 
group by deptno 
having avg(sal)>2000)


列出每个部门内工资最高的员工
select emp.* from emp, 
(select deptno, max(sal) maxsal from emp group by deptno) t
where emp.deptno=t.deptno
and emp.sal=t.maxsal


列出每个部门的平均工资对应的工资等级
select deptno,grade from 
(select deptno, avg(sal) avgsal from emp group by deptno) t
join salgrade on t.avgsal between losal and hisal

    原文作者:SQL
    原文地址: https://blog.csdn.net/weixin_39788493/article/details/80916538
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞