大家好,又见面了,我是你们的朋友全栈君。
Student 学生表(学号,姓名,性别,年龄,组织部门) Course 课程表(编号,课程名称) Sc 选课表(学号,课程编号,成绩) 表结构如下: 1)写一个SQL 语句,查询选修了’计算机原理’的学生学号和姓名(3 分钟) 2)写一个SQL 语句,查询’周星驰’同学选修了的课程名字(3 分钟) 3)写一个SQL 语句,查询选修了5 门课程的学生学号和姓名(9 分钟) 答:1)SQL 语句如下:
select stu.sno, stu.sname from Student stu
where (select count(*) from sc where sno=stu.sno and cno =
(select cno from Course where cname='计算机原理')) != 0;
2)SQL 语句如下:
select cname from Course
where cno in ( select cno from sc where sno =
(select sno from Student where sname='周星驰'));
3)SQL 语句如下:
select stu.sno, stu.sname from student stu
where (select count(*) from sc where sno=stu.sno) = 5;
1)写出建表语句; 2)写出SQL 语句,查询选修了所有选修课程的学生; 3)写出SQL 语句,查询选修了至少5 门以上的课程的学生。 答:1)建表语句如下(mysql 数据库):
create table s(id integer primary key, name varchar(20));
create table c(id integer primary key, name varchar(20));
create table sc(
sid integer references s(id),
cid integer references c(id),
primary key(sid,cid)
);
2)SQL 语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)
= (select count(*) from c);
3)SQL 语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)>=5;
ID NAME AGE MANAGER(所属主管人ID) 106 A 30 104 109 B 19 104 104 C 20 111 107 D 35 109 112 E 25 120 119 F 45 NULL 要求:列出所有年龄比所属主管年龄大的人的ID 和名字? 答:SQL 语句如下:
select employee.name from test employee
where employee.age > (select manager.age from test manager
where manager.id=employee.manager);
表city: 表state: CityNo CityName StateNo BJ 北京(Null) SH 上海(Null) GZ 广州GD DL 大连LN 欲得 到如下结果: City No City Name State No State Name BJ 北京(Null) (Null) DL 大连LN 辽宁GZ 广州GD 广东SH 上海(Null) (Null) 写相 应的SQL 语句。 答:SQL 语句为:
SELECT C.CITYNO, C.CITYNAME, C.STATENO, S.STATENAME
FROM CITY C, STATE S
WHERE C.STATENO=S.STATENO(+)
ORDER BY(C.CITYNO);
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106575.html原文链接:https://javaforall.cn