学生表:xstudent
+-------+------+------+----------+-------+
|(学生ID) (名字) (性别) (生日) (班级)
+-------+------+------+----------+-------+
|xsid xsnm xssex xsbirthday sclass
+-------+------+------+----------+-------+
|108 曾华 男 09/01/77 95033
+-------+------+------+----------+-------+
|105 匡明 男 10/02/75 95031
+-------+------+------+----------+-------+
|107 王丽 女 01/23/76 95033
+-------+------+------+----------+-------+
|101 李军 男 02/20/76 95033
+-------+------+------+----------+-------+
|109 王芳 女 02/10/75 95031
+-------+------+------+----------+-------+
|103 陆军 男 06/03/74 95031
+-------+------+------+----------+-------+
sql:
+------------------------------------------------------------------+
CREATE TABLE IF NOT EXISTS `xstudent` (
`xsid` int(11) NOT NULL auto_increment,
`xsnm` varchar(50) default NULL,
`xssex` tinyint(4) default NULL,
`xsbrithday` date default '0000-00-00',
`sclass` varchar(11) default NULL,
PRIMARY KEY (`xsid`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
INSERT INTO `xstudent` VALUES (108, '曾华', 1, '1977-06-23', '95033');
INSERT INTO `xstudent` VALUES (105, '匡明', 1, '1975-06-03', '95031');
INSERT INTO `xstudent` VALUES (107, '王丽', 0, '1976-06-17', '95033');
INSERT INTO `xstudent` VALUES (101, '李军', 1, '1976-02-22', '95033');
INSERT INTO `xstudent` VALUES (109, '王芳', 0, '1975-02-10', '95031');
INSERT INTO `xstudent` VALUES (103, '陆军', 1, '1974-03-06', '95031');
+-------------------------------------------------------------------+
组合表:score
+-------+--------+-------+
(学生ID) (课程ID) (成绩)
+-------+--------+-------+
|xsid kuid degree
+-------+--------+-------+
|103 3-245 84
+-------+--------+-------+
|105 3-245 67
+-------+--------+-------+
|109 3-245 98
+-------+--------+-------+
|103 3-105 78
+-------+--------+-------+
|105 3-105 91
+-------+--------+-------+
|109 3-105 68
+-------+--------+-------+
|101 3-105 93
+-------+--------+-------+
|107 3-105 72
+-------+--------+-------+
|108 3-105 66
+-------+--------+-------+
|101 6-166 80
+-------+--------+-------+
|107 6-166 77
+-------+--------+-------+
|108 6-166 62
+-------+--------+-------+
sql:
+-----------------------------------------------+
CREATE TABLE IF NOT EXISTS `score` (
`xsid` int(11) NOT NULL,
`kuid` varchar(20) default NULL,
`degree` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk COMMENT='组合表';
INSERT INTO `score` VALUES (103, '3-245', 84);
INSERT INTO `score` VALUES (105, '3-245', 67);
INSERT INTO `score` VALUES (109, '3-245', 98);
INSERT INTO `score` VALUES (103, '3-105', 78);
INSERT INTO `score` VALUES (105, '3-105', 91);
INSERT INTO `score` VALUES (109, '3-105', 68);
INSERT INTO `score` VALUES (101, '3-105', 93);
INSERT INTO `score` VALUES (107, '3-105', 72);
INSERT INTO `score` VALUES (108, '3-105', 66);
INSERT INTO `score` VALUES (101, '6-166', 80);
INSERT INTO `score` VALUES (107, '6-166', 77);
INSERT INTO `score` VALUES (108, '6-166', 62);
+-----------------------------------------------+
课程表:course
+-------+--------+--------+
(课程ID) (课名字) (老师ID)
+-------+--------+--------+
|kuid kmname lsid
+-------+--------+--------+
|3-105 计算机导论 825
+-------+--------+--------+
|3-245 操作系统 804
+-------+--------+--------+
|6-166 数字电路 856
+-------+--------+--------+
|9-888 高等数学 831
+-------+--------+--------+
sql:
+----------------------------------------------------+
CREATE TABLE IF NOT EXISTS `course` (
`kuid` varchar(20) NOT NULL,
`kmname` varchar(50) NOT NULL,
`lsid` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk COMMENT='course';
INSERT INTO `course` VALUES ('3-105', '计算机导论', 825);
INSERT INTO `course` VALUES ('3-245', '操作系统', 804);
INSERT INTO `course` VALUES ('6-166', '数字电路', 856);
INSERT INTO `course` VALUES ('9-888', '高等数学', 831);
+----------------------------------------------------+
老师表:teacher
+-------+--------+-------+-----------+------+---------+
|(老师ID) (名字) (性别) (生日) (职称) (系)
+-------+--------+-------+-----------+------+---------+
|lsid lsname lssex lsbirthday lsprof lsdepart
+-------+--------+-------+-----------+------+---------+
|804 李城 男 12/02/58 副教授 计算机系
+-------+--------+-------+-----------+------+---------+
|856 张旭 男 03/12/69 讲师 电子工程系
+-------+--------+-------+-----------+------+---------+
|825 王萍 女 05/05/72 助教 计算机系
+-------+--------+-------+-----------+------+---------+
|831 刘冰 女 08/14/77 助教 电子工程系
+-------+--------+-------+-----------+------+---------+
sql:
+-------------------------------------------------------+
CREATE TABLE IF NOT EXISTS `teacher` (
`lsname` varchar(40) default NULL,
`lssex` tinyint(4) default NULL,
`lsbirthday` date default '0000-00-00',
`lsprof` varchar(20) default NULL,
`lsdepart` varchar(25) default NULL,
`lsid` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk COMMENT='教师信息表';
INSERT INTO `teacher` VALUES('李城',1,'1958-12-02','副教授','计算机系',804);
INSERT INTO `teacher` VALUES('张旭',1,'1969-12-03','讲师','电子工程系',856);
INSERT INTO `teacher` VALUES('王萍',0,'1972-05-05','助教','计算机系',825);
INSERT INTO `teacher` VALUES('刘冰',0,'1977-08-14','助教','电子工程系',831);
INSERT INTO `teacher` VALUES ('丁力',0,'1972-05-05','教师','计算机系',825);
INSERT INTO `teacher` VALUES ('许文祥',0,'1977-08-14','教师','电子工程系',831);
+---------------------------------------------------------------------------+
要求:
1.在表teacher中,查询"计算机系"与"电子工程系"不同职称的教师的lsname和lsprof
方法一:
+-------------------------------------------------------+
|select a.lsname,a.lsprof from teacher a |
|where (a.lsdepart ="计算机系" or a.lsdepart ="电子工程系")|
|and a.lsprof not in ( |
| select lsprof |
| from teacher |
| group by lsprof |
| having count(lsprof)>=2 |
| ) |
+-------------------------------------------------------+
方法二:
+-------------------------------------------------------+
|SELECT lsname,lsprof from teacher t |
|where lsdepart ="计算机系" or lsdepart ="电子工程系" |
|and not exists ( |
| select lsprof from teacher |
| where lsprof =t.lsprof |
| ); |
+-------------------------------------------------------+
2.查询选修课编号是"3-105"课程且成绩至少高于选修课程编号为"3-245"的同学的kuid,xsid和degree,并且degree从高到低次序排列
+---------------------------------+
|SELECT a.kuid, a.xsid, a.degree |
|FROM score a, score b |
|WHERE a.kuid = '3-105' |
|AND b.kuid = '3-245' |
|AND a.xsid = b.xsid |
|AND a.degree > b.degree |
|order by a.degree asc; |
+---------------------------------+
3.查询成绩比该课程平均成绩低的同学的成绩表
+-------------------------------------+
|SELECT a.xsid, a.kuid, a.degree |
|FROM score a, ( |
| SELECT kuid, avg( degree ) AS degree|
| FROM score |
| GROUP BY kuid |
| )b |
|WHERE a.degree > b.degree |
|AND a.kuid = b.kuid |
+-------------------------------------+
4.查询平均成绩最低的学生姓名及其平均成绩
+-------------------------------------+
|select a.xsnm,avg(b.degree) as avg |
|from xstudent a,score b |
|where a.xsid = b.xsid |
|group by b.xsid |
|order by avg limit 1 |
+-------------------------------------+
5.查询选修多门课程的同学中分数为非最高成绩的记录
+--------------------------------------+
|select DISTINCT a.xsid,a.kuid,a.degree|
|from score a, |
|(select kuid,max(degree) as maxdegree |
| from score |
| group by kuid) b, |
|(select xsid,count(kuid) as nums |
| from score |
| group by xsid) c |
|where c.nums>=2 |
|and a.kuid = b.kuid |
|and a.degree<b.maxdegree |
+--------------------------------------+
分享到:
相关推荐
SQL跟踪后,用该工具能够快速整理出整洁的SQL,减少人工的复制、帖贴
SqlServer索引碎片整理脚本,提据库查询效率,很有用。
自制SQL文整理,非开源,非完整,写得很挫,有使用时限,强烈不建议下载
《SQL》基本语句整理大全
对Sql语句的格式进行整理,使其易读。 操作简单,只需拷贝Sql语句,执行整理程序,然后再粘贴,就成为格式清晰的Sql语句了。
SQL Server面试常用sql整理
SQL格式整理工具,而且还可以把SQL语句转换成VB DELPHI C# JAVA里面用的格式
数据库整理工具 SQLserver专业数据整理
一些sql整理实例 sql整理实例 sql整理实例
很实用的sql语句整理工具,把杂乱的语句变得规范和易读。
SQLSERVER基础实用必会SQL语句整理.docx
sql语句整理 sql增删改查 更新数据记录 删除数据记录
经典sql语句整理大全 喜欢的朋友可以下来看看 纯文字版 比较小
DBA日常维护SQL整理,涵盖操作系统、存储、数据库各个层面
自己整理的sql常用语句,比较适合0接触学习sql的人
我最近在CSDN上学的SQL笔记.如有错误请多多指教.
sql优化总结,针对一般sql语句的优化编写进行总结整理,有助于形成良好的sql语句书写习惯
网上找的并汇总整理的SQL日期函数语句,应该比较全面了希望对大家有用
sqllice,一个自动整理SQL文格式的工具,很好用的,适合各语言的开发人员。
SQL Server 2005索引碎片整理