50个经典SQL查询语句

合集下载

pgsql常用查询语句

pgsql常用查询语句

pgsql常用查询语句1. 选择所有列:```sqlSELECT * FROM table_name;```2. 选择特定列:```sqlSELECT column1, column2 FROM table_name;```3. 使用 WHERE 子句筛选数据:```sqlSELECT * FROM table_name WHERE condition;```4. 使用 GROUP BY 子句对数据进行分组:```sqlSELECT column1, AVG(column2) FROM table_name GROUP BY column1;```5. 使用 HAVING 子句对分组后的数据进行筛选:```sqlSELECT column1, AVG(column2) FROM table_name GROUP BY column1 HAVING AVG(column2)>10;```6. 使用 ORDER BY 子句对结果进行排序:```sqlSELECT * FROM table_name ORDER BY column1 ASC/DESC;```7. 使用 LIMIT 子句限制结果返回的行数:```sqlSELECT * FROM table_name LIMIT 10;```8. 使用 JOIN 语句连接多个表:```sqlSELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;```9. 使用子查询(Subquery):```sqlSELECT * FROM table_name WHERE column1 = (SELECT column1 FROM table_name2);```这只是一些基本的查询语句示例,PostgreSQL 支持丰富的 SQL 语法和功能,可以根据具体的需求构建更复杂的查询。

完整word版,三表联查经典50题

完整word版,三表联查经典50题

