select st.[Name],c1.Score,c2.Score from (select sc.[Score], sc.StudentID from Score sc where sc.[CourseID]=1)c1, (select sc.[Score],sc.StudentID from Score sc where sc.[CourseID]=2)c2 join Student st on st.[ID]= c2.[StudentID] where c1.[Score]>c2.[Score] and c1.[StudentID]=c2.[StudentID]
-- 与上面sql执行结果相同 select stu.[Name],c1.Score,c2.Score from (select sc.[Score], sc.StudentID from Score sc where sc.[CourseID]=1)c1, (select sc.[Score],sc.StudentID from Score sc where sc.[CourseID]=2)c2,student stu WHERE c1.[StudentID]=c2.[StudentID] AND c1.[Score]>c2.[Score] AND stu.ID=c1.StudentID
2. 查询平均成绩大于60分的同学的学号和平均成绩
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。 GROUP BY后面不能接WHERE条件,使用HAVING代替
先以学生ID分组,然后求每个学生的平均成绩。
1 2
SELECT StudentID,AVG(stu.score) FROM [dbo].[Score] AS stu GROUPBY StudentID HAVINGAVG(stu.score)>60
3. 查询所有同学的学号、姓名、选课数、总成绩;
成绩表分组统计总成绩,课程数目,然后连表查姓名。
1 2 3 4 5 6 7 8 9 10
SELECT sc1.StudentID,sc.NAME,counts,sums FROM Student sc JOIN ( SELECT StudentID AS StudentID,COUNT(CourseID) AS counts,SUM(score) sums FROM Score stu GROUPBY StudentID ) as sc1 ON sc1.StudentID= sc.ID
--不显示姓名可以用一下sql SELECT stu.ID ,COUNT(sc.CourseID),SUM(score) FROM Student stu LEFTOUTERJOIN Score sc ON sc.StudentID= stu.ID GROUPBY stu.ID
selectcount(t.ID) from Teacher t where t.Name like'张%'
SQL LIKE子句使用通配符运算符比较相似的值。符合LIKE操作符配合使用2个通配符:
百分号 (%):百分号代表零个,一个或多个字符
下划线 (_):下划线表示单个数字或字符
5. 找出教师表中姓名重复的数据,然后删除多余重复的记录,只留ID小的那个。
1 2 3 4 5 6 7
-- 首先找到重复姓名的 select t.Name,count(t.Name) from Teacher t groupby t.[Name] havingcount(t.Name)>1
deletefrom Teacher where Name in (select t2.Name from Teacher t2 groupby t2.[Name] havingcount(t2.Name)>1) and ID notin (selectmin(t3.ID) from Teacher t3 groupby t3.Name havingcount(t3.Name)>1)
select st.Name,c.Name,sc.Score,(case when sc.Score >80then'优' when sc.Score <60then'不及格' else'良'end) as'Remark' from Score sc innerjoin Student st on st.ID=sc.[StudentID] innerjoin Course c on c.ID=sc.[CourseID] ORDERBY st.Name
7. 查询所有课程成绩小于60分的同学的学号、姓名信息
1 2 3 4 5 6 7 8
-- 先查询小于60分的学生,然后去重 selectdistinct st.*from Student st leftjoin Score sc on sc.[StudentID]=st.ID where sc.[Score]<60
-- 查询小于60分的学生作为子查询; select*FROM Student st WHERE st.ID in (SELECT s1.ID FROM Student s1,Score s2 WHERE s1.ID=s2.[StudentID] AND s2.Score<60)
8. 查询各科成绩最高和最低的分:以如下形式显示:课程名称,最高分,最低分
1 2 3 4 5 6
SELECT cou.NAME AS 课程名称,maxsc 最高分,minsc 最低分 FROM [dbo].[Course] cou JOIN ( SELECT [CourseID],MAX(sc.score) maxsc ,MIN(sc.score) minsc FROM Score sc,[dbo].[Student] stu groupBY [CourseID] ) tb1 ON cou.id=tb1.[CourseID]
9. 查询不同老师所教不同课程平均分从高到低显示
1 2 3 4 5 6 7
SELECT te.NAME,cu.NAME,cu.TeacherID,sc1.avgsc FROM [dbo].[Course] cu JOIN ( SELECT sc.[CourseID] ,AVG(sc.Score) avgsc FROM Score sc,[dbo].[Teacher] te GROUPBY sc.[CourseID] ) sc1 ON sc1.[CourseID]= cu.ID JOIN [dbo].Teacher te ON te.id= cu.[TeacherID] ORDERBY avgsc desc
10. 查询和“7”号的同学学习的课程完全相同的其他同学学号和姓名
1 2 3 4 5 6 7
SELECT stu.id,stu.Name FROM Student stu JOIN ( select s1.StudentID from Score s1 where s1.CourseID IN (select s2.CourseID from Score s2 where s2.StudentID=7) groupby s1.StudentID havingcount(*)=(selectcount(*) from Score s3 where s3.StudentID=7) ) sc ON stu.id=sc.studentid
11. 查询选修“张老师”老师所授课程的学生中,成绩最高的学生姓名及其成绩
1 2 3 4 5
SELECT cou.NAME,te.NAME,sc.score,sc.StudentID,stu.name FROM [dbo].[Course] cou,[dbo].[Teacher] te,[dbo].[Score] sc,student stu where te.name='张老师'AND cou.TeacherID=te.ID AND cou.id=sc.courseid AND sc.score= (SELECTMAX(score) FROM [dbo].[Score] sc1 WHERE sc1.[CourseID]=cou.ID) AND stu.id =sc.StudentID
12. 查询所有成绩第二名到第四名的成绩
1 2 3 4 5 6
-- sqlite 分页 select s.[StudentID],s.Score from Score s orderby s.Score desc limit 2offset2 -- SQL 2005/2008中的分页函数是ROW_NUMBER() Over (Order by 列...)-- select t.[StudentID],t.Score from( select s2.[StudentID],s2.Score,ROW_NUMBER() OVER (ORDERBY s2.[Score]) AS rn from Score s2) t where t.rn>=2and t.rn<=4
12. 查询各科成绩前2名的记录:(不考虑成绩并列情况)
1 2 3 4 5 6 7
select*from Score s1 where s1.Score in(select s2.Score from Score s2 where s1.[CourseID]=s2.[CourseID] orderby s2.Score desc limit 2offset0) orderby s1.[CourseID],s1.[Score] desc -- 上面是sqlite中的语法,sqlite中没有top,使用limit代替,效果是一样的 -- select*from Score s1 where s1.Score in(select Top 2 s2.Score from Score s2 where s1.[CourseID]=s2.[CourseID] orderby s2.Score desc) orderby s1.[CourseID],s1.[Score] desc
表A,表B,使用表B中的Name字段更新表A中的Name字段,其中Id字段相关联
1 2 3 4 5 6 7
update b set ClientName = a.name from a,b where a.id = b.id
--oracle update b set (ClientName) = (SELECT name FROM a WHERE b.id = a.id)
--mysql UPDATE A, B SET A1 = B1, A2 = B2, A3 = B3 WHERE A.ID = B.ID