百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

常用sql 语句

toyiye 2024-06-21 12:35 8 浏览 0 评论

三张表 学生信息、 课程表、分数表

创表语句

-- 学生信息

create table t0_student

(

studentid varchar(10),

classid varchar(10),

name varchar(20),

sex varchar(1)

);

comment on column t0_student.studentid is '学生编码';

comment on column t0_student.classid is '学生班级 1 班级1 ,2 班级2';

comment on column t0_student.name is '学生姓名';

comment on column t0_student.sex is '学生性别,1 男,2 女';

alter table t0_student add constraint pk_t0_student primary key (studentid,classid)

-- 课程表

create table to_course

(

courseid varchar(10),

coursename varchar(10)

);

comment on column to_course.courseid is '课程编码,1,2,3,4,5';

comment on column to_course.coursename is '课程名称 1英语,2语文,3数学,4物理,5化学';

alter table to_course add constraint pk_to_course primary key (courseid);

-- 分数表

create table to_score

(

courseid varchar(10),

classid varchar(10),

studentid varchar(10),

score number

);

comment on column to_score.courseid is '课程编码';

comment on column to_score.classid is '班级编码';

comment on column to_score.studentid is '学生编码';

comment on column to_score.score is '分数';

alter table to_score add constraint pk_to_score primary key (courseid,classid,studentid);

insert into t0_student values('1','1','张三','1');

insert into t0_student values('2','1','李四','2');

insert into t0_student values('3','1','王五','1');

insert into t0_student values('4','1','赵六','2');

insert into t0_student values('5','2','张三','1');

insert into t0_student values('6','2','李四','2');

insert into t0_student values('7','2','王五','1');

insert into t0_student values('8','2','赵六','2');

insert into t0_student values('9','1','jackMa','1');

insert into to_course values('1','英语');

insert into to_course values('2','语文');

insert into to_course values('3','数学');

insert into to_course values('4','物理');

insert into to_course values('5','化学');

insert into to_score values('1','1','1',50);

insert into to_score values('1','2','1',70);

insert into to_score values('2','1','2',100);

insert into to_score values('2','2','2',100);

insert into to_score values('3','1','3',90);

insert into to_score values('3','2','1',70);

insert into to_score values('4','1','4',80);

insert into to_score values('4','2','1',60);

insert into to_score values('5','1','4',90);

insert into to_score values('4','2','5',80);

insert into to_score values('3','2','5',80);

insert into to_score values('2','2','5',80);

insert into to_score values('1','2','5',80);

insert into to_score values('5','2','6',75);

insert into to_score values('4','2','6',60);

insert into to_score values('3','2','6',60);

insert into to_score values('2','2','6',60);

insert into to_score values('1','2','6',90);

/****************************************************************************sql 语句 ****************************************************************************/

--- 2班张三分数列表

select student.name, decode(student.sex,'1','男','2','女') sex,course.coursename,score.score

from to_score score, t0_student student, to_course course

where student.studentid='5' and student.classid='2'

and score.courseid = course.courseid

and student.studentid = score.studentid

and student.classid = score.classid

--- 2班 成绩行转列方式展示

with t as(

select student.name, decode(student.sex,'1','男','2','女') sex,course.coursename,score.score

from to_score score, t0_student student, to_course course

where score.courseid = course.courseid

and student.studentid = score.studentid

and student.classid = score.classid and student.classid='2' )

select * from(select * from t

)

pivot (sum(score) for coursename in ('英语','语文','数学','物理','化学'));

--- 查询分数大于80分 的人数

select count(distinct studentid) from to_score

where score>80

--查询有两科成绩大于 80分记录数

select studentid,score,count(*) from to_score

where score>=80

group by studentid,score

having count(*)>2

--- 查询所有的英语成绩 按成绩 由高往低排序

select * from to_score where courseid ='1' order by score desc

-- 查询 所有后英语成绩的总分,最高分,最低分,平均分(保留两位小数)

select sum(score) 总分,max(score) 最高分,min(score) 最低分,round(sum(score)/count(*),2) 平均分 from to_score where courseid ='1'

-- 查询所有成绩 优秀人数 (>=90),良好人数(80-90),及格人数(60-80), 不及格人数(<60)

select suM(case

when score>=90 then 1

else 0 end ) 优秀人数,

suM( case

when score >=80 and score<90 then 1

when score >=60 and score<80 then 1

else 0 end) 良好人数,

suM(case

when score >=60 and score<80 then 1

else 0 end) 及格人数,

suM( case

when score <60 then 1

else 0 end) 不及格人数

from to_score

---查询90分以上各分数人数。rollup 统计总人次

select score ,count(*) from to_score where score >=90 group by rollup(score)