--一个题目涉及到的50个Sql语句--(下面表的结构以给出,自己在数据库中建立表•并且添加相应的数据,数据要全面些.其中Student表中,SId为学生的ID)----------------------- 表结构-----------------------------学生表tblStudent (编号StuId、姓名StuName、年龄StuAge、性别StuSex)--课程表tblCourse (课程编号Courseld、课程名称CourseName、教师编号Teald)--成绩表tblScore (学生编号StuId、课程编号CourseId、成绩Score)--教师表tbITeacher (教师编号TeaId、姓名TeaName)--1、查询“ 001 ”课程比“ 002”课程成绩高的所有学生的学号;Select StuId From tblStude nt s1Where (Select Score From tblScore t1 Where t1.Stuld=s1.stuld And t1.Courseld='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002')--2、查询平均成绩大于60分的同学的学号和平均成绩;Select StuId,Avg(Score) as AvgScore From tblScoreGroup By StuIdHavi ng Avg(Score)>60--3、查询所有同学的学号、姓名、选课数、总成绩;Select StuId,StuName,SelCourses=(Select Cou nt(Courseld) From tblScore t1 Where t1.StuId=s1.StuId),SumScore=(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId) From tblStude nts1--4、查询姓“李”的老师的个数;Select Count(*) From tblTeacher Where TeaName like '李%'--5、查询没学过“叶平”老师课的同学的学号、姓名;Select StuId,StuName From tblStude ntWhere StuId Not In(Select StulD From tblScore scInner Joi n tblCourse cu ON sc.CourseId=cu.CourseIdInner Join tblTeacher tc ON cu.TeaId=tc.TeaIdWhere tc.TeaName='叶平')--6、查询学过“ 001 ”并且也学过编号“ 002”课程的同学的学号、姓名;Select Stuld,StuName From tbIStude nt stWhere (Select Cou nt(*) From tbIScore si Where s1.Stuld=st.Stuld And s1.Courseld='001')>0And(Select Cou nt(*) From tbIScore s2 Where s2.Stuld=st.Stuld Ands2.Courseld='002')>0--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;Select Stuld,StuName From tbIStude nt st Where n ot exists(Select CourselD From tbICourse cu Inner Join tbITeacher tc On cu.TealD=tc.TealDWhere tc.TeaName='叶平' And CourselD not in(Select CourselD From tbIScore Where StulD=st.StulD))--8、查询课程编号“”的成绩比课程编号“001 ”课程低的所有同学的学号、姓名;Select Stuld,StuName From tbIStude nt siWhere (Select Score From tbIScore t1 Where t1.Stuld=s1.stuld And t1.Courseld='001')> (Select Score From tbIScore t2 Where t2.Stuld=s1.stuld And t2.Courseld='002')9、查询所有课程成绩小于分的同学的学号、姓名;Select Stuld,StuName From tbIStude nt stWhere Stuld Not lN(Select Stuld From tbIScore sc Where st.Stuld=sc.Stuld And Score>60)--10、查询没有学全所有课的同学的学号、姓名;Select Stuld,StuName From tbIStude nt stWhere (Select Cou nt(*) From tbIScore sc Where st.Stuld=sc.Stuld)<(Select Cou nt(*) From tbICourse)--11、查询至少有一门课与学号为“1001 ”的同学所学相同的同学的学号和姓名;-- 运用连接查询Select Dist In ct st.Stuld,StuName From tbIStude nt stInner Join tbIScore sc ON st.Stuld=sc.StuldWhere sc.Courseld lN (Select Courseld From tbIScore Where Stuld='1001') -- 嵌套子查询Select Stuld,StuName From tbIStude ntWhere Stuld ln(Select Distinct Stuld From tbIScore Where Courseld In (Select Courseld From tbIScore Where Stuld='1001'))--12、查询至少学过学号为“1001 ”同学所有课程的其他同学学号和姓名;Select StuId,StuName From tbIStude ntWhere Stuld In(Select Dist in ct Stuld From tbIScore Where Courseld Not In (Select Courseld From tbIScore Where Stuld='1001')--13、把“ SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;(从子查询中获取父查询中的表名,这样也行????)--创建测试表Select * Into Sc From tbIScoregoUpdate Sc Set Score=(SeIect Avg(Score) From tbIScore si Where s1.Courseld=sc.Courseld) Where Courseld IN(Select Courseld From tbICourse cs INNER JOIN tbITeacher tc ON cs.TealD=tc.TealD WHERE TeaName ='叶平')--14、查询和“ 1002”号的同学学习的课程完全相同的其他同学学号和姓名;SeIect StulD,StuName From tbIStude nt stWhere Stuld <> '1002'AndNot Exists(Select * From tbIScore sc Where sc.StuId=st.StuId And Courseld Not In (SeIect Courseld From tbIScore Where Stuld='1002'))AndNot Exists(SeIect * From tbIScore Where Stuld='1002' And Courseld Not In (SeIect Courseld From tbIScore sc Where sc.StuId=st.StuId))--15、删除学习“叶平”老师课的SC表记录;DeIete From tbIScore Where Courseld IN(SeIect Courseld From tbICourse cs INNER JOIN tbITeacher tc ON cs.TeaId=tc.TeaId Where tc.TeaName='叶平')--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、“号课的平均成绩;In sert Into tbIScore (StuId,CourseId,Score)SeIect StuId,'002',(SeIect Avg(Score) From tbIScore Where Courseld='002') From tbIScore WhereStuld Not In (SeIect Stuld From tbIScore Where Courseld='003')--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分SeIect Stuld,数据库=(SeIect Score From tbIScore sc Inner Join tbICourse cs Onsc.CourseId=cs.CourseId Where CourseName='数据库'And sc.StuID=st.StuId),企业管理=(SeIect Score From tbIScore sc Inner Join tbICourse cs Onsc.CourseId=cs.CourseId Where CourseName='企业管理'And sc.StuID=st.StuId),英语=(SeIect Score From tbIScore sc Inner Join tbICourse cs On sc.CourseId=cs.CourseId Where CourseName='英语'And sc.StuID=st.StuId),有效课程数=(SeIect Cou nt(Score) From tbIScore sc Inn er Joi n tbICourse cs Onsc.CourseId=cs.CourseId Where (CourseName='数据库'or CourseName='企业管理'or CourseName='英语')And sc.StuID=st.StuId),有效平均分=(Select Avg(Score) From tbIScore sc Inn er Joi n tbICourse cs Onsc.CourseId=cs.CourseId Where (CourseName='数据库'or CourseName='企业管理'or CourseName='英语')And sc.StuID=st.StuId)From tbIStude nt stOrder by有效平均分Desc--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分Select Courseld as 课程ID,最高分=(Select Max(Score) From tbIScore sc Wheresc.Courseld=cs.Courseld ),最低分=(Select Mi n(Score) From tblScore sc Where sc.Courseld=cs.Courseld ) FromtblCourse cs--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序(百分数后如何格式化为两位小数??)Select课程ID,平均分,及格率From(Select Courseld as 课程ID,平均分=(Select Avg(Score) From tblScore sc Wheresc.CourseId=cs.CourseId ),及格率=Convert(varchar(10),((Select Count(*) From tblScore sc Wheresc.CourseId=cs.CourseId And sc.Score>=60)*10000/(Select Count(*) From tblScore sc Wheresc.Courseld=cs.Courseld))/100)+'%'From tblScore cs) as tmpGroup by课程ID,平均分,及格率Order by 平均分,Convert(float,substring(及格率,1,len(及格率)-1)) Desc--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):企业管理(001),马克思(002), OO&UML ( 003),数据库(004)Select 课程ID=sc.CourseId,课程名称=cs.CourseName,平均成绩=Avg(Score),及格率=Convert(varchar(10),((Select Count(Score) From tblScore WhereCourseId=sc.CourseId And Score>=60)*10000/Cou nt(Score))/100.0)+'%'From tblScore scInner Joi n tblCourse cs ON sc.CourseId=cs.CourseIdWhere sc.Courseld like '00[1234]'Group By sc.CourseId,cs.CourseName--21、查询不同老师所教不同课程平均分从高到低显示Select 课程ID=CourseId,课程名称=CourseName,授课教师=TeaName,平均成绩=(SelectAvg(Score) From tblScore Where CourseId=cs.CourseId)From tblCourse csInner Join tblTeacher tc ON cs.TeaId=tc.TeaIdOrder by平均成绩Desc--22、查询如下课程成绩第3名到第6名的学生成绩单:企业管理( 001),马克思(002),UML ( 003),数据库(004)格式:[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩Select * From(Select Top 6 学生ID=StuId,学生姓名=StuName,企业管理=(Select Score From tblScore sc Inner Join tblCourse cs Onsc.CourseId=cs.CourseId Where CourseName='企业管理'And sc.StuID=st.StuId),马克思=(Select Score From tblScore sc Inner Join tblCourse cs Onsc.CourseId=cs.CourseId Where CourseName='马克思'And sc.StuID=st.StuId),UML=(Select Score From tblScore sc Inner Join tblCourse cs Onsc.CourseId=cs.CourseId Where CourseName='UML' A nd sc.StuID=st.StuId),数据库=(Select Score From tblScore sc Inner Join tblCourse cs Onsc.CourseId=cs.CourseId Where CourseName='数据库'And sc.StuID=st.StuId),平均成绩=(Select Avg(Score) From tbIScore sc Inner Join tbICourse cs On sc.Courseld=cs.Courseld Where (CourseName='数据库'or CourseName='企业管理'orCourseName='UML'or CourseName='马克思')And sc.StulD=st.Stuld),排名=Row_Number() Over(Order by(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On sc.Courseld=cs.Courseld Where (CourseName='数据库'or CourseName=' 企业管理'or CourseName='UML'or CourseName='马克思')And sc.StuID=st.StuId) DESC)From tblStude nt stOrder by 排名)as tmpWhere 排名between 3 And 6--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] Select 课程ID=CourseId,课程名称=CourseName,[100-85]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 85 And 100),[85-70]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 70 And 84),[70-60]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 60 And 69),[<60]=(Select Cou nt(*) From tblScore sc Where CourseId=cs.CourseId And Score <60)From tblCourse cs--24、查询学生平均成绩及其名次Select 学号=st.Stuld,姓名=StuName,平均成绩=sc.AvgScore,名次=(Dense_Rank() Over(Order by sc.AvgScore Desc)) From tblStude nt stInner Join (Select StuId,Avg(Score) as AvgScore From tblScore Group by Stuld) as sc On sc.StuId=st.StuIdOrder by 学号--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)Select 学号=Stuld,课程号=Courseld,分数=ScoreFrom(Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as tmp --得到一个临时的排名表,其中i表示编号Where i In(Select Top 3 i From (Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as t1 Where t1.CourseId=tmp.CourseId)--26、查询每门课程被选修的学生数Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct Stuld From tblScore Where CourseId=cs.CourseId) as tmp)From tblCourse cs--27、查询出只选修了一门课程的全部学生的学号和姓名Select 学号=Stuld,姓名=StuNameFrom tblStude nt stWhere (Select Count(*) From (Select Distinct Courseld From tblScore Where StuId=st.StuId) as tmp)=1--28、查询男生、女生人数Select 男生人数=(select Count(*) From tbIStudent Where StuSex='男'),女生人数=(select Count(*) From tblStudent Where StuSex='女')--29、查询姓“张”的学生名单Select * From tbIStudent Where StuName like '张%'--30、查询同名同性学生名单,并统计同名人数Select Distinct 学生姓名=StuName,同名人数=(Select Count(*) From tblStudent s2 Wheres2.StuName=st.StuName) From tblStude nt stWhere (Select Cou nt(*) From tblStude nt s2 Where s2.StuName=st.StuName)>=2--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)Select * From tblStude nt Where Year(Sage)=1981--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列Select 课程ID=Courseld,课程名称=CourseName,平均成绩=(Select Avg(Score) From tblScore Where CourseId=cs.CourseId)From tblCourse csOrder by 平均成绩,CourseId Desc--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩Select 学号=Stuld,姓名=StuName,平均成绩=(Select Avg(Score) From tblScore WhereStuId=st.StuId) From tblStude nt stWhere (Select Avg(Score) From tblScore Where Stuld=st.Stuld)>85--34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数Select 姓名=StuName,分数=Score From tblScore scInner Join tblStude nt st On sc.Stuld=st.StuldInner Join tblCourse cs On sc.CourseId=cs.CourseIdWhere CourseName='数据库'And Score<60--35、查询所有学生的选课情况;Select 学号=Stuld,选课数=(Select Count(*) From (Select Distinct Courseld From tblScore Where StuId=st.StuId) as tmp)From tblStude nt stSelect distinet 姓名=StuName,选修课程=CourseName From tblScore scInner Join tblStude nt st On sc.StuId=st.StuIdInner Join tblCourse cs On sc.CourseId=cs.CourseId--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;Select 姓名=StuName,课程名称=CourseName,分数=Score From tblScore scInner Join tblStude nt st On sc.StuId=st.StuIdInner Join tblCourse cs On sc.CourseId=cs.CourseIdWhere Score>=70--37、查询不及格的课程,并按课程号从大到小排列Select * From tblScore Where Score<60 order by Courseld Desc--38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;Select Stuld,StuName From tbIStude ntWhere StuId in(Select StuId From tblScore Where Courseld='003' A nd Score>=80)--39、求选了课程的学生人数Select 选了课程的学生人数=Cou nt(*) From tblStude nt st Where StuId IN (Select StuID From tblScore)--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩Select CourseId,CourseName,该科最高学生=(Select StuName From tblStudent Where StuId in (Select Top 1 StuID From tblScore Where Courseld=cs.Courseld Order by Score Desc)),成绩=(Select Top 1 Score From tblScore Where CourseId=cs.CourseId Order by Score Desc) From tblCourse cs Inner Join tblTeacher tc ON cs.Teald=tc.TealdWhere TeaName='叶平'--41、查询各个课程及相应的选修人数Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)From tblCourse cs--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩Select 学号=Stuld,课程号=Courseld,成绩=Score From tblScore scWhere Exists (Select * From tblScore Where Score=sc.Score And StuId=sc.StuId And Courseld <>sc.CourseId)Order by学号,成绩--43、查询每门功成绩最好的前两名Select 课程号=Courseld,第 1 名=(Select Top 1 StuId From tblScore Where CourseId=cs.CourseId Order by Score DESC),第 2 名=(Select Top 1 StulD From (Select Top 2 StuId,Score From tblScore Where CourseId=cs.CourseId Order by Score DESC) as tmp Order by Score)From tblCourse cs--44、统计每门课程的学生选修人数(超过10人的课程才统计)。

