表结构:
Student学生表(学号、姓名、性别、年龄、编辑)
Course课程表(编号、课程名称)
sc选课表(选课编号、学号、课程编号、成绩)
(1)写一个SQL语句,查询选修了“计算机原理”的学生学号和姓名
(2)写一个SQL语句,查询“小明”同学选修的课程名称
(3)写一个SQL语句,查询选修了5门课程的学生学号和姓名
答案:
(1)
select student.stu_no,student.stu_name
from student,course,sc
where course.c_no=sc.c_no and sc.stu_no=student.stu_no and course.c_name=‘计算机原理‘;
(2)
select course.c_name,student.stu_name
from student,course,sc
where student.stu_name=‘小明‘ and student.stu_no=sc.stu_no and sc.c_no=course.c_no;
(3)
select student.stu_no,stu_name
from student,course,sc
where student.stu_no=sc.stu_no and sc.c_no=course.c_no
group by student.stu_no
having count(*)=5
建表及插入数据可参考如下语句:
create table student(
stu_no int,
stu_name varchar(10),
sex char(1),
age int(3),
edit varchar(20)
);
insert into student values
(1,‘wang‘,‘男‘,21,‘hello‘),
(2,‘小明‘,‘女‘,22,‘haha2‘),
(3,‘hu‘,‘女‘,23,‘haha3‘),
(4,‘li‘,‘男‘,25,‘haha4‘);
create table course(
c_no int,
c_name varchar(10)
);
insert into course values
(1,‘计算机原理‘),
(2,‘java‘),
(3,‘c‘),
(4,‘php‘),
(5,‘py‘);
create table sc(
sc_no int,
stu_no int,
c_no int,
score int(3)
);
insert into sc values
(1,1,1,80),
(2,2,2,90),
(3,2,1,85),
(4,2,3,70),
(5,2,4,95),
(6,2,5,89);