1.創(chuàng)建student和score表
Create table student(
Id int(10)not nullunique primary key,
Name Varchar(20) not null,
Sex varchar(4),
Birth year,
Department varchar(20),
Address varchar(50)
);
創(chuàng)建score表
create table score(
id int(10) not null unique primary keyauto_increment,
stu_id int(10) not null,
c_name varchar(20),
grade int(10)
);
2.為student 表和score 表增加記錄
向student表中插入記錄
Insert into student values(901,‘張老大’底燎,‘男’,’1985‘,’計算機(jī)系’,‘北京市海淀區(qū)’);
Insert into student values(901,‘張老二’,‘男’棕兼,‘’1986‘,中文系,‘北京市昌平區(qū)’);
Insert into student values(901,‘張三’契邀,‘男’,’1990‘, ‘中文系’失暴,‘湖南省永州市’)坯门;
Insert into student values(901,‘李四’微饥,‘男’,’1990‘, ‘英語系’古戴,‘遼寧省阜新市’)欠橘;
Insert into student values(901,‘王五’,‘男’现恼,’1991, ‘英語系’肃续,‘福建省廈門市’);
Insert into student values(901,‘王六’叉袍,‘男’始锚,’1988‘, ‘計算機(jī)系’,‘湖南省衡陽市’)喳逛;
向score表插入記錄的語句
Insert into score values( null ,901,’計算機(jī)’,98)瞧捌;
Insert into score values( null ,901,’英語‘,80);
Insert into score values( null ,902,’計算機(jī)‘,65)润文;
Insert into score values( null ,902,’中文,‘88)察郁;
Insert into score values( null ,903,’中文‘,95);
Insert into score values( null ,904,’計算機(jī)‘,70)转唉;
Insert into score values( null ,904,’英語‘,92)皮钠;
Insert into score values( null ,905,’英語‘,94);
Insert into score values( null ,906,’計算機(jī)‘,90)赠法;
Insert into score values( null ,906,’英語,‘85)麦轰;
3.查詢student表的所有記錄
select * from student;
4.查詢student表的第2條到第4條的記錄? 關(guān)鍵詞 limit
select * from student limit 1,3砖织;
5.從student表查詢所有學(xué)生的學(xué)號 款侵、姓名和院系的信息
select id,name侧纯,department from student新锈;
6.從student表中查詢計算機(jī)系和英語系的學(xué)生的信息
select * from student where department in(‘計算機(jī)系’,‘英語系’)眶熬;
7.從student表中查詢年齡18-22歲的學(xué)生信息
select id妹笆,name ,sex,2013-birth as age,department,address from student where 2013-birth between 18 and 22;
select? id,name,sex,2013-birth,department ,address from student where 2013-birth >= 18 and 2013-birth <= 22;
8.從student表中查詢每個院系有多少人
select department,count(id) from student group by department;
9.從score表中查詢每個科目的最高分
select c_name娜氏,Max(grade) from score group by c_name拳缠;
10.查詢李四的考試科目(c_name)和考試成績(grade)
select c_name,grade from score where stu_id = (select id from student where name = ‘李四’)贸弥;
11.用鏈接的方式查詢所有學(xué)生的信息和考試信息
select student.id, name , sex , birth ,department ,address,c_name,grade from student , score where? student.id = score.stu_id;
12.計算每個學(xué)生的總成績
select? student.id ,name ,sum(grade) from student ,score where student.id = score.stu_id? group by id;
13.計算每個考試科目的平均成績
select c_name,avg(grade) from score group by c_name;
14.查詢計算機(jī)成績低于95的學(xué)生信息
select * from student? where id in(select stu_id from score where c_name = '計算機(jī)’ and grade < 95);
15.查詢同時參加計算機(jī)和英語考試的學(xué)生的信息
select * from student where id = any??
(select stu_id from score where stu_id in?
(select? stu_id from score where c_name = '計算機(jī)' )
and c_name = '英語');
select a.* from student a ,score b,score c?
where a.id = b.stu_id
and b.c_name ='計算機(jī)'
and a.id = c.stu_id
and c.c_name = '英語';
16.將計算機(jī)考試成績按從高到低進(jìn)行排序
select stu_id ,grade?
from score where c_name = '計算機(jī)'
order by grade desc;
17.從student表和score表中查詢出學(xué)生的學(xué)號窟坐,然后合并查詢結(jié)果
select id from student? ?union? ?select stu_id from score;
18.查詢姓張或者姓王的同學(xué)的姓名、院系和考試科目及成績
select student.id ,name,sex,birth,department,address,c_name,grade
from student ,score?
where?
(name like '張%' or name like '王%')
and
student.id = score.stu_id;
19.查詢都是湖南的學(xué)生的姓名哲鸳、年齡臣疑、院系和考試科目及成績
select student.id ,name,sex,birth,department,address,c_name ,grade,
from student,score
where address like '湖南%' and?
student.id = score.stu_id;