-- SQL COOKBOOK CHAPTER1
-- 查看所有内容
select * from emp;
-- 可以单列
select empno,ename,job,sal,mgr,hiredate,comm,deptno
from emp;
-- 设定条件
select * from emp
where deptno=10;
-- 条件可以用逻辑运算符连接
select * from emp
where deptno=10
or comm is null
or sal<=2000
and deptno=20;
-- 注意括号和顺序
select * from emp
where (
deptno=10
or comm is not null
or (deptno=20 and sal<=2000)
);
-- 起有意义的别名
select sal as salary, comm as commission from emp;
-- 使用子查询可以在条件中用别名筛选
select * from
(
select sal as salary, comm as commission
from emp
)x
where salary<5000;
-- 代码执行的顺序:from -> where -> select
select ename, job
from emp
where deptno=10;
-- 连接符号
select ename||' WORKS AS A '||job as msg
from emp where deptno=10;
-- || 是concat的快捷方式
select concat(ename,' WORKS AS A ', job) FROM EMP where deptno=10;
-- case when
select ename,sal,
case when sal<=2000 then 'UNDERPAID'
when sal>=4000 then 'OVERPAID'
ELSE 'OK'
end as status
from emp;
-- 取前5行
select * from emp limit 5;
-- 任意序列的前5行
select ename, job from emp order by random() limit 5;
-- 查询空值
select * from emp where comm is null;
-- 空值置换
select coalesce(comm, 0) from emp;
-- in的使用
select ename,job,deptno from emp where deptno in (10, 20);
-- like的使用
select ename,job from emp where deptno in (10,20) and (ename like '%I%' or job like '%ER')
SQL COOKBOOK SQL经典实例代码 笔记第一章代码
原文:https://www.cnblogs.com/heenhui2016/p/11623711.html