mysql 查询语句学生表、课程表、 成绩表、教师表mysql 查询语句学生表、课程表、 成绩表、教师表.docx
文本预览下载声明
【引用】学生表、课程表、 成绩表、教师表50个常用sql语句?2011-08-07 15:01:48|??分类: 数据库 |??标签: |字号大中小?订阅 本文引用自大河之舟《学生表、课程表、 成绩表、教师表50个常用sql语句》001--Student(S#,Sname,Sage,Ssex) --学生表 002--Course(C#,Cname,T#) --课程表 003--SC(S#,C#,score) --成绩表 004--Teacher(T#,Tname) --教师表 005??006create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2))? 007--前面加一列序号: 008if 009exists(select table_name from information_schema.tables 010?where table_name=Temp_Table) 011drop table Temp_Table 012go 013select 排名=identity(int,1,1),* INTO Temp_Tablefrom Student? 014go 015select * from Temp_Table 016go? 017??018drop database [ ]? --删除空的没有名字的数据库 019--问题: 020--1、查询“”课程比“”课程成绩高的所有学生的学号; 021?select a.S# from (select s#,score from SC where C#=001) a,(select s#,score? 022?from SC where C#=002) b? 023?where a.scoreb.score and a.s#=b.s#;? 024??025--2、查询平均成绩大于分的同学的学号和平均成绩; 026?select S#,avg(score)? 027?from sc? 028?group by S# having avg(score) 60;? 029??030--3、查询所有同学的学号、姓名、选课数、总成绩; 031?select Student.S#,Student.Sname,count(SC.C#),sum(score)? 032?from Student left Outer join SC on Student.S#=SC.S#? 033?group by Student.S#,Sname? 034??035--4、查询姓“李”的老师的个数; 036?select count(distinct(Tname))? 037?from Teacher? 038?where Tname like 李%;? 039??040--5、查询没学过“叶平”老师课的同学的学号、姓名; 041?select Student.S#,Student.Sname? 042?from Student043?where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=叶平);? 044??045--6、查询学过“”并且也学过编号“”课程的同学的学号、姓名; 046?select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#=001and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#=002);? 047??048--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 049?select S#,Sname? 050?from Student? 051?where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=叶平 group by S# having count(SC.C#)=(select count(C#) from Course,Teacher? where Teacher.T#=Course.T# and Tname=叶平));? 052??053--8、查询课程编号“”的成绩比课程编号“”课程低的所有同学的学号、姓名; 05
显示全部