sql经典查询语句

sql经典查询语句

sql经典查询语句查询:select * from table1 where ⼯资>2500 and ⼯资<3000 //查找⼀个⼯资区间select 姓名 from table1 where 性别='0' and ⼯资='4000' //查找性别和⼯资的条件select * from table1 where not ⼯资= 3200 //查找⼯资不等于3200的select * from table1 order by ⼯资desc //将⼯资按照降序排列select * from table1 order by ⼯资 asc //将⼯资按照升序排列select * from table1 where year(出⾝⽇期)=1987 //查询table1 中所有出⾝在1987的⼈select * from table1 where name like '%张' /'%张%' /'张%' //查询1,⾸位字‘张’3,尾位字‘张’2,模糊查询select * from table1 order by money desc //查询表1按照⼯资的降序排列表1 (升序为asc)select * from table1 where brithday is null //查询表1 中出⾝⽇期为空的⼈select * into table2 from table3 //将表3中的所有数据转换成表2 (相当于复制)删库和建库use 数据库(aa) //使⽤数据库aacreate bb(数据库) //创建数据库bbcreate table table3 ( name varchar(10),sex varchar(2),money money, brithday datetime) //创建⼀个表3中有姓名,性别,⼯资,出⾝⽇期(此表说明有四列)insert into table3 values ('张三','男','2500','1989-1-5') //在表中添加⼀⾏张三的记录alter table table3 add tilte varchar(10) //向表3 中添加⼀列“title(职位)”alter table table3 drop column sex //删除table3中‘性别’这⼀列drop database aa //删除数据库aadrop table table3 //删除表3delete * from table3 //删除table3 中所有的数据,但table3这个表还在delete from table1 where 姓名='倪涛' and ⽇期 is nulldelete from table1 where 姓名='倪涛' and ⽇期='1971'更改库表的数据update table3 set money=money*1.2 //为表3所有⼈⼯资都增长20%update table3 set money=money*1.2 where title='经理' //为表3中“职位”是经理的⼈⼯资增长20%update table1 set ⼯资= 5000 where 姓名='孙⼋' //将姓名为孙⼋的⼈的⼯资改为5000update table1 set 姓名='敬光' where 姓名='倪涛' and 性别=1 //将性别为男和姓名为倪涛的⼈改为敬光经典查询语句之⼆1显⽰系部编号为03的系部名称Select departname From department Where departno=’03’2.查询系部名称中含有'⼯程'两个字的系部的名称。

