MySQLデータベース文練習(参考答案付)
22249 ワード
一、テストデータテーブル
①テストデータ
②試験要求
必要に応じて以上の4枚の表を作成し、対応するテストデータを追加し、テストデータは以下の通りである.
create table student(Sid varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into student values('01' , ' ' , '1990-01-01' , ' ');
insert into student values('02' , ' ' , '1990-12-21' , ' ');
insert into student values('03' , ' ' , '1990-05-20' , ' ');
insert into student values('04' , ' ' , '1990-08-06' , ' ');
insert into student values('05' , ' ' , '1991-12-01' , ' ');
insert into student values('06' , ' ' , '1992-03-01' , ' ');
insert into student values('07' , ' ' , '1989-07-01' , ' ');
insert into student values('08' , ' ' , '1990-01-20' , ' ');
create table course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into course values('01' , ' ' , '02');
insert into course values('02' , ' ' , '01');
insert into course values('03' , ' ' , '03');
create table teacher(Tid varchar(10),Tname varchar(10));
insert into teacher values('01' , ' ');
insert into teacher values('02' , ' ');
insert into teacher values('03' , ' ');
create table sc(Sid varchar(10),Cid varchar(10),score decimal(18,1));
insert into sc values('01' , '01' , 80);
insert into sc values('01' , '02' , 90);
insert into sc values('01' , '03' , 99);
insert into sc values('02' , '01' , 70);
insert into sc values('02' , '02' , 60);
insert into sc values('02' , '03' , 80);
insert into sc values('03' , '01' , 80);
insert into sc values('03' , '02' , 80);
insert into sc values('03' , '03' , 80);
insert into sc values('04' , '01' , 50);
insert into sc values('04' , '02' , 30);
insert into sc values('04' , '03' , 20);
insert into sc values('05' , '01' , 76);
insert into sc values('05' , '02' , 87);
insert into sc values('06' , '01' , 31);
insert into sc values('06' , '03' , 34);
insert into sc values('07' , '02' , 89);
insert into sc values('07' , '03' , 98);
二、sql文の練習
三、参考答案
select a.*, b.*, c.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
left join student c
on a.Sid = c.Sid
where a.score > b.score;
update sc set score=100 where Sid="01" and Cid=(select Cid from course where Cname=" ");
select a.*, b.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
where b.Sid is not null;
select Sid, Sname from student where Sid in
(select Sid from sc group by Sid having avg( score) >= 70);
select a.Sid, a.score, b.Sname from (
select Sid, avg(score) as score from sc group by Sid having avg(score) >= 85)a
left join student b
on a.Sid = b.Sid;
select a.Sid, b.Sname, c.Cname, a.score from sc a
left join student b
on a.Sid = b.Sid
left join course c
on a.Cid = c.Cid
where score > 70;
select * from student where year(Sage) = 1990;
select Cid, count(distinct Sid) as counts from sc group by C
select Ssex, count(distinct Sid) as counts from student group by Ssex;
select * from student where Sname like '% %';