1. 程式人生 > >sqlserver 模糊查詢,連表,聚合函數,分組

sqlserver 模糊查詢,連表,聚合函數,分組

server min inner name 連表 sum col core sqlserve

 1 use StudentManageDB
 2 go
 3 select StudentName,StudentAddress from Students
 4 where StudentAddress like 天津%
 5 
 6 select StudentName,StudentAddress from Students
 7 where StudentName like %小%
 8 
 9 select * from ScoreList
10 where CSharp between 80 and 90
11 
12 select StudentName,StudentAddress,Birthday from
Students 13 where Birthday between 1987-01-01 and 1988-01-01 14 15 select StudentName,StudentAddress,age from Students 16 where Age in(21,22,23) 17 18 select StudentName,StudentAddress,age from Students 19 where StudentName in(王小虎,賀小張) 20 21 22 select SUM(CSharp) as C#總成績 from ScoreList 23 24
select 總人數=COUNT(*) from Students 25 26 select MAX(Csharp) as C#最高分 ,MIN(CSharp) as C#最低分,AVG(CSharp) as C#最低分 from ScoreList 27 28 select Students.StudentId,C#成績=CSharp,StudentName,ClassName 29 from ScoreList 30 inner join Students on Students.StudentId=ScoreList.StudentId 31 inner join StudentClass on
Students.ClassId=StudentClass.ClassId 32 where CSharp >80 33 34 select Students.StudentId,StudentName,Gender ,C#成績=CSharp from Students 35 left outer join ScoreList on Students.StudentId=ScoreList.StudentId 36 where Gender= 37 38 select Students.StudentId,StudentName,Gender ,C#成績=CSharp from ScoreList 39 left outer join Students on Students.StudentId=ScoreList.StudentId 40 where Gender= 41 42 select 班級=StudentClass.ClassName,人數=COUNT(*),C#最高分=Max(CSharp),DB最高分=MAX(SQLServerDB), 43 AVG(CSharp) as C#平均分,AVG(SQLServerDB) as DB平均分 44 from Students 45 inner Join StudentClass on Students.ClassId =StudentClass.ClassId 46 inner join ScoreList on ScoreList.StudentId=Students.StudentId 47 group by ClassName 48 having AVG(CSharp)>=70 and AVG(SQLServerDB)>=70 49 50 select * from ScoreList 51 select StudentId from ScoreList group by StudentId having COUNT(*)>1 52 53 select * from ScoreList 54 where StudentId in(select StudentId from ScoreList group by StudentId having COUNT(*)>1) 55 order by StudentId 56 57 select * from ScoreList 58 where (select COUNT(*) from ScoreList s where s.StudentId=ScoreList.StudentId)>1 59 order by StudentId

sqlserver 模糊查詢,連表,聚合函數,分組