SQL查詢二之分組統計


  分組必統計,分組查詢其實是排序
 1
--使用in查詢信息工程系和電子商務系的學生 2 3 --查詢信息工程系和電子商務系的學生 4 5 select * from student where stuDept='信息工程系' or stuDept='電子商務系' 6 7 select * from student where stuDept in('電子商務系','信息工程系') 8 9 10 select * from student 11 12 --使用count函數查詢全體學生的人數 13 14 select count(stuId) as 人數 from student 15 16 select count(*) as 人數 from student 17 18 19 /********************************/ 20 --分組必統計 21 --使用group分組查詢各系學生的數量 22 23 --男生女生各多少人 24 25 26 27 select * from student 28 29 select stuSex, max(stuAvgrade) from student 30 group by stuSex 31 32 --查詢男生和女生都有誰:(分組查詢信息-都有誰-:是排序不是分組) 33 select stuSex, * from student 34 order by student.stuSex 35 36 --各系學生的數量 37 select stuDept,count(*) as 人數 from student 38 group by stuDept 39 40 select * from student 41 42 計算機系 男 3 43 計算機系 男 1 44 電子商務系 男 1 45 電子商務系 女 1 46 47 48 --各系男生女生各多少人 49 select stuDept, stuSex,count(*) as 人數 from student 50 group by stuDept,stuSex 51 52 53 select stuDept, stuSex,count(*) as 人數 from student 54 group by stuDept, stuSex 55 56 --各系學生總分數 57 58 59 select stuDept, sum(stuAvgrade) as 總成績 from student 60 group by stuDept 61 62 --每個系的成績最好的 63 select stuDept, Max(stuAvgrade) as 最好的一個 from student 64 group by stuDept 65 66 select stuDept, Min(stuAvgrade) as 最差勁的一個 from student 67 group by stuDept 68 69 70 select stuDept, avg(stuAvgrade) as 平均 from student 71 group by stuDept 72 73 select * from student 74 75 --統計各系的男生和女生各多少人 76 select stuDept,stuSex,COUNT(*) from student 77 group by stuDept, stuSex 78 order by stuDept --order by 排序 79 80 81 --查詢各系學生信息 82 83 84 85 select * from student 86 group by stuDept,stuId,stuName 87 88 select stuDept, stuName, stuSex, stuBirth, stuSpeciality, stuAvgrade from student 89 group by stuDept, stuName, stuSex, stuBirth, stuSpeciality, stuAvgrade --這樣寫是可以的,其實組到最后,會發現等同於select * from student,也就是過分分組等於沒有分組 90 91 --查詢各系學生的信息,不是分組,因為分組必統計,這里其實是按系進行排序的概念 92 select student.stuDept, student.* from student 93 order by student.stuDept 94 95 --查詢每個系的各專業的學生人數 96 select stuDept, stuSpeciality, count(*) from student 97 group by stuDept, stuSpeciality 98 99 --查詢每個系的各專業的最好成績 100 101 102 103 select stuDept, stuSpeciality, max(stuAvgrade) from student 104 group by stuDept, stuSpeciality 105 106 107 108 109 select stuDept, stuName, stuSex, stuBirth, stuSpeciality, stuAvgrade from student 110 order by stuDept -- order by 是排序關鍵字 dian, ji, xin 111 112 select * from student order by stuAvgrade Desc --desc是降序,默認值是Asc 113 114 select * from student order by stuAvgrade Asc 115 116 117 --使用having子句查詢人數大於2的系 118 119 --查詢人數大於2的系 120 121 --select stuDept from student where count(*) > 2 122 123 124 --//where子句是用於分組前的條件篩選// 125 select stuDept from student 126 where count(*) > 2 127 group by stuDept --非法,where條件部分不能有聚合函數 128 129 --select stuDept from student where count(*) > 2 group by stuDept 這樣的寫法是我們很自然就想到的,但是是非法,因為在Sql中不能在where條件后使用有計算的表達式,如聚合函數 130 131 132 --//having子句用於分組后的篩選 133 select stuDept, count(*) as 人數 from student 134 group by stuDept 135 having count(*) >= 2 136 137 select * from student 138 139 140 141 142 --查詢人數大於1的系並且,不能是計算機系 143 144 --能在分組前的where子句中篩選的就一定要放在where子句中 145 select stuDept from student 146 group by stuDept 147 having count(*) >= 2 and stuDept <> '計算機系' 148 149 select stuDept from student 150 where stuDept <> '計算機系' 151 group by stuDept 152 having count(*) >= 2 153 154 155 156 157 select stuDept as 系, count(*) as 人數, sum(stuAvgrade) as 總成績, avg(stuAvgrade) as 平均成績, max(stuAvgrade) as 最好成績 from student 158 group by stuDept 159 having count(*) > 2 160 161 --查詢平均成績大於全體學生平均成績的學生的信息 162 163 164 select * from student 165 where stuAvgrade > ( 166 select AVG(stuAvgrade) from student 167 )

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM