一、单表查询
素材: 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等
CREATE TABLE `worker` (
`部门号` int(11) NOT NULL,
`职工号` int(11) NOT NULL,
`工作时间` date NOT NULL,
`工资` float(8,2) NOT NULL,
`政治面貌` varchar(10) NOT NULL DEFAULT '群众',
`姓名` varchar(20) NOT NULL,
`出生日期` date NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
-- 1、显示所有职工的基本信息。
select * from worker;
-- 2、查询所有职工所属部门的部门号,不显示重复的部门号。
select distinct 部门号 from worker;
-- 3、求出所有职工的人数。
select count(职工号) from worker;
-- 4、列出最高工和最低工资。
select max(工资),min(工资) from worker;
-- 5、列出职工的平均工资和总工资。
select avg(工资),sum(工资) from worker;
-- 6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
create table work_date(职工号 int(11) not null,姓名 varchar(20) not null,参加工作 varchar(20));
-- 7、显示所有女职工的年龄。
select `出生日期` from worker where
-- 8、列出所有姓刘的职工的职工号、姓名和出生日期。
select 职工号,姓名,出生日期 from worker where `姓名` like '刘%';
-- 9、列出1960年以前出生的职工的姓名、参加工作日期。
select 姓名,工作时间 from worker where `出生日期` < '1996-0-0';
-- 10、列出工资在1000-2000之间的所有职工姓名。
select 姓名 from worker where 1000<`工资`<2000;
-- 11、列出所有陈姓和李姓的职工姓名。
select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
-- 12、列出所有部门号为2和3的职工号、姓名、党员否。
select 职工号,姓名,政治面貌 from worker where 部门号=102 or 部门号=103;
-- 13、将职工表worker中的职工按出生的先后顺序排序。
select * from worker order by 出生日期;
-- 14、显示工资最高的前3名职工的职工号和姓名。
select 职工号,姓名 from worker order by 工资 limit 3;
-- 15、求出各部门党员的人数。
select 部门号,count(职工号) from worker group by 部门号;
-- 16、统计各部门的工资和平均工资
select sum(工资),avg(工资) from worker group by 部门号;
-- 17、列出总人数大于4的部门号和总人数。
select 部门号,count(职工号) as 总人数 from worker group by 部门号 having sum(职工号) >4;
二、多表查询
1.创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
-- 3.查询student表的所有记录
select * from student;
-- 4.查询student表的第2条到4条记录
select * from student limit 1,3;
-- 5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
select id,name,department from student;
-- 6.从student表中查询计算机系和英语系的学生的信息
select * from student where department in ('英语系','计算机系');
-- 7.从student表中查询年龄18~22岁的学生信息
select * from student where birth between 1990 and 2000;
-- 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.用连接的方式查询所有学生的信息和考试信息
select * from score inner join student on score.stu_id=student.id;
-- 12.计算每个学生的总成绩
select stu_id,sum(grade) from score group by stu_id;
-- 13.计算每个考试科目的平均成绩
select c_name,avg(grade) from score group by c_name;
-- 14.查询计算机成绩低于95的学生信息
select * from student where id in (select stu_id from score where c_name='计算机' and grade<95);
-- 15.查询同时参加计算机和英语考试的学生的信息
select stu_id from score where c_name='计算机' or c_name='英语';
-- 16.将计算机考试成绩按从高到低进行排序
select * from score where c_name='计算机' order by grade desc;
-- 17.从student表和score表中查询出学生的学号,然后合并查询结果
select id from student union select stu_id from score group by stu_id;
-- 18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select student.name,student.department,score.c_name,score.grade from score inner join student on score.stu_id=student.id where student.name like '张%' or student.name like '王%';
-- 19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select student.name,student.birth,student.department,score.c_name,score.grade from score inner join student on score.stu_id=student.id where address like '湖南%';
三,面试题
学生表 Student
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-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');
科目表 Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表 SC
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);
1,
select student.SId,student.Sname,t.shunian,t.chengji from (select count(sid) as shunian,sid,sum(score) as chengji from sc group by sid) as t inner join student on t.sid=student.SId;
2,
select student.SId,student.Sname from sc inner join student on sc.SId=student.Sid where cid=(select cid from course inner join teacher on course.tid=teacher.tid where teacher.Tname='张三');
4,
select t1.sid,t2.score as '语文',t3.score as '数学',t4.score as '英语',t1.number,t1.avgs from (select sid,count(score) as number,avg(score) as avgs from sc group by sid) as t1 left join (select sid,score from sc where cid='01') as t2 on t1.sid=t2.sid left join (select sid,score from sc where cid='02') as t3 on t1.sid=t3.sid left join (select sid,score from sc where cid='03') as t4 on t1.sid=t4.sid order by t1.avgs desc;
5
select course.cid,course.cname,c1 as '[100-85]',c2 as '(85-70]',c3 as '(70-60]',c4 as '<60' from course left join (select cid,count(cid) as c1 from sc where score<=100 and score>=85 group by cid) as t1 on course.cid=t1.cid left join (select cid,count(cid) as c2 from sc where score<85 and score>=70 group by cid) as t2 on course.cid=t2.cid left join (select cid,count(cid) as c3 from sc where score<70 and score>=60 group by cid) as t3 on course.cid=t3.cid left join (select cid,count(cid) as c4 from sc where score<60 group by cid) as t4 on course.cid=t4.cid;