oracle--基础

 

一、基本查询
1.查看所有信息
select * from 表名;

select * from stu;

2.显示指定字段
select 字段名1, 字段名2,。。。 from 表名
select first_name, age from stu;

3.给字段取别名: as 可以省略 ,如果别名为 关键字 必需加双引号


select first_name as 姓, age as 年龄 , sex as "select" from stu 表名;


4.给表取别另:as不加
select 表别名.字段名, 表别名.字段名,... from 表名 表的别名

select s.id , s.last_name from stu s;

5、字段的联接: ||

select 字段字名1||字段字名2||'字符串',.... 别名 from 表名;


select first_name||' '||last_name||'---'||age 姓名 from stu;

习题1:在s_emp表中,查找出LAST_NAME列和FIRST_NAME列中间加一个空格连在一起输出,其输出的列名为my$name的sql语句

6、删除重复

SELECT DISTINCT id, last_name FROM stu;

习题2:请查找出表S_DEPT中,NAME列不重复的内容

二.按条件查
select * from 表名 where 条件表达式

条件表达式: 真、假

对SQL语句返回的数据集进行筛选
--紧跟在FROM子句后
--由一个或多个限定条件组成,限定条件由表达式、比较运算符和字面值组成
--所有字符串和日期需要用单引号括起来,数值不需要单引号
--日期在Oracle里有特定的格式,'DD-MON-YY'(具体看日期的显示格式),否则作为一个字符串。

1.比较运算符:= , < , > ,<=,>=, != 或 <>

select * from 表名 where 字段名 比较运算符 值


select * from stu where sex='女';
select * from stu where age=20;
select * from stu where mathe>60;
select * from stu where first_name <> '王';// !=

习题3:到s_emp表中,查找出月工资大于2000的员工?
习题4:到s_emp表中,查找出title为 Warehouse Manager的员工?

2.通配符 (模糊查询)like / not like
字符串的值是区分大小写,其它的不区分大小写

%:0、1、多个字符
字段名字符类型

select * from 表名 where 字段名 like '含有通配符的字符串'

select * from stu where last_name like '%w%';

-:1个字符

select * from stu where last_name like 'w_';

select * from stu where last_name not like 'w_';

习题5:到s_emp表中,查找出LAST_NAM中含有m的,工资大于1000的员工?

3.算数运算符 (可以出现在where,但要与比较运算符一起 , select--from)
+,-,*,/ 字段为数值类型

select * from stu where (mathe+chinese) > 120;


4.逻辑运算符:and 、or、 in ()、between... and

select * from stu where mathe>60 and mathe<80;//在60与80之间的, 字段可以是不同的

select * from stu where chinese < 60 or chinese >80; //小于60 或 大于80

select * from stu where grades in (1,3);//查出括号列出的取值

select * from stu where mathe between 60 and 80;//小值在前大值在后包含60,80, 对一个字段,字段是数值类型的

习题6:到s_emp表中,查找出工资大于1000并小于1500的员工?


5.null, 值未知(还没有赋值), 注意:空格不是null

select * from 表名 where 字段名 is null; //is not null

select * from stu where first_name is not null;
select * from stu where first_name is null;

习题7:到s_emp表中,查找出comments为空的记录?

六、排序
select * from 表名 where 条件表达式 order by 排序字段名1,排序字段名2,... asc/desc

--ORDER BY子句,在整个SELECT语句中始终位于最后,后面可以是列名、列的别名、表达式、顺序号
--ORDER BY后面可以跟多列,表示先按第一列排序,如果第一列值相同再按第二列排序,如果第二列值也相同,则按第三列排序,依次类推
--ASC表示升序(可省略),DESC表示降序,空值是最大的

select * from 表名 order by 列数 asc/desc
select * from stu order by 字段名1 asc,字段名2 desc,... ;
select * from 表名 whele 条件表达式 order by 字段名 asc/desc

select * from stu order by age;
select * from stu order by 7 desc;
select * from stu order by mathe, chinese desc ;//当第一个字段相同时,按第二个字段排

select * from stu where sex='女' order by mathe desc;

习题8:s_emp表,查找出员工的id、last_name、manager_id、年薪,请按照员工的manager_id升序排列,年薪降序排列

 

七、聚合函数:from的前面, select 后面, 数值类型, 字段只能有一个

count(), max(), min(),sum(), avg()

 

1.查表的所有记录个数 count(字段名)或count(*)

select count(sex) 总数 from stu;
select count(*) 总数 from stu;
select count(*) "1班人数" from stu where grades=1;


2.max(数值类型字段),min(数值类型字段)
select max(mathe) from stu where sex='男';
select min(mathe) , max(mathe)from stu where sex='男';

3.sum(数值类型字段), avg(数值类型字段)
select sum(mathe) from stu;
select sum(mathe)/count(*) from stu;
select avg(mathe) from stu;

习题8:到s_emp表中,查找出最高的工资与最小的工资,员工的工资总和与平均工资, 并求出员工总数?

八、rownum:查询结果中每一行的编号,
select from 的中间,
where :
1. = 1
2. <= 数值
3. < 数值

select rownum, last_name, id from stu where age=20 and rownum<=2 ;
select * from stu where age=20 and rownum =1;
select * from stu where age =20 and rownum<=2;

select * from stu where rownum<5;
select * from stu where age=20 and rownum>0;

显示语文成绩的前3名
select * from (select * from stu order by chinese desc) where rownum<=3;

思考:
习题9:到s_emp表中,查找出工资在3到5名的员工?

posted @ 2019-10-05 16:06  传道授业  阅读(164)  评论(0)    收藏  举报