day05_日常SQL练习(一)

查询score表中成绩在60到80之间的所有记录

SELECT * FROM score WHERE grate BETWEEN 60 and 80

查询student表中所有姓王的学生记录

SELECT * FROM student WHERE sname LIKE ‘王%‘

查询score表中成绩为85,86或88的记录

SELECT * FROM score WHERE grate IN (85,86,88)

查询student表中“95931”班或性别为“女”的学生记录

SELECT sno,sname,class FROM student WHERE class=‘95931‘ or ssex=‘女‘

查询student表的所有男生记录,并以class降序排列

SELECT sno,sname,class FROM student WHERE ssex=‘男‘ ORDER BY class desc

查询以cno升序、degree降序显示score表所有记录

SELECT * FROM score ORDER BY cno,degree desc

查询‘95931‘班的学生人数

SELECT COUNT(*) as 人数 FROM student WHERE class=‘95931‘

查询score表中"3-105"号课程的平均分

SELECT AVG(degree) as ‘课程平均分‘ FROM score WHERE cno=‘3-105‘

查询score表中多于5名学生选修并以3开头的课程号的平均分数

SELECT cno,AVG(degree) as ‘平均成绩‘ FROM score WHERE cno LIKE ‘3%‘ GROUP BY cno HAVING COUNT(*)>5

查询score表中最低分大于70,最高分小于90的sno列

SELECT sno FROM score GROUP BY sno HAVING MIN(degree)>70 and MAX(degree)<90

查询score表中最高分与最低分之差大于12分的课程号

SELECT cno as ‘课程号‘,MAX(degree) as ‘最高分‘,MIN(degree) as ‘最低分‘ FROM score GROUP BY cno HAVING MAX(degree)-MIN(degree)>12

查询所有学生的sname、cname、degree例

SELECT s.sname,c.cname,sc.degree FROM student s,course c,score sc WHERE s.sno=sc.sno and sc.con=c.cno

查询student表中与学号“105”的学生同年出生的所有学生的sno、sname、sbirthday列

SELECT sno,sname,YEAR(sbirthday) as ‘出生年份‘ FROM student WHERE YEAR(sbirthday)=(SELECT YEAR(sbirthday) FROM student WHERE sno=‘105‘)

查询成绩最高分的学生姓名

SELECT sname FROM student WHERE sno=(SELECT sno FROM score WHERE degree=(SELECT MAX(degree) FROM score))

查询选修学生人数多于5人的某课程的任课老师姓名

SELECT tname FROM teacher WHERE tno IN(SELECT x.tno FROM course x,score y WHERE x.cno=y.cno GROUP BY x.tno HAVING COUNT(x.tno)>5)

查询成绩在85分以上的课程号cno

SELECT DISTINCT cno FROM score WHERE degree IN(SELECT degree FROM score WHERE degree>85)

查询“计算机系”与“电子工程系” 中不同职称的教师tname和prof

SELECT tname,prof FROM teacher WHERE depart=‘计算机系‘ and prof NOT IN(SELECT prof FROM teacher WHERE depart=‘电子工程系‘)

查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”课程的学生的cno,sno和degree,并按degree降序排列

SELECT cno,sno,degree FROM score WHERE cno=‘3-105‘ and degree > ANY(SELECT degree FROM score WHERE cno=‘3-245‘) ORDER BY degree desc

查询选修编号为“3-105”课程且成绩高于选修编号为“3-245”课程的学生的cno,sno和degree

SELECT cno,sno,degree FROM score WHERE cno=‘3-105‘ and degree > ALL(SELECT degree FROM score WHERE cno=‘3-245‘)

查询成绩比平均成绩低的学生成绩表

SELECT sno,cno,degree FROM score a WHERE degree<(SELECT AVG(degree) FROM score b WHERE a.cno=b.cno)

查询所有任课教师的tname和depart

SELECT tname,depart FROM teacher a WHERE EXISTS (SELECT * FROM course b WHERE a.tno=b.tno)

查询所有未任课教师的tname和depart

SELECT tname,depart FROM teacher a WHERE NOT EXISTS (SELECT * FROM course b WHERE a.tno=b.tno)