mysql查询语句大全及用法

mysql查询语句大全及用法

mysql查询语句大全及用法MySQL是一种常用的关系型数据库管理系统,提供了强大的查询语言(SQL)来操作和管理数据库。

下面是一些常见的MySQL查询语句及其用法:1. SELECT语句:-用法:用于从数据库中检索数据。

-示例:SELECT * FROM 表名;(检索表中的所有数据)SELECT 列1, 列2 FROM 表名WHERE 条件;(根据条件检索指定列的数据)2. INSERT语句:-用法:用于向数据库中插入新的数据。

-示例:INSERT INTO 表名(列1, 列2) VALUES (值1, 值2);(向表中插入指定列的数据)3. UPDATE语句:-用法:用于更新数据库中的数据。

-示例:UPDATE 表名SET 列1 = 值1, 列2 = 值2 WHERE 条件;(根据条件更新指定列的数据)4. DELETE语句:-用法:用于从数据库中删除数据。

-示例:DELETE FROM 表名WHERE 条件;(根据条件删除数据)5. WHERE子句:-用法:用于在SELECT、UPDATE和DELETE语句中指定条件。

-示例:SELECT * FROM 表名WHERE 列= 值;(根据条件检索数据)6. ORDER BY子句:-用法:用于对结果进行排序。

-示例:SELECT * FROM 表名ORDER BY 列ASC/DESC;(根据指定列的升序或降序排序数据)7. GROUP BY子句:-用法:用于将结果分组。

-示例:SELECT 列, COUNT(*) FROM 表名GROUP BY 列;(根据指定列对数据进行分组并计数)8. JOIN语句:-用法:用于在多个表之间建立连接。

-示例:SELECT * FROM 表1 JOIN 表2 ON 表1.列= 表2.列;(根据指定列在两个表之间建立连接)9. DISTINCT关键字:-用法:用于返回唯一的结果。

-示例:SELECT DISTINCT 列FROM 表名;(返回指定列的唯一结果)10. LIMIT关键字:-用法:用于限制结果集的行数。

数据库SQL查询语句

数据库SQL查询语句

一、简单查询语句1. 查看表结构SQL>DESC emp;2. 查询所有列SQL>SELECT * FROM emp;3. 查询指定列SQL>SELECT empmo, ename, mgr FROM emp;SQL>SELECT DISTINCT mgr FROM emp; 只显示结果不同的项4. 查询指定行SQL>SELECT * FROM emp WHERE job='CLERK';5. 使用算术表达式SQL>SELECT ename,sal*13+nvl(comm,0) FROMemp;nvl(comm,1)的意思是,如果comm中有值,则nvl(comm,1)=comm; comm中无值,则nvl(comm,1)=0。

SQL>SELECT ename, sal*13+nvl(comm,0) year_sal FROM emp; (year_sal为别名,可按别名排序)SQL>SELECT * FROM emp WHERE hiredate>'01-1月-82';6. 使用like操作符(%,_)%表示一个或多个字符,_表示一个字符,[charlist]表示字符列中的任何单一字符,[^charlist]或者[!charlist]不在字符列中的任何单一字符。

