赞
踩
实不相瞒,我就是题目中所说的初级开发,在曲折的面试道路上,被各大互联网公司的笔试题吊打。所以,我就整了一个最经典的学生成绩表sql练习,保证你看完,有所收获。
下面是三张表的创表语句,供你们快速练习:
- Student表:
- CREATE TABLE `student` (
- `sno` varchar(10) NOT NULL,
- `sname` varchar(10) DEFAULT NULL,
- `ssex` varchar(10) DEFAULT NULL,
- `sage` int(50) DEFAULT NULL,
- `birthday` date DEFAULT NULL,
- `sstate` int(2) DEFAULT NULL,
- PRIMARY KEY (`sno`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
- Course表:
- CREATE TABLE `course` (
- `cno` varchar(10) NOT NULL,
- `cname` varchar(10) DEFAULT NULL,
- `cstate` varchar(10) DEFAULT NULL,
- PRIMARY KEY (`cno`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
- SC表:
- CREATE TABLE `sc` (
- `sno` varchar(10) NOT NULL,
- `cno` varchar(10) NOT NULL,
- `score` int(10) DEFAULT NULL,
- PRIMARY KEY (`sno`,`cno`) USING BTREE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Student表:
Course表:
SC表:
废话不讲,拿到笔试题,手机打开,开始百度。
1.插入语句:
insert into student(sno,sname,ssex,sage,birthday,sstate)values('7','九九','男',29,'1929-03-03',2)
2.更新语句:
update student set sage= sage+2 where sname like '张%' and sstate=2
3.简单查询:
select count(1) from student where sstate=1 and ssex='女' and sage between15 and 20
4.连表查询:
select course.* from course left join sc on course.cno= sc.cno
left join student on student.sno = sc.sno WHERE student.sname='王五'
5.多表条件查询:
select student.*,avg(score) avg from student left join sc on student.sno = sc.sno
left join course on course.cno = sc.cno
where course.cstate=1
GROUP BY sno HAVING avg(score)>60
6.说实话这个烂sql,我写了十几分钟,主要是我太菜的原因:
select a.sno,a.score'01语文',b.score'02数学',c.sname from
(select sno,cno,score from sc where cno='1')as a
INNER JOIN
(select sno,cno,score from sc where cno='2')as b
on a.sno=b.sno
INNER JOIN student as c ON c.sno=a.sno
WHERE a.score>b.score其实这条语句可以去掉一些的比如连student表,因为他只需要一个sno结果,所以我只是想让大家看的更清楚些 ,多查了两个字段。
看到这就完了, 下次再不会写联查,扇自己的耳瓜子。
还有啊,我整理了一些面试pdf文档,很多很多,虽然我平时也不看,还不如分享一下呢。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。