1.定义表的结构:

sql>

create  table stu (

student_id  number(5)

constrant  student_pk  primary  key,  --定义主键约束

moitor_id  number(5),

name  varchar2(10)  not null,

sex varchar2(6),

constraint  sex_chk  check(sex  in (‘男’,’女’)),

dob  Date,

specialty   varchar2(10)

);

插入数据:

insert into  stu  values('12','5','e’,’男’,’07-11月-2009,’mm’);

删除数据:

Delete  from  stu;

2.简单查询

1. 检索所有教师信息,日期格式按“YYYY-MM-DD”输出:

Select  name,title,wage,TO_CHAR(hire_date,’YYYY-MM-DD’)  from  teachers;

2. 检索学生表,显示学生专业列(带distinct关键字,消除重复行)

Select distinct  specialty  from  students;

3. 使用别名:

Select  name  as “姓名”,  dob as “生日”  from stu;

4. 字符串连接符“||”

Select  name || ‘生日是:’ || dob  as “学生清单” from stu;

结果为:

学生清单:

小小生日是:08-3月-89

5.使用算术表达式:

Select  name  AS “姓名” , bonus+wage  AS ”月总收入”

from teachers;

注意:bonus+wage指“奖金+工资”

3.空值处理:

1. 利用NVL()函数处理bonus出现空值情况:

Select   NVL(bonus,0)  as “奖金” from teachers;

注意:NVL(bonus,0)表示当bonus=null时,返回0,不为null时,返回bonus

2.利用NVL2()函数处理bonus出现空值情况:

Select   NVL2(A,B,C)  from  D;

注:当A=null时,返回C;

A!=null时,返回B

2. 利用COALESCE()函数处理bonus出现空值情况:

Select   COALESCE (A,B)  from  D;

A!=null 时,返回A

A=null,B!=null,返回B

A=B=null  返回null

3.条件查询

1.使用算术比较符()

检索工资多余或等于2000元的教师信息:

Select name from teacher where wage>=2000;

检索计算机专业学生信息:

Select * from students where specialty=’计算机’;

检索1990年1月1号以前出生学生信息:

Select  * from students where dob

2.使用包含测试:

检索teachers表中获得奖金为500或600元的老师信息:

Select  * from teachers where bonus in (500,600);

检索students表中在1989年12月26日及1990年8月10日出生的学生信息:

Select * from where dob in(’08-10月-1990’,’26-12月-1989’);

时间格式:

select to_char(sysdate,'yy-mm-dd') from dual;

select to_char(sysdate,'dd-mon-yy') from dual;

检索teachers表中获得奖金为500-- 600元之间的老师信息:

Select  * from teachers where bonus  between 500 and 600;

3.使用匹配测试(Like)

检索students表中所有姓袁的学生信息:

Select * from students where name Like ‘袁%’;

4.空值测试:

表students中bonus为空:

Select * from students where bonus is null;

4.复合条件查询:

1.使用and,or,not

检索计算机专业男生信息:

Select * from students where specialty=’ 计算机’ and sex=’男’;

检索计算机专业和自动化专业学生信息:

Select * from students where specialty=’ 计算机’ or specialty=’自动化’;

检索不是计算机专业男生信息:

Select * from students where  not specialty=’ 计算机’ ;

检索students表中不姓袁的学生信息:

Select * from students where name not like ‘袁%’;

除姓名为:A和B以外的的学生信息:

Select * from students where name not in(‘A’,’B’);

检索students表中在1989年12月26日和1990年8月10日之间出生的学生信息:

Select * from where dob between ’08-10月-1990’ and ’26-12月-1989’;

2.组合使用逻辑条件:

检索计算机专业女生和机电专业男生的学生信息:

Select * from students  where specialty=’计算机’ and sex=’女’

or specialty=’机电’ and sex=’男’;

4. 检索teachers表中不是工程师,并且2002年1月1日前参加工作,工资低于3000元的教师信息:

Select * from teachers  where not  title=’工程师’

and hire_date

5.记录排序:

单一排序:

1.升序:

Select  *  from  teachers  order  by  id  ASC;

2.降序:

Select  id  as”ID号”  from  teachers  order  by  “ID号”  DESC;

按多列排序:

--按姓名降序,id升序排序:

Select  id ,name  from  teachers  order  by id  DESC,name  asc;

6.分组查询:

1.函数AVG(),COUNT(),MAX(),MIN(),SUM()的使用:

Select  avg(wage)  from teachers;              --教师的平均工资

Select  sum(wage)  from teachers;              --教师的总工资

Select  count(*)  from teachers ;                   --教师总数

Select  max(dob), min(dob) from teachers;           --最大及最小的出生日期

Select  stddev(wage) from teachers;         --教师的工资标准偏差

2.HAVING子句(必须与GROUP BY子句一起使用):

Select department_id,AVG(wage)  from teachers

group by department_id  HAVING(WAGE)>2200;

3. 工资高于2000,低于3000的教师,并按工资排序:

Select  id,  wage  from teachers  where  wage<3000  GROUP BY  id  HAVING  wage>=2000  order by 2;

7.子查询:

1.单行子查询(where)

查询工资低于平均工资:

Select * from  teachers where wage

from teachers);

