我是从别的博主抄来的题,算不上面试题,但是基本上涵盖了查询所需要的方法。包括多表查询等
原博地址:http://blog.sina.com.cn/s/blog_767d65530101861c.html
虽然比较老,但是知识是不变的。
下面贴出我的答案,与原博的不一样,但是也能实现结果。(题目需要查看原博,从最基本的SQL语句建表开始)
//创建学生表
create table student (
id int(10) not null PRIMARY key ,
name varchar(50) not null ,
age int(10) not null ,
sex varchar(4),
BirthDay YEAR ,
Deptment VARCHAR(20) not NULL,
Address varchar(50)
)
//创建score成绩表
create table Sorce(
id int(10) not NULL UNIQUE PRIMARY KEY auto_increment,
Stu_id INT(10) NOT NULL ,
C_name VARCHAR(20),
Grade INT(10)
)
插入数据
#插入数据项数据库
INSERT INTO student VALUES( 901,'张老大', 20,'男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', 21,'男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三',22, '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四',23, '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五',21, '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', 22,'男',1988,'计算机系', '湖南省衡阳市');
#项source表中填入数据
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);
答案如下:
#查询student表的所有记录
select * from student
#查询score表的所有记录
SELECT * from score
#查询student表的第2条到4条记录
SELECT * from student LIMIT 1,3
#查询score表的第2条到4条记录
SELECT * from score LIMIT 4,2
#查询score表的第6条到8条记录
SELECT * from score LIMIT 5,3
#从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息\
SELECT id,name,Deptment from student
#从student表中查询计算机系和英语系的学生的信息
错误的:SELECT * from student where Deptment='计算机系' and Deptment='英语系'
select * from student where Deptment in('计算机系','英语系')
#从student表中查询年龄18~22岁的学生信息(havaing 语句的使用表示的是先分组再进行筛选 往往和 GROUP BY 联合使用)
SELECT * from student GROUP BY age HAVING 18<age<22
SELECT id,name,2013-BirthDay as age ,Deptment,Address from student where 2013-BirthDay BETWEEN 18 AND 22
#从student表中查询每个院系有多少人
SELECT Deptment ,count(id) from student GROUP BY Deptment
#从score表中查询每个科目的最高分
SELECT C_name,MAX(Grade) from score GROUP BY C_name
下面的比较复杂也是本篇的核心,读者可以对比原博客的写法进行学习。
#.查询李四的考试科目(c_name)和考试成绩(grade)
SELECT c_name,grade FROM score INNER JOIN (SELECT * FROM student WHERE name='李四')a ON a.id=score.Stu_id
SELECT id from student where name='李四'
SELECT c_name,grade FROM score where Stu_id(SELECT id from student where name='李四')
#$用连接的方式查询所有学生的信息和考试信息
SELECT name,sex,BirthDay,Deptment,Address ,C_name ,Grade from student,score where student.id=score.Stu_id
#12.计算每个学生的总成绩
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.查询计算机成绩低于95的学生信息
SELECT * from student where Deptment ='计算机系' INNER JOIN
(SELECT * from score where C_name='计算机' and Grade < 95)a on student.id = a.Stu_id
正确:
select * from student where id in (SELECT Stu_id from score where C_name="计算机" and grade<95)
#15.查询同时参加计算机和英语考试的学生的信息
SELECT * from student where
(SELECT Stu_id FROM score where C_name="计算机" AND C_name="英语")
SELECT Stu_id FROM score where C_name="计算机" AND C_name="英语"
SELECT * from student INNER JOIN
(SELECT Stu_id FROM score where C_name="英语" )a INNER JOIN ON student.id=a.Stu_id
(SELECT Stu_id FROM score where C_name="计算机")b ON student.id=a.Stu_id=b.Stu_id
SELECT Stu_id from score where C_name="计算机"
###############上面的是错误的
#第一步:
SELECT * from score where Stu_id in (SELECT Stu_id from score where C_name="计算机") AND C_name="英语"
#(重叠式的,先满足一个条件,再将这个条件嵌套在另外一个条件中充当元素)
#第二步:
SELECT student.id,name,sex,BirthDay,Deptment,Address from student INNER JOIN (SELECT * from score where Stu_id in
(SELECT Stu_id from score where C_name="计算机") AND C_name="英语")a ON student.id=a.Stu_id
#16.将计算机考试成绩,按从高到低进行排序
SELECT Stu_id,Grade from score where C_name="计算机" ORDER BY Grade DESC
#17.从student表和score表中查询出学生的学号,然后合并查询结果(考察union 联合查询合并重复)
SELECT id from student
union
SELECT Stu_id from score
#18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
#正确:
SELECT student.id,name,Deptment,C_name,Grade from student,score where (name like '王%' OR name like '张%') AND student.id=score.Stu_id
select student.id,name,Deptment,C_name,Grade from student,score where (name like '王%' OR name like '张%') and student.id= score.Stu_id
#19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select a.name ,a.age,a.Deptment,C_name,Grade from score INNER JOIN
(SELECT * from student where Address LIKE '%湖南%' )a on a.id=score.Stu_id
半吊子初学并稍微学有所成,诚心招各位学友学习一起走向架构师。
飞向架构师交流群:264698630