查询所有教师和学生的姓名、性别和出生年份

SELECT tname as ‘姓名‘,tsex as ‘性别‘,YEAR(tbirthday) as ‘出生年份‘ FROM teacher

UNION

SELECT tname as ‘姓名‘,tsex as ‘性别‘,YEAR(tbirthday) as ‘出生年份‘ FROM student

查询所有教师和学生的姓名、性别和出生年份,并按name排序

SELECT tname as ‘姓名‘,tsex as ‘性别‘,YEAR(tbirthday) as ‘出生年份‘ FROM teacher

UNION

SELECT tname as ‘姓名‘,tsex as ‘性别‘,YEAR(tbirthday) as ‘出生年份‘ FROM student

ORDER BY tname,sname

查询所有比“王华”年龄大的学生姓名、年龄和性别

SELECT sn,age,sex FROM s WHERE age>(SELECT age FROM s WHERE sn=‘王华‘)

查询选修课程“C2”的学生中成绩最高的学生的学号

SELECT s# FROM sc WHERE c#=‘c2‘ and grade >=ALL(SELECT grade FROM sc WHERE c#=‘c2‘)

查询学生姓名及其所选修课程的课程号和成绩

SELECT s.sn,sc.c#,sc.grade FROM s,sc WHERE s.s#=sc.s#

查询选修4门以上课程的学生总成绩(不统计不及格的课程),并要求按总成绩排序列出来

SELECT s#,SUM(grade) FROM sc WHERE grade >=60 GROUP BY s# HAVING COUNT(*)>=4 ORDER BY 2 DESC

