一、SQL简单查询
1.所谓简单查询就是查询一张数据表中所有数据行的内容。
①FROM 表名称 [别名]
②SELECT [distinct] * | 列名称 [别名],列名称 [别名].....
全部查询使用通配符 “ * ”完成
select* from emp;
查询几个固定的数据列,不再显示所有的列
select empno,ename from emp;
2.查询的基本内容
①使用distinct消除重复数据行显示的内容
select distinct ename from emp;
②除了简单的查询外,也可以对查询的数据列进行简单四则运算
查询雇员的编号、姓名、年薪
select empno,ename,sal*12 from emp;
但有时候查询出来的列不清楚表达的是什么,这是可以使用别名,但不建议使用中文。
select empno,ename,sal*12 income from emp;
雇员每年的年薪,5个月200元的电话补贴,4个月300元的高温补贴,2个月的500出差补贴
select empno,ename,(sal*12 +5*200+4*300+2*500) income from emp;
如果执行四则运算依然是先乘除后加减。
③查询常量
对于常量有如下三点说明:
▶ 如果常量是字符串,则要求使用“ ‘ ”,例如:'hello';
▶ 如果常量是数字,则直接编写,例如 : 10;
▶ 如果常量是日期,则按照日期的风格编写,使用“xx日-xx月-xx年”
直接查询常量
select '雇员',empno,ename from emp;
④也可以在select 中使用 “||”连接连个列
select empno||ename from emp;
查询雇员编号姓名收入,使用常量显示
select '编号:'|| empno ||',姓名:'||ename||',收入:'||sal income from emp;
二.SQL限定查询
1.关系运算
查询职位不等于SALESMAN,有两种表达方式,sql查询job中严格区分大小写
select empno,ename,job
from emp
where job!='SALESMAN';
select empno,ename,job
from emp
where job <> 'SALESMAN';
2.逻辑运算符
①与
查询工资大于1500和小于3000
select*
from emp
where sal>1500 and sal <3000;
②或
查询工资大于1500或小于3000
select*
from emp
where sal>1500 or sal <3000;
③非
3.空判断
查询所有领取佣金的雇员信息(佣金存在,不为空)
select* from emp where comm is not null;
select* from emp where not comm is null;
任何数据库,空的操作只能使用以上两个标记来判断
4.IN操作符
要求查询雇员编号是7369,7566,9999的雇员信息
select* from emp where empno=7369 or empno=7566 or empno=9999;
select* from emp where empno in(7369,7566,9999);
在指定的操作范围内使用in,操作比较简单
不在操作的范围内使用NOT IN
select* from emp where empno not in (7369,7566,9999);
select* from emp where not empno in (7369,7566,9999);
关于NOT IN 与 NULL的问题
①使用in包含有null(没有任何影响)
select* from emp where empno in (7369,7566,null);
②使用not in里面包含有null
select* from emp where empno not in(7369,7566,null);
使用not in是查询部分数据,里面有null就变为了全部查询,不会有任何结果返回
5.模糊查询LIKE
在使用like的时候可以使用连个通配符:
“—” :匹配任意的一个字符;
“%”:匹配任意的零位、一位或多位字符;
①查询以字母A开头的任意字符
select* from emp where ename like 'A%';
②查询第二个字符是A的任意字符
select* from emp where ename like '_A%';
③查询姓名中任意位置包含A的字符
select* from emp where ename like '%A%';
三.排序
针对查询结果进行排序
ORDER BY 字段 [ASC|DESC]
对于排序有两种方式:
ASC 升序 默认情况下是升序
DESC 降序
①查询雇员信息,要求工资由高到低排序
select* from emp order by sal desc;
②查询销售人员的信息,要求按照雇用日期由早到晚进行排序
select* from emp where job ='SALESMAN' order by hiredate desc;
③查询由工资高到低,如果工资相同,则按照雇用日期由早到晚排序
select* from emp order by sal desc ,hiredate asc;
select* from emp order by sal desc ,hiredate;
④查询每个雇员员工的编号,姓名,年薪,并按照年薪由高到低排列
select empno,ename,sal*12 income
from emp
order by income;
⑤查询佣金高于60%的所有员工
select* from emp where comm>sal*0.6;
⑥查询部门10中的所有经理或部门20中的所有经理
select*
from emp
where (deptno=10 and job='MANAGER' or deptno=20 and job='CLERK');
⑦查询部门编号是10的经理,或者部门编号是20的职员,或者工资大于2000但不包括经理、职员
select*
from emp
where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK') or (job not in ('MANAGER','CLERK')AND sal>=200);
⑧收取佣金的员工的不同工作
select distinct job
from emp
where comm is not null;
⑨显示姓名字段的任何位置包含'A'的所有员工的姓名,显示结果按照基本工资由高到低排序,基本工资相同按照雇用年限由早到晚排序,雇用年限相同按照职位升序。
第一步:显示姓名字段的任何位置包含'A'的所有员工的姓名
select* from emp where ename like '%A%';
第二步:显示结果按照基本工资由高到低排序
select* from emp where ename like '%A%' order by sal ;
第三步:基本工资相同按照雇用年限由早到晚排序
select* from emp where ename like '%A%' order by sal , hiredate ;
第四步:雇用年限相同按照职位升序
select* from emp where ename like '%A%' order by sal , hiredate ,job ;
对于较复杂的题目,分步进行。