--- 删除名字重复的 学生 (如果没有studentid 这样的主键列的时候 可以通过rownum 增加一个虚拟列然后再删除)

delete t0_student

where studentid in(select studentid from (

select max(studentid) studentid ,name from t0_student group by name ))

--查询为非汉字的学生名字

select * from t0_student where asciistr(name) not like '%\%';

--随机抽取成绩中10% 数据

select * from to_score sample block(10);

相关推荐

Asterisk-ARI对通道中的DTMF事件处理

Asterisk通道中关于DTMF处理是一个非常重要的功能。通过DTMF可以实现很多的业务处理。现在我们介绍一下关于ARI对通道中的DTMF处理,我们通过自动话务员实例来说明Asterisk如何创建一...

PyQt5 初次使用(pyqt5下载官网)

本篇文章默认已安装Python3,本篇文章默认使用虚拟环境。安装pipinstallPyQt5PyQt一些图形界面开发工具QtDesigner、国际化翻译工具Liguist需要另外...

Qt开发,使用Qt for Python还是Qt C++ Qt开发,使用Qt for

Qt开发使用QtforPython还是QtC++?1.早些年写过一个PyQt5的项目,最近几年重构成QtC++了,其中有个人原因,如早期代码写得烂,...

最简单方法!!用python生成动态条形图

最近非常流行动态条形图,在B站等视频网站上,此类视频经常会有上百万的播放量,今天我们通过第三方库:bar_chart_race(0.2版本)来实现动态条形图的生成;生成的效果如图:问题:...

Asterisk通道和ARI接口的通信(aau通道数)

Asterisk通道和ARI详解什么是通道Asterisk中,通道是介于终端和Asterisk自己本身的一个通信媒介。它包含了所有相关信息传递到终端,或者从终端传递到Asterisk服务器端。这些信...

Python GUI-长链转短链(长链接转化成短链接java)

当我们要分享某一个链接给别人,或是要把某个链接放入帖子中时,如果链接太长,则会占用大量空间,而且很不美观。这时候,我们可以结束长链转短链工具进行转换。当然可以直接搜索在线的网站进行转换,但我们可以借此...

Python 的hash 函数(python的hash函数)

今天在看python的hash函数源码的时候,发现针对不同的数据类型python实现了不同的hash函数,今天简单介绍源码中提到的hash函数。(https://github.com/pyth...

8款Python GUI开源框架,谁才是你的菜?

作为Python开发者,你迟早都会用到图形用户界面来开发应用。本文千锋武汉Python培训小编将推荐一些PythonGUI框架,希望对你有所帮助。1、Python的UI开发工具包Kivy...

python适合开发桌面软件吗?(python可不可以开发桌面应用软件)

其实Python/Java/PHP都不适合用来做桌面开发,Java还是有几个比较成熟的产品的,比如大名鼎鼎的Java集成开发环境IntelliJIDEA、Eclipse就是用Java开发的,不过PH...

CryptoChat:一款功能强大的纯Python消息加密安全传输工具

关于CryptoChatCryptoChat是一款功能强大的纯Python消息加密安全传输工具,该工具专为安全研究专家、渗透测试人员和红蓝队专家设计,该工具可以完全保证数据传输中的隐私安全。该工具建立...

为什么都说Python简单,但我觉得难?

Python普遍被大家认为是编程语言中比较简单的一种,但有一位电子信息的学生说自己已经学了C语言,但仍然觉得Python挺难的,感觉有很多疑问,像迭代器、装饰器什么的……所以他提出疑问:Python真...

蓝牙电话-关联FreeSwitch中继SIP账号通过Rest接口

蓝牙电话-关联FreeSwitch中继SIP账号通过Rest接口前言上一篇章《蓝牙电话-与FreeSwitch服务器和UA坐席的通话.docx》中,我们使用开源的B2B-UA当中经典的FreeSWIT...

技术分享|Sip与WebRTC互通-SRProxy开源库讲解

SRProxy介绍目前WebRTC协议跟SIP协议互通场景主要运用在企业呼叫中心、企业内部通信、电话会议(PSTN)、智能门禁等场景,要想让WebRTC与SIP互通,要解决两个层面的...

全网第N篇SIP协议之GB28181注册 JAVA版本

鉴于网上大部分关于SIP注册服务器编写都是C/C++/python,故开此贴,JAVA实现也贴出分享GB28181定义了了基于SIP架构的视频监控互联规范,而对于多数私有协议实现的监控系统...

「linux专栏」top命令用法详解,再也不怕看不懂top了

在linux系统中,我们经常使用到的一个命令就是top,它主要是用来显示系统运行中所有的进程和进程对应资源的使用等信息,所有的用户都可以使用top命令。top命令内容量丰富,可令使用者头疼的是无法全部...

取消回复欢迎 发表评论:

请填写验证码