SQL>SELECT * FROM emp WHERE ename like 'S__T%';7. 在where条件中使用InSQL>SELECT * FROM emp WHERE job IN ('CLERK','ANAL YST');8. 查询字段内容为空/非空的语句SQL>SELECT * FROMemp WHERE mgr IS/IS NOT NULL;9. 使用逻辑操作符号SQL>SELECT * FROM emp WHERE (sal>500 or job='MANAGE') and ename like 'J%';10. 将查询结果按字段的值进行排序SQL>SELECT * FROM emp ORDER BY deptno, sal DESC; (按部门升序,并按薪酬降序)二、复杂查询1. 数据分组(max,min,avg,sum,count)SQL>SELECT MAX(sal),MIN(age),A VG(sal),SUM(sal) from emp;SQL>SELECT * FROM emp where sal=(SELECT MAX(sal) from emp));SQL>SELEC COUNT(*) FROM emp;2. group by(用于对查询结果的分组统计)和having子句(用于限制分组显示结果)SQL>SELECT deptno,MAX(sal),A VG(sal) FROM emp GROUP BY deptno;SQL>SELECT deptno, job, A VG(sal),MIN(sal) FROM emp group by deptno,job having A VG(sal)<2000;对于数据分组的总结:a. 分组函数只能出现在选择列表、having、order by子句中(不能出现在where中)b. 如果select语句中同时包含有group by, having, order by,那么它们的顺序是group by, having, order by。

数据库SQL查询语句大全

数据库SQL查询语句大全

经典SQL查询语句大全一、基础1、说明:创建数据库CREATE DATABASE database-name2、说明:删除数据库drop database dbname3、说明:备份sql server--- 创建备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.d at'--- 开始备份BACKUP DATABASE pubs TO testBack4、说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)根据已有的表创建新表:A:create table tab_new like tab_old (使用旧表创建新表)B:create table tab_new as select col1,col2… from tab_old d efinition only5、说明:删除新表drop table tabname6、说明:增加一个列Alter table tabname add column col type注:列增加后将不能删除。

DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

7、说明:添加主键:Alter table tabname add primary key(col)说明:删除主键: Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….)删除索引:drop index idxname注:索引是不可更改的,想更改必须删除重新建。

常用sql查询语句大全

常用sql查询语句大全

常用sql查询语句大全常用SQL查询语句大全SQL (Structured Query Language) 是一种用于管理关系型数据库的编程语言,它可以用来从数据库中检索、插入、更新和删除数据。

以下是一些常用的SQL查询语句:1. SELECT语句:用于检索数据库中的数据。

- SELECT * FROM table_name; -- 检索表中的所有列和行- SELECT column1, column2 FROM table_name; -- 检索表中指定的列- SELECT column1, column2 FROM table_name WHERE condition; -- 检索满足条件的行2. INSERT语句:用于向数据库中插入新的数据。

- INSERT INTO table_name (column1, column2) VALUES (value1, value2); -- 插入指定的列和值- INSERT INTO table_name VALUES (value1, value2); -- 插入所有列和值3. UPDATE语句:用于更新数据库中的数据。

- UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition; -- 更新满足条件的行的值4. DELETE语句:用于从数据库中删除数据。

- DELETE FROM table_name WHERE condition; -- 删除满足条件的行5. WHERE子句:用于指定条件。

- SELECT * FROM table_name WHERE column_name = value; -- 检索满足条件的行- SELECT * FROM table_name WHERE column_name LIKE 'value%'; -- 使用通配符进行模糊匹配- SELECT * FROM table_name WHERE column_name IN (value1, value2); -- 检索列值在指定列表中的行6. ORDER BY子句:用于对结果进行排序。

SQL查询经典例题

SQL查询经典例题

SQL查询经典例题通过以下习题的练习,我们能快速熟悉掌握sql语句查询的语法和要领,大家要用心领会其中的要领和步骤,要学会分析步骤。

一、单表查询练习1、查询<学生信息表>,查询学生"张三"的全部基本信息Select *from A_studentinfowhere sname='张三'2、查询<学生信息表>,查询学生"张三"和”李四”的基本信息Select *from A_studentinfowhere sname='张三'or sname='李四'3、查询<学生信息表>,查询姓"张"学生的基本信息Select *from A_studentinfowhere sname like '张%'4、查询<学生信息表>,查询姓名中含有"四"字的学生的基本信息Select *from A_studentinfowhere sname like '%四%'5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。

select *from A_studentinfowhere sname like '李_强'6、查询<学生信息表>,查询姓"张"或者姓”李”的学生的基本信息。

