数据库练习题26-45题

 1 --26、  查询存在有85分以上成绩的课程Cno.
 2 select distinct Cno from Score where Degree >85
 3
 4 --27、查询出“计算机系“教师所教课程的成绩表。
 5 select * from Score where Cno in ( select Cno from Course where Tno in ( select Tno from Teacher where Depart = ‘计算机系‘))
 6
 7
 8 --0028、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
 9 select Tname,Prof from Teacher t1 where Depart = ‘计算机系‘ and not exists (select * from Teacher t2 where Depart = ‘电子工程系‘ and t1.Prof = t2.prof)
10 union
11 select Tname,Prof from Teacher t1 where Depart = ‘电子工程系‘ and not exists (select * from Teacher t2 where Depart = ‘计算机系‘ and t1.Prof = t2.prof)
12
13 --0029、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。--any 任何一个值 , all 所有的数值
14 select * from Score where Degree >any( select Degree from Score where Cno=‘3-245‘) and Cno =‘3-105‘  order by  Degree desc
15
16 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
17 select * from Score where Degree > all(select Degree from Score where Cno=‘3-245‘) and Cno=‘3-105‘
18
19 --31、 查询所有教师和同学的name、sex和birthday.
20     select Sname,Ssex,Sbirthday from Student
21 union
22     select Tname,Tsex,Tbirthday from Teacher
23
24 --32、查询所有“女”教师和“女”同学的name、sex和birthday.
25     select Sname,Ssex,Sbirthday from Student where Ssex=‘女‘
26 union
27     select Tname,Tsex,Tbirthday from Teacher where Tsex=‘女‘
28
29 --33、 查询成绩比该课程平均成绩低的同学的成绩表。
30     select * from Score s1 where Degree<(select AVG(degree) from Score s2 group by Cno having s1.Cno= s2.Cno)
31
32 --34、 查询所有任课教师的Tname和Depart.
33     select Tname,Depart from Teacher where Tno in ( select Tno from Course where Cno in (select Cno from Score))
34
35 --35 、 查询所有未讲课的教师的Tname和Depart.
36     select Tname,Depart from Teacher where Tno not in ( select Tno from Course where Cno in (select Cno from Score))
37
38 --36、查询至少有2名男生的班号。--先选出所有男生,再按班级分组
39     select Class from Student where Ssex=‘男‘ group by Class having COUNT(*)>1
40
41 --37、查询Student表中不姓“王”的同学记录。
42     select * from Student where Sname not like ‘王%‘
43
44 --0038、查询Student表中每个学生的姓名和年龄。
45      --注:year(getdate())获取当前时间“年” getdate()获取
46     select Sname,year(getdate())-YEAR(Sbirthday) as age from Student
47
48 --39、查询Student表中最大和最小的Sbirthday日期值。
49      select MAX(Sbirthday),MIN(Sbirthday) from Student
50
51 --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
52     select  *  from Student  order by Class desc,Sbirthday asc
53
54 --41、查询“男”教师及其所上的课程。
55     select Cname,Tname from Course join Teacher on Course.Tno=Teacher.Tno where Tsex=‘男‘ --联合2个表格做法
56
57 select * from course where tno in(select tno from teacher where tsex=‘男‘)    --where做法
58
59 --42、查询最高分同学的Sno、Cno和Degree列。
60     select Sno,Cno,Degree from Score where Degree =( select MAX(Degree) from Score )--方法一
61
62     select top 1 * from score order by degree desc     --方法二
63
64 --43、查询和“李军”同性别的所有同学的Sname.
65     select Sname from Student where Ssex=( select Ssex from Student  where Sname=‘李军‘) and Sname !=‘李军‘
66
67 --44、查询和“李军”同性别并同班的同学Sname.
68     select Sname from Student where Ssex=( select Ssex from Student  where Sname=‘李军‘) and Class=(select Class from Student where Sname=‘李军‘) and Sname !=‘李军‘ --方法一
69
70 select sname from student s1 where ssex=(
71 select ssex from student s2 where sname=‘李军‘ and s1.class= s2.class
72 )--方法二
73
74 --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
75     select * from Score where Cno=( select Cno from Course where Cname=‘计算机导论‘) and Sno in (select Sno from Student where Ssex=‘男‘)--f方法1
时间: 2024-08-02 11:02:14

