select关键字
作用:检索“列”
注意:1.select后面的列可以起别名(查询的显示结果)
1) 列名后面一个空格后添加别名(别名中不许有“空格”)
2) 列名后面一个空格后使用双引号添加别名
3) 列名后面一个空格后使用as关键字,在as后面添加别名
2.distinct用于对显示结果的去重
1) distinct必须放在select后面
2) 如果查询有多列,必须满足多列值都相同时,方可去重。
from关键字
作用:检索“表”
注意:检索的表后可以添加别名(别名不需要被双引号引起)
where关键字
where关键字
作用:过滤“行”记录(record)
用法:
1.=,!=,<>,<,>,<=,>=,any,some,all
例子:select sal from emp where sal>2000;
2. is null,is not null
例子:select comm from emp where comm is not null;
3.between x and y
例子:select sal from emp where sal between 2000 and 3000
4.and 、 or 、 not
例子:select sal from emp where sal<2000 or sal>3000;
5.in(list),not in(list)
例子:select deptno from emp where deptno in(20,30);
6.exists(子查询)、not exists(子查询)
例子:select * from emp where exists(select * from dept where deptno != 50)
7.like模糊查询
“%”:匹配零个或若干个字符
“_”:匹配一个字符
在模糊查询中,如果查询的数据中有“%”,“_”时,可以使用escape自定义转义字符
例子:select ename from emp where ename like '%%%' escape ''
order by关键字
作用:用于对查询结果进行排序
用法:select * from emp where deptno = 20 order by sal desc,ename desc
1.利用asc 、desc对排序列进行升序或降序
2.order by后可以添加多个列(逗号分隔),当一个列的值相同时,在按第二列进行排序,依次类推