热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

一学期积累下来的SQL语句写法的学习

整合了一下上学期学习的积累,希望可以帮到初学者!可能以后会有用吧!A基本语句的运用操作基于emp表1、按工资从高到低排列SQL>selectrownumas次序,ename,sal

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

可能以后会有用吧!

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.score11、查询表中课程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.score12、查询平均成绩大于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);


推荐阅读
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 摘要: 在测试数据中,生成中文姓名是一个常见的需求。本文介绍了使用C#编写的随机生成中文姓名的方法,并分享了相关代码。作者欢迎读者提出意见和建议。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
author-avatar
zhousulian
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有