Select *from A_studentinfowhere sname like '张%'or sname like '李%'7、查询<学生信息表>,查询姓"张"并且"所属省份"是"北京"的学生信息Select *from A_studentinfowhere sname like '张%'and province='北京'8、查询<学生信息表>,查询"所属省份"是"北京"、”新疆”、”山东”或者"上海"的学生的信息Select *from A_studentinfowhere province in ('北京','上海','新疆','山东')9、查询<学生信息表>,查询姓"张",但是"所属省份"不是"北京"的学生信息Select *from A_studentinfowhere sname like '张%'and province !='北京'10、查询<学生信息表>,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所属省份相同的情况下再按照“班级”排序select *from A_studentinfoorder by sex,province,class11、查询<学生信息表>,查询现有学生都来自于哪些不同的省份select distinct province as省份from A_studentinfo12、查询<学生选修信息表>,查询没有填写成绩的学生的学号、课程号和成绩Select *where score is null13、查询<学生选修信息表>,查询全部填写了成绩的学生的选修信息,并按照“成绩”从高到低进行排序Select *from A_studentcoursewhere score is not nullorder by score desc二、聚合函数练习1、统计<学生信息表>,统计共有多少个学生Select count (*) as学生数量from A_studentinfo2、统计<学生信息表>,统计年龄大于20岁的学生有多少个Select count(*) as学生数量from A_studentinfowhere (2008-yearofbirth)>203、统计<学生信息表>,统计入学时间在1980年至1982年的学生人数select count(*) as学生数量from A_studentinfowhere enrollment between '1998-01-01' and '2003-12-30' 对比以下查询方式,看看有何不同,为什么?select count(*) as学生数量from A_studentinfowhere enrollment between '1998' and '2003'4、统计<学生选修信息表>,统计学号为"S001"的学生的平均成绩Select avg(score) as平均成绩from A_studentcoursewhere sno='S001'5、统计<学生选修信息表>,统计学号为"S001"的学生的总成绩select sum(score) as总成绩where sno ='S001'6、统计<学生选修信息表>,查询课程号为”C001”的课程的最高成绩select max(score) as最高成绩from A_studentcoursewhere cno='C001'7、统计<学生信息表>,查询所有学生中的最大年龄是多少select 2008-min(yearofbirth) as最大年龄from A_studentinfo三、分组查询练习1、统计<学生选修信息表>,统计每个课程的选修人数select cno,count(*) as学生数量from A_studentcoursegroup by cno2、统计<学生选修信息表>,统计每个同学的总成绩select sno,sum(score) as总成绩from A_studentcoursegroup by sno3、统计<学生信息表>,统计每个班级中每种性别的学生人数,并按照班级排序select class as班级,sex as性别, count(*) as人数from A_studentinfogroup by class,sexorder by class4、统计<学生选修信息表>,统计每门课程的平均成绩,并按照成绩降序排序Select cno,avg(score) as平均成绩from A_studentcoursegroup by cnoorder by avg(score) desc5、统计<学生选修信息表>,显示有两门以上课程不及格的学生的学号Select sno as不及格学生学号from A_studentcoursewhere score<60group by snohaving count(*)>16、统计<学生信息表>,统计每个班级中的最大年龄是多少select class as班级, 2008-min(yearofbirth) as最大年龄from A_studentinfogroup by class四、嵌套查询练习1、用子查询实现,查询选修“高等数学”课的全部学生的总成绩select sum(score) as高等数学总成绩from A_studentcoursewhere cno =(select cnofrom A_courseinfowhere subject='高等数学')2、用子查询实现,统计<学生选修信息表>,显示学号为"S001"的学生在其各科成绩中,最高分成绩所对应的课程号和成绩select score,cnofrom A_studentcoursewhere sno='S001'and score =(select max(score)from A_studentcoursewhere sno ='S001')思考:如果该学号学生有两个课程分数都为最高的100分,查询会有什么结果3、用子查询实现,查询2班选修"数据库技术"课的所有学生的成绩之和select sum(score) as数据库技术总成绩from A_studentcoursewhere cno =(select cnofrom A_courseinfowhere subject='数据库技术')and sno in(select snofrom A_studentinfowhere class='2')4、用子查询实现,查询3班"张三"同学的"测试管理"成绩select scorefrom A_studentcoursewhere cno=(select cnofrom A_courseinfowhere subject='测试管理')and sno in(select snofrom A_studentinfowhere class='3'and sname='张三')五、联接查询练习1、查询"张三"的各科考试成绩,要求显示姓名、课程号和成绩select sname as姓名,cno as课程号,score as成绩from A_studentinfo,A_studentcoursewhere A_studentinfo.sno=A_studentcourse.snoand sname='张三'2、查询"张三"的各科考试成绩中,哪科没有记录考试成绩,要求显示姓名、课程号和成绩select sname as姓名,cno as课程号,score as成绩from A_studentinfo,A_studentcoursewhere A_studentinfo.sno=A_studentcourse.snoand sname='张三'and score is null3、查询"张三"的各门课程成绩,要求显示姓名、课程名称和成绩select sname as姓名,subject as课程名称,score as成绩from A_studentinfo,A_courseinfo,A_studentcoursewhere A_studentcourse.sno=A_studentinfo.snoandA_/doc/4a1283234.html,o=A_http://www. /doc/4a1283234.html,oand A_studentinfo.sname='张三'4、查询3班"张三"的"测试管理"成绩,要求显示姓名、成绩select sname as姓名,score as成绩from A_studentcourse,A_courseinfo,A_studentinfowhereA_/doc/4a1283234.html,o=A_http://www. /doc/4a1283234.html,oand A_studentcourse.sno=A_studentinfo.snoand subject='测试管理'and class='3' and sname='张三'。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