2.查询与小笨同专业的所有学生

Select * from students where specialty = (select specialty from students  where  name=’小笨’)

4. Having 子句使用子查询:

在teachers表中查询部门平均工资高于最低部门平均工资的部门和平均工资

Select  department_id, avg(wage) as 平均工资from teachers  group by department_id  having  avg(wage) >

(select min( avg(wage) ) from teachers group by department_id );

5. 在from子句中使用子查询:

在students表中的男同学中,查询计算机专业的学生:

Select  * from  (select * from students where sex=’男’)  where

Specialty=’计算机’;

6.多行子查询:

利用IN或not in操作符,查询students表中姓王同学信息:

Select * from  students  where  student_id  in

(select student_id from students where  name  like ‘王%’);

查询未被学生选修的课程:

Select  *  from  courses

Where   courses_id   not  in   (select course_id  from  students_grade);

使用any操作符:

查询工资低于如何一个部门平均工资的教师信息:

Select * from teachers where  wage

使用all操作符:

查询工资多余各部门平均工资的教师信息:

Select * from teachers where wage > all(select  avg(wage) from teachers  group by department_id);

7.多列子查询:

1.和小笨同专业,同生日的学生信息:

Select * from students where (specialty,dob)=(select specialty , dob  from students where name=’小笨’);

2.在teachers表中查询在各部门工资最低的教师:

Select * from teachers where (department_id,wage) in

(select department_id,min(wage) from teachers

group by department_id);

8.相关子查询:

1. 使用exists

在courses表查询已经被选修的课程:

Select course_id,course_name from courses c

Where exists (select 2 from students_grade  sg

Where  sg.courses_id  =  c.courses_id);

2. 使用  not  exists

在courses表查未被选修的课程:

Select course_id,course_name from courses c

Where  not  exists (select 2 from students_grade  sg

Where  sg.courses_id  =  c.courses_id);

3. 使用  in

在departments表中查已经安排的教师的系部:

Select department_id,department_name from departments

Where department_id  IN

(select  department_id  from  teachers);

4. 使用 not  in

在departments表中查没有安排的教师的系部:

Select department_id,department_name from departments

Where department_id   NOT  IN

(select  department_id  from  teachers);

9.嵌套子查询:

Students表中查询与小笨同专业的学生的姓名,性别:

Select name , sex  from (

Select  * from  students where specialty=

(select specialty  from students  where  name=’小笨’)

);

8.聚合操作:

1.使用union all求并集(不取消重复行,而且不会对结果进行排序):

Select course_id ,course_name,credit_hour  from  courses

Union  all

Select course_id ,course_name,credit_hour  from  minors;

2.使用union求并集(自动取消重复行,而且对结果进行排序):

Select course_id ,course_name,credit_hour  from  courses

union

Select course_id ,course_name,credit_hour  from  minors;

3.使用聚合操作符intersect:(取交集,对交集的第一列对交集

进行排序)

Select course_id ,course_name,credit_hour  from  courses

intersect

Select course_id ,course_name,credit_hour  from  minors;

4.使用聚合操作符minus:(取差集(即在第一个集中,不在第二个集中),对差集的第一列对差集进行排序)

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors;

5. 组合使用聚合操作:

Courses表与minors表进行intersect操作,然后与courses2进行union操作:

(Select course_id ,course_name,credit_hour  from  courses

intersect

Select course_id ,course_name,credit_hour  from  minors)

Union

Select course_id ,course_name,credit_hour  from  courses2;

9.聚合操作进一步讨论:

1.使用order by子句:

使用列名作为排序表达式:

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order by course_name;

使用别名作为排序表达式:

Select course_id ,course_name  as name ,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order by  name;

使用位置编号作为排序表达式:

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order   by  2;  --即按course_name排序;

2.聚合操作中的数据类型:

--将name,dob均改为文本型数据:

Select  to_char(name) as 姓名, to_char(dob,’YYY-MM-DD’)

as  出生年月from students;

SQL语句:

1. select sex as 性别,count(*)as人数from tb_studentinfo group by sex

2.select sex as 性别,count(*)as人数,avg (age )as平均年龄from tb_studentinfo group by sex

3.SQL注释语句:

“- -”,“/*….*/”

4.insert into tb_studentinfo values('45','yuan','boy','22','','','')

5.delete  from tb_studentinfo where studentid='45'   --注意:delete后没有“*”号