假定学生关系是S(S#,SNAME,SEX,AGE),C(C#,CNAME,TEACHER),学生选课关系是SC(S#,C#,GRADE)。要查找选修"COMPUTER"课程的"女"学生姓名。

SELECT sname FROM s,c,sc WHERE sc.c#=c.c# AND c.cname="COMPUTER" AND s.s#=sc.s# AND s.sex=‘女‘

书店(书店编号,书店名,地址)     图书(书号,书名,定价)

图书馆(馆号,馆名,城市,电话)   图书发行(馆号,书号,书店号,数量)

(1)查询已发行的图书中最贵的最便宜的书名和定价

SELECT 图书.书名,图书.定价 FROM 图书 WHERE 定价=

(SELECT MAX(定价) FROM 图书,图书发行 WHERE 图书.书号=图书发行.书号)

UNION

SELECT 图书.书名,图书.定价 FROM 图书 WHERE 定价=

(SELECT MAX(定价) FROM 图书,图书发行 WHERE 图书.书号=图书发行.书号)

(2)查询拥有已发行的"数据库系统基础"一书图书馆馆名

SELECT 馆名 FROM 图书馆 WHERE 馆名 IN

(SELECT 馆名 FROM 图书发行 WHERE 书号 IN

(SELECT 书号 FROM 图书 WHERE 书名=‘数据库系统基础‘))

people(pno职工号,pname职工姓名,sex性别,job职业,wage工资,dptno部门号)

(1)查询工资比其所在部门平均工资高的所有职工信息

SELECT * FROM people x WHERE wage >(SELECT AVG(wage) FROM people y WHERE x.dptno=y.dptno)

(2)查询工资大于"赵明华"工资的所有职工信息

SELECT * FROM people WHERE wage > (SELECT wage FROM people WHERE pname="赵明华")

A(A#商店代号,ANAME商店名,WQTY店员人数,CITY所在城市)

B(B#商品号,BNAME商品名称,PRICE价格)

AB(A#商店代号,B#商品号,QTY商品数量)

(1)查询店员人数不超过100人或者在长沙市的所有商店的商店代号和商店名

SELECT A#,ANAME FROM A WHERE WQTY<=100 OR CITY=‘长沙‘

(2)查询供应书包的商店名(2种做法)

1、SELECT A.ANAME FROM A,B,AB WHERE A.A#=AB.A# AND B.B#=AB.B# AND B.BNAME=‘书包‘

2、SELECT ANAME FROM A WHERE A# IN

(SELECT A# FROM AB WHERE B# IN

(SELECT B# FROM B WHERE BNAME=‘书包‘))

(3)查询至少供应代号为256的商店所供应的全部商品的商店名和所在城市

SELECT A.ANAME,A.CITY FROM A,AB WHERE A.A#=AB.A# AND AB.B# IN

(SELECT AB.B# RFOM AB WHERE A#=‘256‘)

来自为知笔记(Wiz)

时间: 2024-10-24 21:36:45

day05_日常SQL练习(一)的相关文章

[Sqlite] 移动嵌入式数据库Sqlite的日常SQL操作语句汇总

序言:     嵌入式数据库Sqlite的基本sql使用汇总,使用测试起来,与关系型数据库mysql在语法上有很多的相似之处,先准备测试数据: CREATE TABLE COMPANY(ID INT NOT NULL, NAME VARCHAR(20),AGE INT,ADDRESS VARCHAR(20),SALARY DECIMAL(7,2)); INSERT INTO "COMPANY" VALUES(1,'Paul',32,'California',20000); INSERT

DBA日常SQL之查询数据库运行状况

SELECT SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'),1,5) Day, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'00',1,0)) H00, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'01',1,0)) H01, SUM(DECODE(SUBSTR(T

SQL中利用脚本恢复数据库

SQL中利用脚本恢复数据库   编写人:CC阿爸 2014-6-15 在日常SQL数据库的操作中,如何恢复数据库 use master drop database SCS1 RESTORE DATABASE SCS1 from disk='C:\Documents and Settings\kevinzhang\桌面\eee.bak' USE master RESTORE DATABASE SCS1 from disk='C:\Documents and Settings\kevinzhang\

SQL中如何检查死锁

编写人:CC阿爸 2014-6-15 在日常SQL数据库的操作中,SQL偶尔会出现表被死锁的问题.比如: 在执行事务时,突然中止事务.系统肯定会锁表. 大批量数据操作时,由于网络 机器性能等相关因素,都会偶尔引起死锁. 这也是SQL没有oracle强大的功能之一. select 标志='死锁的进程', spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran, status,hostname,program_name,hostp

SQL语言逻辑执行顺序

SQL语言逻辑执行顺序 2012-12-18 16:18:13 分类: 数据库开发技术 查询的逻辑执行顺序 FROM < left_table> ON < join_condition> < join_type> JOIN < right_table> WHERE < where_condition> GROUP BY < group_by_list> WITH {cube | rollup} HAVING < having_c

SQL Server优化之SQL语句优化

一切都是为了性能,一切都是为了业务 一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WHERE where_condition (5) GROUP BY group_by_list (6) WITH {cube | rollup} (7) HAVING having_condition (8) SELECT (9) DISTINCT (11) top_specific

MySQL管理之SQL语句实例

MySQL管理之SQL语句实例 首先,我们来导入world库,这个world库中的表是mysql ocp考试专用表,在网上有下 mysql> source/root/world_innodb.sql 表结构如下: 本文来自http://yijiu.blog.51cto.com 转载请经博主允许 ,盗帖可耻! CREATE TABLE `Country` (  `Code` char(3) NOT NULL DEFAULT '',  `Name` char(52) NOT NULL DEFAULT

SQL中删除某数据库所有trigger及sp

SQL中删除某数据库所有trigger及sp   编写人:CC阿爸 2014-6-14 在日常SQL数据库的操作中,如何快速的删除所有trigger及sp呢 以下有三种方式可快速处理. --第一种 --事务的处理方法 Begin Transaction Begin try declare @SQL varchar(max) set @SQL='' select @SQL=@SQL+name+',' from sysobjects where xtype='TR' and name<>'Drop

转摘: CSDN linxianliang5201314 的 blog ------sql解释执行顺序

我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动 大,那么我么还能保证下一段时间系统还能流畅的运行吗?我么还能保证下一个人能看懂我么的存储过程吗?那么我结合公司平时的培训和平时个人工作经验和大家 分享一下,希望对大家有帮助. 要知道SQL语句,我想我们有必要知道SQL Server查询分析器怎么执行我们的SQL语句的,我们很多人会看执行计划,或者用Profiler来监视和调优查询语句或者存储过程慢的原因