--一个题目涉及到的50个Sql语句--(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID)------------------------------------表结构----------------------------------------学生表tblStudent(编号StuId、姓名StuName、年龄StuAge、性别StuSex)--课程表tblCourse(课程编号CourseId、课程名称CourseName、教师编号TeaId)--成绩表tblScore(学生编号StuId、课程编号CourseId、成绩Score)--教师表tblTeacher(教师编号TeaId、姓名TeaName)-----------------------------------------------------------------------------------1、查询“001”课程比“002”课程成绩高的所有学生的学号;Select StuId From tblStudent s1Where (Select Score From tblScore t1 Where t1.StuId=s1.stuId And t1.CourseId='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002')--2、查询平均成绩大于60分的同学的学号和平均成绩;Select StuId,Avg(Score) as AvgScore From tblScoreGroup By StuIdHaving Avg(Score)>60--3、查询所有同学的学号、姓名、选课数、总成绩;Select StuId,StuName,SelCourses=(Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId),SumScore=(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId)From tblStudent s1--4、查询姓“李”的老师的个数;Select Count(*) From tblTeacher Where TeaName like '李%'--5、查询没学过“叶平”老师课的同学的学号、姓名;Select StuId,StuName From tblStudentWhere StuId Not In(Select StuID From tblScore scInner Join tblCourse cu ON sc.CourseId=cu.CourseIdInner Join tblTeacher tc ON cu.TeaId=tc.TeaIdWhere tc.TeaName='叶平')--6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;Select StuId,StuName From tblStudent stWhere (Select Count(*) From tblScore s1 Where s1.StuId=st.StuId Ands1.CourseId='001')>0And(Select Count(*) From tblScore s2 Where s2.StuId=st.StuId And s2.CourseId='002')>0--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;Select StuId,StuName From tblStudent st Where not exists(Select CourseID From tblCourse cu Inner Join tblTeacher tc On cu.TeaID=tc.TeaID Where tc.TeaName='叶平' And CourseID not in(Select CourseID From tblScore Where StuID=st.StuID))--8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;Select StuId,StuName From tblStudent s1Where (Select Score From tblScore t1 Where t1.StuId=s1.stuId And t1.CourseId='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002')--9、查询所有课程成绩小于60分的同学的学号、姓名;Select StuId,StuName From tblStudent stWhere StuId Not IN(Select StuId From tblScore sc Where st.StuId=sc.StuId And Score>60)--10、查询没有学全所有课的同学的学号、姓名;Select StuId,StuName From tblStudent stWhere (Select Count(*) From tblScore sc Where st.StuId=sc.StuId)<(Select Count(*) From tblCourse)--11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;------运用连接查询Select DistInct st.StuId,StuName From tblStudent stInner Join tblScore sc ON st.StuId=sc.StuIdWhere sc.CourseId IN (Select CourseId From tblScore Where StuId='1001') ------嵌套子查询Select StuId,StuName From tblStudentWhere StuId In(Select Distinct StuId From tblScore Where CourseId In (Select CourseId From tblScore Where StuId='1001')) //一门课相同就可以查出来了--12、查询至少学过学号为“1001”同学所有课程的其他同学学号和姓名;Select StuId,StuName From tblStudentWhere StuId In(Select Distinct StuId From tblScore Where CourseId Not In (Select CourseId From tblScore Where StuId='1001')--13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;(从子查询中获取父查询中的表名,这样也行)--创建测试表Select * Into Sc From tblScoregoUpdate Sc Set Score=(Select Avg(Score) From tblScore s1 Where s1.CourseId=sc.CourseId) Where CourseId IN(Select CourseId From tblCourse cs INNER JOIN tblTeacher tc ON cs.TeaID=tc.TeaID WHERE TeaName ='叶平')--14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;Select StuID,StuName From tblStudent stWhere StuId <> '1002'AndNot Exists(Select * From tblScore sc Where sc.StuId=st.StuId And CourseId Not In (Select CourseId From tblScore Where StuId='1002'))AndNot Exists(Select * From tblScore Where StuId='1002' And CourseId Not In (Select CourseId From tblScore sc Where sc.StuId=st.StuId))--15、删除学习“叶平”老师课的SC表记录;Delete From tblScore Where CourseId IN(Select CourseId From tblCourse cs INNER JOIN tblTeacher tc ON cs.TeaId=tc.TeaId Where tc.TeaName='叶平')--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、'002'号课的平均成绩;Insert Into tblScore (StuId,CourseId,Score)Select StuId,'002',(Select Avg(Score) From tblScore Where CourseId='002') From tblScore WhereStuId Not In (Select StuId From tblScore Where CourseId='003')--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分Select StuId,数据库=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='数据库' And sc.StuID=st.StuId),企业管理=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='企业管理' And sc.StuID=st.StuId),英语=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='英语' And sc.StuID=st.StuId),有效课程数=(Select Count(Score) From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='英语') And sc.StuID=st.StuId),有效平均分=(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='英语') And sc.StuID=st.StuId)From tblStudent stOrder by 有效平均分Desc--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分Select CourseId as 课程ID, 最高分=(Select Max(Score) From tblScore sc Where sc.CourseId=cs.CourseId ),最低分=(Select Min(Score) From tblScore sc Where sc.CourseId=cs.CourseId )From tblCourse cs--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序(百分数后如何格式化为两位小数??)Select 课程ID,平均分,及格率From(Select CourseId as 课程ID, 平均分=(Select Avg(Score) From tblScore sc Where sc.CourseId=cs.CourseId ),及格率=Convert(varchar(10),((Select Count(*) From tblScore sc Where sc.CourseId=cs.CourseId And sc.Score>=60)*10000/(Select Count(*) From tblScore sc Where sc.CourseId=cs.CourseId))/100)+'%'From tblScore cs) as tmpGroup by 课程ID,平均分,及格率Order by 平均分, Convert(float,substring(及格率,1,len(及格率)-1)) Desc--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)Select 课程ID=sc.CourseId,课程名称=cs.CourseName,平均成绩=Avg(Score) ,及格率=Convert(varchar(10),((Select Count(Score) From tblScore Where CourseId=sc.CourseId And Score>=60)*10000/Count(Score))/100.0)+'%'From tblScore scInner Join tblCourse cs ON sc.CourseId=cs.CourseIdWhere sc.CourseId like '00[1234]'Group By sc.CourseId,cs.CourseName--21、查询不同老师所教不同课程平均分从高到低显示Select 课程ID=CourseId,课程名称=CourseName,授课教师=TeaName,平均成绩=(Select Avg(Score) From tblScore Where CourseId=cs.CourseId)From tblCourse csInner Join tblTeacher tc ON cs.TeaId=tc.TeaIdOrder by 平均成绩Desc--22、查询如下课程成绩第3 名到第6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)格式:[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩Select * From(Select Top 6 学生ID=StuId,学生姓名=StuName,企业管理=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='企业管理' And sc.StuID=st.StuId),马克思=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='马克思' And sc.StuID=st.StuId),UML=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='UML' And sc.StuID=st.StuId),数据库=(Select Score From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where CourseName='数据库' And sc.StuID=st.StuId),平均成绩=(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='UML'or CourseName='马克思') And sc.StuID=st.StuId),排名=Row_Number() Over(Order by(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='UML'or CourseName='马克思') And sc.StuID=st.StuId) DESC)From tblStudent stOrder by 排名) as tmpWhere 排名between 3 And 6--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] Select 课程ID=CourseId, 课程名称=CourseName,[100-85]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 85 And 100),[85-70]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 70 And 84),[70-60]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 60 And 69),[<60]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score <60)From tblCourse cs--24、查询学生平均成绩及其名次Select 学号=st.StuId, 姓名=StuName,平均成绩=sc.AvgScore,名次=(Dense_Rank() Over(Order by sc.AvgScore Desc)) From tblStudent stInner Join (Select StuId,Avg(Score) as AvgScore From tblScore Group by StuId) as sc On sc.StuId=st.StuIdOrder by 学号--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)Select 学号=StuId,课程号=CourseId,分数=ScoreFrom(Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as tmp --得到一个临时的排名表,其中i表示编号Where i In(Select Top 3 i From (Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as t1 Where t1.CourseId=tmp.CourseId)--26、查询每门课程被选修的学生数Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)From tblCourse cs--27、查询出只选修了一门课程的全部学生的学号和姓名Select 学号=StuId,姓名=StuNameFrom tblStudent stWhere (Select Count(*) From (Select Distinct CourseId From tblScore Where StuId=st.StuId) as tmp)=1--28、查询男生、女生人数Select 男生人数=(select Count(*) From tblStudent Where StuSex='男'),女生人数=(select Count(*) From tblStudent Where StuSex='女')--29、查询姓“张”的学生名单Select * From tblStudent Where StuName like '张%'--30、查询同名同性学生名单,并统计同名人数Select Distinct 学生姓名=StuName,同名人数=(Select Count(*) From tblStudent s2 Where s2.StuName=st.StuName) From tblStudent stWhere (Select Count(*) From tblStudent s2 Where s2.StuName=st.StuName)>=2--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) Select * From tblStudent Where Year(Sage)=1981--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列Select 课程ID=CourseId,课程名称=CourseName,平均成绩=(Select Avg(Score) From tblScore Where CourseId=cs.CourseId)From tblCourse csOrder by 平均成绩,CourseId Desc--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩Select 学号=StuId,姓名=StuName,平均成绩=(Select Avg(Score) From tblScore Where StuId=st.StuId) From tblStudent stWhere (Select Avg(Score) From tblScore Where StuId=st.StuId)>85--34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数Select 姓名=StuName,分数=Score From tblScore scInner Join tblStudent st On sc.StuId=st.StuIdInner Join tblCourse cs On sc.CourseId=cs.CourseIdWhere CourseName='数据库' And Score<60--35、查询所有学生的选课情况;Select 学号=StuId,选课数=(Select Count(*) From (Select Distinct CourseId From tblScore Where StuId=st.StuId) as tmp)From tblStudent stSelect distinct 姓名=StuName,选修课程=CourseName From tblScore scInner Join tblStudent st On sc.StuId=st.StuIdInner Join tblCourse cs On sc.CourseId=cs.CourseId--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;Select 姓名=StuName,课程名称=CourseName,分数=Score From tblScore scInner Join tblStudent st On sc.StuId=st.StuIdInner Join tblCourse cs On sc.CourseId=cs.CourseIdWhere Score>=70--37、查询不及格的课程,并按课程号从大到小排列Select * From tblScore Where Score<60 order by CourseId Desc--38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;Select StuId,StuName From tblStudentWhere StuId in(Select StuId From tblScore Where CourseId='003' And Score>=80)--39、求选了课程的学生人数Select 选了课程的学生人数=Count(*) From tblStudent st Where StuId IN (Select StuID From tblScore)--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩Select CourseId,CourseName,该科最高学生=(Select StuName From tblStudent Where StuId in (Select Top 1 StuID From tblScore Where CourseId=cs.CourseId Order by Score Desc)),成绩=(Select Top 1 Score From tblScore Where CourseId=cs.CourseId Order by ScoreDesc)From tblCourse cs Inner Join tblTeacher tc ON cs.TeaId=tc.TeaIdWhere TeaName='叶平'--41、查询各个课程及相应的选修人数Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)From tblCourse cs--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩Select 学号=StuId, 课程号=CourseId, 成绩=Score From tblScore scWhere Exists (Select * From tblScore Where Score=sc.Score And StuId=sc.StuId And CourseId <>sc.CourseId)Order by 学号,成绩--43、查询每门功成绩最好的前两名Select 课程号=CourseId,第1名=(Select Top 1 StuId From tblScore Where CourseId=cs.CourseId Order by Score DESC),第2名=(Select Top 1 StuID From (Select Top 2 StuId,Score From tblScore Where CourseId=cs.CourseId Order by Score DESC) as tmp Order by Score)From tblCourse cs--44、统计每门课程的学生选修人数(超过10人的课程才统计)。

相关文档
最新文档