6.update tb_studentinfo set sex='男' where studentid='002'    --tb_studentinfo是表名

oracle查询姓王的学生,Oracle之SQL 和 PL/SQL编程指南相关推荐

  1. 数据库作业(1.查询姓‘’王‘’的学生个数;2.查询“数学”比“语文”成绩高的所有学生的学号;)

    SQL查询姓''王''的学生个数 #一.创建学生.班级.成绩表 #二.打开mysql 1.快捷的win+r,输入cmd,打开命令提示符: 2.连接数据库,在cmd种输入:MySQL -u root - ...

  2. mysql 查询姓王_mysql常用数据操作之查

    将数据库查询单独开来是因为使用最多,也是技巧最多,同时面试最容易问的部分 简单查询 select * from table_name; select name,age from table_name; ...

  3. oracle 的遍历语法,oracle pl/sql之pl/sql语法

    一.pl/sql基础 pl/sql分匿名块和命名块. 命名块:存储过程,函数,触发器,包等. pl/sql语句块分3部分: (1)声明部分 (2)可执行部分 (3)异常处理部分 其中可执行部分是语句块 ...

  4. 如何在Oracle数据库内格式化SQL或PL/SQL ?

    点击上方"蓝字" 关注我们,享更多干货! 在一些情况下,梳理复杂的SQL或PL/SQL代码逻辑时最好是格式化一下文本. 当然我们有Toad或PL/SQL Developer等其它第 ...

  5. mysql查询姓王的信息代码_MySQL查询语句练习题

    1.创建student和score表 CREATE  TABLE  student ( id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  , name  VARC ...

  6. mysql 查询姓王_MySQL查询语句练习题,测试足够用了

    MySQL查询语句练习题,测试足够用了 博客分类: http://blog.sina.com.cn/s/blog_767d65530101861c.html 1.创建student和score表 CR ...

  7. mysql 查询姓王_MySQL查询语句

    来源于网络... Sutdent表的定义 字段名 字段描述 数据类型 主键 外键 非空 唯一 自增 Id 学号 INT(10) 是 否 是 是 是 Name 姓名 VARCHAR(20) 否 否 是 ...

  8. java oracle查询结果list取数,Oracle函数返回Table集合

    Oracle table()函数查询函数返回的结果集 2015年12月13日 22:42:51 warrenjiang 阅读数:7452 版权声明:本文为博主原创文章,未经博主允许不得转载. http ...

  9. oracle查询cpu占用率高,ORACLE杀掉cpu占用率高的session

    SPID一一system process id,表示该server process在OS层面的Porcess ID PID一一Oracle process id,可以理解Oracle给自己的进程的一个 ...

最新文章

  1. 吃了这些数据集和模型,跟 AI 学跳舞,做 TensorFlowBoys
  2. 二,zabbix与php的一些问题
  3. 2020 我的C++学习之路 C++PrimerPlus第八章课后习题
  4. IIS7中WCF配置问题集合
  5. 第十章:在Spark集群上掌握比较重要的图操作之Computing Degree
  6. 三星发布110寸大屏MicroLED面板电视
  7. SPS:设置访问群体
  8. web.xml配置以及一些详解
  9. 小米路由器3无线网连接到服务器,小米路由器3设置完成后,手机能用,电脑没法上网...
  10. 蓝屏代码——STOP:c000021a Unknown Hard Error
  11. Arduino基础应用学习
  12. hp linux 禁用u盘启动不了,u盘启动禁用旧版启动模式并启用UEFI
  13. 7-62 贴“福”字
  14. 重载和重写的区别???
  15. Word生成图表(柱状图、线形图等,并附带表格展示数值)
  16. RISC-V应用于高性能处理器的可能性
  17. Guava系列:Shorts、Doubles、Chars、Floats、Ints、Longs、Bytes使用方法
  18. 【MIRACL】 用户手册研究学习 chapter1
  19. 一文解决浏览器跨域问题
  20. 用dreamweaver打开asp网页出现乱码怎么办

热门文章

  1. 如何快速删除 .txt 中上万行(不方便快速拖动的情况下)
  2. docker无法拉取harbor镜像
  3. 【PADS_001】【报错“没有可用的未使用开孔”】
  4. 16进制转10进制,以及二进制负数的补码
  5. 悬镜安全:用开源的方式做开源风险治理
  6. mysql数据库数据类型money_常用数据库基本数据类型
  7. java问题的英语对话_十二句你应该掌握的英语口语表达,让你不再和老外尬聊,建议收藏...
  8. Windows系统下的TCP参数优化(注册表\TCPIP\Parameters)
  9. mysql 关系图 工具_DbSchema(数据库关系图设计器) V8.1.7 官方版
  10. 82、基于STM32单片机电动车充电桩 MLX90614温度检测自动灭火设计