数据库练习题26-45题的相关文章

数据库练习题1-25题

1 --1. 查询Student表中的所有记录的Sname.Ssex和Class列 2 select Sname,Ssex,Class from Student 3 4 --2. 查询教师所有的单位即不重复的Depart列.--distinct 去除重复 5 select distinct Depart from Teacher 6 7 --3. 查询Student表的所有记录. 8 select * from Student 9 10 --4. 查询Score表中成绩在60到80之间的所有记录

Day 45 Mysql 数据库练习题二

1.表关系   注意:创建表时,根据合理性设置字段的长度和类型. 2.下面:开始你的表演 1.查询所有人员信息 2.只查询人员的姓名和年龄 3.查询年龄为20岁的有哪些人员 4.查询60岁以下的人员有哪些人员 5.查询50岁以上并且工资大于8000的人员有哪些 6.查询姓[张]的人员有哪些 7.查询哪些人员属于 武当/华山/嵩山 8.查询工资在 5000-8900 的人员有哪些 9.查询所有人员,要求按工资倒序排列 10.查询令狐冲的领导人是谁 11.查询人员表中最高工资是多少 12.查询人员表

数据库练习题(2--简单查询题)

2.表的结构如下: student表(id,name,sex,class_id,address); score表(id,student_id,course_id,score); class表(id,name,grade_id); grade表(id,name); course表(id,name); 1)查询出"高一年级"下面的所有班级信息 分析:用到的表有grade,class 方法:把grade与class通过年级号联合后,选出高一年级的班级信息即可 语句: select s1.*

数据库基础(面试常见题)

一.数据库基础 1. 数据抽象:物理抽象.概念抽象.视图级抽象,内模式.模式.外模式 2. SQL语言包括数据定义.数据操纵(Data Manipulation),数据控制(Data Control) 数据定义:Create Table,Alter Table,Drop Table, Craete/Drop Index等 数据操纵:Select ,insert,update,delete, 数据控制:grant,revoke 3. SQL常用命令: CREATE TABLE Student( I

mysql数据库基础语句训练题

SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for course -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `cno` varchar(10) DEFAULT NULL, `Cname` varchar(255) DEFAULT NULL, `tno` va

php 查询45题

表格代码 create table student ( sno varchar(20) primary key, sname varchar(20) not null, ssex varchar(20) not null, sbirthday datetime, class varchar(20) ) ; create table teacher ( tno varchar(20) primary key, tname varchar(20) not null, tsex varchar(20)

全国计算机等级考试 三级数据库精选填空100题

1. 用树型结构表示实体类型及实体间联系的数据模型称为(层次模型). 2. 模式/内模式映象为数据库提供了(物理)数据独立性. 3. 在层次.网状模型中,数据之间联系用(指针)实现. 4. 数据库管理技术的发展经过三个阶段(人工管理阶段),(文件系统阶段),(数据库阶段). 5. 三种主要的数据模型包括(层次模型),(网状模型),(关系模型). 6. 数据模型的三要素包括(数据结构),(数据操作),(数据完整性约束). 7. 由于数据冗余,当进行更新时,稍不谨慎,易引起(数据不一致性). 8. 

MySQL 数据库 练习题

一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示.用SQL语句创建四个表并完成相关题目. 表1-1数据库的表结构 表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Char(3) 否 学号(主码) Sname Char(8) 否 学生姓名 Ssex Char(2) 否

15-07-19数据库练习题答案

创建数据库,创建表 create database xiti use xiti create table Student ( Sno char(3) not null primary key, Sname char(8) not null, Ssex char(2) not null, Sbirthday datetime, Class char(5), ) create table Teacher ( Tno char(3) not null primary key, Tname char(4