整合了一下上学期学习的积累,希望可以帮到初学者!

可能以后会有用吧!

A 基本语句的运用

操作基于emp表
1、按工资从高到低排列
SQL> select rownum as 次序,ename,sal
  2  from (select ename,sal
  3  from emp
  4  order by sal desc)
  5  where rownum<=5
  6  ;
2、做内嵌式图由大到小排序后找前五个的错误写法

SQL> select ename,sal
  2  from emp
  3  where rownum<=5
  4  order by sal desc;
先找前五个记录后按照工资排序(并不是所求,这是错误的写法)

3、把名为scott的job 奖金更改
update emp set job='MANAGER',comm='4000'
 where ename='SCOTT';
4、寻找薪水大于scott或hiredate早于scott的
select ename,empno from emp
where sal>(select sal from emp where ename='SCOTT')
or hiredate>(select hiredate from emp where ename='SCOTT');

5、寻找最大
 select ename,sal from emp
 where sal=(select max(sal) from emp);

6、寻找部门人员小于4 的部门
 select avg(sal) from emp
 where deptno=
 (select deptno from emp
 group by deptno having count(*)<4);

7、一些设置参考
set linesize 180                设置每行显示的字符总数
set pagesize 100             设置每页显示的行数
set feedback on/off          设置是否显示已选择XX行
set heading on/off            设置是否显示列名
set time on/off                   设置是否显示当前系统时间
set timing on/off                设置是否显示每条SQL执行消耗的时间
set termout on/off             设置在执行sql文件时是否在控制台打印相关信息
set trimout on/off              设置是否去除标准输出每行的拖尾空格
set trimspool on/off         设置是否去除spool输出文件中每行的拖尾空格

8、按照不同的维度分组 多维的数据统计
select avg(grade),stu_no,cno
from mark2
group by rollup(cno,stu_no);
先以cno分组,再以stu_no排序
计算一组cno的平均值

9、cube 把group by的数据各维进行组合
 select avg(grade),stu_no,cno
 from mark2
 group by cube(stu_no,cno);

AVG(GRADE) STU_NO     CNO
---------- ---------- ----------
78.8888889
        90            1
83.3333333            2
63.3333333            3
73.3333333 1404010525
        90 1404010525 1
        80 1404010525 2
        50 1404010525 3
83.3333333 1404010526
       100 1404010526 1
        80 1404010526 2
        70 1404010526 3
        80 1404010527
        80 1404010527 1
        90 1404010527 2
        70 1404010527 3
先计算按照cno分组的平均值
再按照(stu_no,cno)分组计算平均值

10、查询一个表中 课程1比课程2分数高的学生号
 select a.num,a.score
 as score1,b.score
 as score2 from(
 select * from sc where cno='001')a
 left join
 (select * from sc where cno='002')b
 on a.num=b.num
 where a.score<b.score;
11、查询表中课程1 比课程2高的学生信息:返回多值?用in!
select num,name
from stu
where num in
(select a.num from(
 select * from sc where cno='001')a
 left join
 (select * from sc where cno='002')b
 on a.num=b.num
 where a.score<b.score);
12、查询平均成绩大于60的学生学号、平均成绩、姓名
1\查询平均成绩大于90的学生学号: select num,avg(score) from sc group by num having avg(score)>90
2\ select num,name
 from stu
 where num in(
  select num from sc group by num having avg(score)>90);(平均成绩怎么办)?

3\select num,avg(score)
from sc
group by num having avg(score)>90;(单查学号和平均成绩)
最后写法:
select stu.num,stu.name,avg(sc.score)
from stu,sc
where stu.num in(
select num from sc group by (sc.num,name) having avg(score)>90)
and stu.num=sc.num ;

13、查询所有同学的学号、姓名、选课数、总成绩
select stu.num,stu.name,count(sc.cno),sum(sc.score)
 from stu,course,sc
 where stu.num=sc.num and course.cno=sc.cno
 group by stu.num,stu.name order by num;

14、查询所有姓李的老师
select * from teacher
where teacher.name like '李%';

