码迷,mamicode.com
首页 > 数据库 > 详细

day05_日常SQL练习(一)

时间:2016-03-21 21:37:20      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:


查询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‘)






day05_日常SQL练习(一)

标签:

原文地址:http://www.cnblogs.com/xiaoxiao5ya/p/ae12abaab136337c90efa9edc585c0b9.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!