数据库及 SQL

1、有三张表,学生表 S,课程 C,学生课程表 SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过 SC 表关联。

  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;

2、数据库表(Test)结构如下:

  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 和名字

  select employee.name from test employee where employee.age > (select manager.age from test manager where manager.id=employee.manager);

posted @ 2020-06-02 09:32  朝暮的小知识  阅读(166)  评论(0)    收藏  举报