15、查询没选过chinese课程的学生的学号与姓名
select stu.num,stu.name
from stu
where stu.num not in
(select distinct stu.num
from stu,course,sc
where stu.num=sc.num and course.cno=sc.cno
and course.name='chinese');
16、查询选过maths课程的所有学生学号与姓名
select stu.num,stu.name
from stu
where stu.num in
(select distinct stu.num
from stu,course,sc
where stu.num=sc.num and course.cno=sc.cno
and course.name='maths');

17、查询同时选过maths和chinese课程的所有学生学号与姓名
 select * from stu
 where stu.num in
 (select num from sc,course where sc.cno=course.cno and course.name='maths')
 and stu.num in
 (select num from sc,course where sc.cno=course.cno and course.name='chinese');

18、查询所有课程小于90分的学生姓名与学号。
 select * from stu
 where stu.num not in
 (select num from sc where score>=90);

B PL/SQL语句
1、用table类型输出一些语句
 declare
 type my_table_type is table of varchar(20)
 index by binary_integer;
 my_table my_table_type;
 begin
 my_table(1):='what the fuck';
 my_table(2):='hehe is a gay';
 my_table(3):='what the hell';
 my_table(-100):='is it a fky';
 dbms_output.put_line(my_table(1));
  dbms_output.put_line(my_table(2));
  dbms_output.put_line(my_table(3));
  dbms_output.put_line(my_table(-100));
  dbms_output.put_line(my_table.count);
 end;
 /
2、select语句的应用 :注意,查询结果只能是唯一的一条记录
SQL>  declare
  2   v_dname student.name%type;
  3   v_dept_rec student%rowtype;
  4   begin
  5   select name into v_dname from student where num='02';
  6  dbms_output.put_line('name of student 02 is:'||v_dname);
  7  end;
  8  /
name of student 02 is:wangwu
3、游标:是一个缓存区,在内存里临时存放多条记录的存储区。
打开游标 取数据 关闭游标
4、存储过程
查询指定号码的姓名:
create or replace procedure p1
as
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=7788;
dbms_output.put_line('7788员工姓名为'||v_ename);
end;
/

查询指定编号员工的姓名

create or replace procedure p1(p_no in emp.empno%type)
as
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=p_no;
dbms_output.put_line(p_no||'员工姓名为'||v_ename);
exception
when no_data_found then
dbms_output.put_line('没有这个编号');
when others then
dbms_output.put_line('系统有错');
end;
/

EMPNO ENAME      JOB              MGR HIREDATE              SAL        COMM DEPTNO

C sql语句练习

1、 列出所有员工的姓名及其直接上司的姓名;
select a.ename empname,b.ename mgrname
from emp a,emp b
where a.mgr=b.empno;

2、 列出在“IT”部门工作的员工姓名;
select ename from emp
 where deptno=
 (select deptno from dept
 where dname='IT');

3、 列出工资高于公司平均工资的所有雇员的姓名、编号及部门号;
select ename,deptno,empno from emp
 where sal>(select avg(sal) from emp);

4、 列出在每个部门的员工数量、平均工资;
select avg(sal),count(*)
 from emp
 group by deptno ;

5、列出所有部门的详细信息和部门人数;
select * from dept a left join
 (select count(*),deptno from emp
 group by deptno)
 b
 on
 a.deptno=b.deptno;

6、列出各种职位的最低工资;
select job,min(sal)
from emp
group by job;

7、 列出部门经理中工资最低的那个经理的姓名、工资和部门号;
 select ename,sal,deptno
 from emp
 where sal<=all(
 select sal from emp where job='MANAGER'
 ) and job='MANAGER';

8、 列出公司里工资最高的五位员工的基本信息。
select * from emp
where ename in(
select ename from (
select ename from emp
order by sal desc) a
where rownum <6);

一学期积累下来的SQL语句写法的学习的更多相关文章

  1. mysql中获取一天、一周、一月时间数据的各种sql语句写法

    今天抽时间整理了一篇mysql中与天.周.月有关的时间数据的sql语句的各种写法,部分是收集资料,全部手工整理,自己学习的同时,分享给大家,并首先默认创建一个表.插入2条数据,便于部分数据的测试,其中 ...

  2. mysql where 条件中的字段有NULL值时的sql语句写法

    比如你有一个sql语句联表出来之后是这样的 id           name            phone                  status 1            张三     ...

  3. 匹配不含有某个信息的sql语句写法

    SELECT id,order_id,flight_info FROM order_flights WHERE mark=0 AND flight_info REGEXP '[^() DAY)]' O ...

  4. 【sql语句】好用的sql语句之项目数据库学习总结

    转载请注明出处:http://blog.csdn.net/pearyangyang/article/details/41115491 这几天学习公司系统的数据流向.主要涉及到几个表的数据. 可是表中的 ...

  5. MySQL数据库实现分页查询的SQL语句写法!

    一:分页需求: 客户端通过传递start(页码),limit(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道MySql数据库提供了分页的函数limit m,n,但是该函数的用法和我们的 ...

  6. 动态语句SQL语句写法

    /*************************************************************************************************** ...

  7. MySql 建表、添加字段、修改字段、添加索引SQL语句写法及SQL索引

    ---------添加索引方法--------- .添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `c ...

  8. 只显示前几条数据的sql语句写法 七种数据库中Select Top的使用方法

    七种数据库中Select Top的使用方法 1. Oracle数据库 SELECT * FROM TABLENAME WHERE ROWNUM <= N 2. Infomix数据库 SELECT ...

  9. SQL 语句 写法

    SELECT * FROM article where  userid=4 order by sort asc LIMIT 0,10; 先根据写where 条件,再排序,在LIMIT.

随机推荐

  1. c语言:简单排序:冒泡排序法、选择排序法、插入排序法(待写)

    1.冒泡排序法: 假设有n个数需要按从小到大排序,冒泡排序的原理是,在这一排数字中,将第一个数与第二个数比较大小,如果后面的比前面的小,就将他们交换位置.然后再比较第二个和第三个,再交换,直到第n-1 ...

  2. Android 百度云推送

    http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/sdk/clientsdk. public class DemoAppl ...

  3. android 屏幕旋转 不重新加载oncreate

    当手机设定了使用横屏或者竖屏的时候,还想要使用重力感应,可以设置activity属性 android:screenOrientation="sensor" 但是每次翻转屏幕,都会重 ...

  4. 冲刺博客NO.6

    今天做了什么:通过自学已经掌握对多控件的使用并学会使用进度条 遇到的困难:Handler使用错误,对其理解错误,导致很多误会后来百度发现 Handler是Android系统消息机制抽象出来的一个类(并 ...

  5. spring security文档地址

    https://docs.spring.io/spring-security/site/docs/4.1.0.RELEASE/reference/htmlsingle/

  6. Python自动化开发 - 常用模块(二)

    本节内容 1.shutil模块 2.shelve模块 3.xml处理模块 4.configparser模块 5.hashlib模块 6.subprocess模块 7.re模块 一.shutil模块 高 ...

  7. Python自动化开发 - 模块与包

    本节内容 一.模块  1.import module 2.from module  import 3.from module  import * 4.模块的__name__属性 5.模块搜索路径 6. ...

  8. Tencent interview

    1.常见的聚类算法 1):划分法:k-means 2):基于密度的方法: 2.EM 算法 EM算法是在概率模型中寻找参数的最大似然估计或者最大后验概率的算法,其中概率模型依赖于无法观测的隐藏变量.EM ...

  9. windows 2008远程桌面企业协议号

    windows 2008远程桌面企业协议号 6565792 (2015-01-07 14:47:31) 转载▼ 标签: it 桌面 终端服务器 客户端 服务器 分类: 操作系统/办公软件 微软的终端服 ...

  10. 把EXE可执行文件等作为资源包含在Delphi编译文件中

    摘自我自己过去写的一段心得. 1.编辑资源文件 *.RCWave: 资源文件是声音文件:RCDATA: 二进制数据AVI: AVI动画:ICON: 图标文件:BITMAP: 位图文件:CURSOR: ...