1. 用一條SQL 語句 查詢出每門課都大於80 分的學生姓名
name kecheng fenshu
張三 語文 81
張三 數學 75
張三 英語 78
李四 語文 76
李四 數學 90
王五 語文 81
王五 數學 100
王五 英語 90
每門課大於80分就是語數英的分數都過80分,
如果不考慮學生的課程少錄入情況(比如張三只有2個課程,王五有3個課程)
select name from ims_ewei_score group by name having min(fenshu)>80;
如果考慮學生的課程數大於等於3的情況
select name from ims_ewei_score group by name having count(kecheng) >=3 and min(fenshu)>80; //按照name 分組 group by 分組后再having 過濾
查詢去重,查詢的數據不顯示重復的
select distinct name from ims_ewei_score ;
select name from ims_ewei_score group by name;
如下圖,8、13條數據重復刪除13條數據
delete from ims_ewei_score where id not in (
select tmp.id from
(select min(id) as id from ims_ewei_score group by name,kecheng,fenshu)
as temp
);
/* Navicat Premium Data Transfer Source Server : bbk Source Server Type : MySQL Source Server Version : 50649 Source Host : localhost:3306 Source Schema : bbk Target Server Type : MySQL Target Server Version : 50649 File Encoding : 65001 Date: 06/11/2020 20:50:59 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for ims_ewei_score -- ---------------------------- DROP TABLE IF EXISTS `ims_ewei_score`; CREATE TABLE `ims_ewei_score` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0', `kecheng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0', `fenshu` int(11) NULL DEFAULT 0, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- Records of ims_ewei_score -- ---------------------------- INSERT INTO `ims_ewei_score` VALUES (1, '張三', '語文', 81); INSERT INTO `ims_ewei_score` VALUES (2, '張三', '數學', 75); INSERT INTO `ims_ewei_score` VALUES (3, '李四', '語文', 100); INSERT INTO `ims_ewei_score` VALUES (4, '李四', '數學', 81); INSERT INTO `ims_ewei_score` VALUES (5, '王五', '數學', 99); INSERT INTO `ims_ewei_score` VALUES (6, '王五', '英語', 98); INSERT INTO `ims_ewei_score` VALUES (7, '王五', '語文', 81); INSERT INTO `ims_ewei_score` VALUES (8, '張三', '英語', 78); INSERT INTO `ims_ewei_score` VALUES (13, '張三', '英語', 78); SET FOREIGN_KEY_CHECKS = 1;
1、列出各門課程成績最好的學生(要求顯示字段:姓名,科目,成績) //首先列出各科最高的分數
select t1.id,t1.name,t1.kecheng,t1.fenshu from ims_ewei_score t1, (select kecheng,max(fenshu) as maxfenshu from ims_ewei_score group by kecheng) as t2 where t2.kecheng=t1.kecheng and t2.maxfenshu=t1.fenshu;
2、計算每個人的平均成績(要求顯示字段: 姓名,平均成績)
select name,avg(fenshu) as avgfenshu from ims_ewei_score group by name;
3、計算每個人單科的最高成績(要求顯示字段: 姓名,課程,最高成績)
select t1.name,t1.fenshu,t1.kecheng from ims_ewei_score t1, (select name,max(fenshu) as maxfenshu from ims_ewei_score group by name)t2 where t2.name=t1.name and t2.maxfenshu=t1.fenshu;
4、.計算每個人的總成績並排名(要求顯示字段: 姓名,總成績)
select name,sum(fenshu) sumfenshu from ims_ewei_score group by name order by sumfenshu;
5、列出各門課程成績最好的兩位學生(要求顯示字段: 姓名,科目,成績) 有兩種方法
方法一: select t1.* from ims_ewei_score t1 where t1.name in ( select TOP 2 name from ims_ewei_score where kecheng = t1.kecheng order by fenshu desc) order by t1.kecheng; 方法二: select t1.name,t1.fenshu,t1.kecheng from ims_ewei_score t1, (select kecheng,max(fenshu) as maxfenshu from ims_ewei_score group by fenshu order by maxfenshu desc limit 2)t2 where t2.kecheng=t1.kecheng and t2.maxfenshu=t1.fenshu;
6、統計如下:
姓名 | 語文 | 數學 | 英語 | 總分 | 平均分 |
select name 姓名,sum(case when kecheng='語文' then fenshu else 0 end)as 語文, sum(case when kecheng='數學' then fenshu else 0 end)as 數學, sum(case when kecheng='英語' then fenshu else 0 end)as 英語, SUM(fenshu)總分,avg(fenshu)平均分 from ims_ewei_score group by name order by 總分; sum(case where kecheng='數學' then fenshu else 0 end) 意思是當kecheng=’數學‘ 計算fenshu的和 否是 fenshu是0
7、列出各門課程的平均成績(要求顯示字段:課程,平均成績)
select kecheng, avg(fenshu) 平均成績 from ims_ewei_score group by kecheng;
8、列出數學成績的排名(要求顯示字段:姓名,成績,排名)
select name,fenshu, (select count(*) from ims_ewei_score t1 where kecheng='數學' and t1.fenshu >t2.fenshu)+1 as 名次 from ims_ewei_score t2 where kecheng='數學' order by fenshu desc; --注釋:排序,比較大小,比較的次數+1 = 排名。
9、列出數學成績在2-3名的學生(要求顯示字段:姓名,科目,成績)
select t3.* from ( select name,kecheng,fenshu, (select count(*) from ims_ewei_score t1 where kecheng ='數學' and t1.fenshu > t2.fenshu)+1 as 名次 from ims_ewei_score t2 where kecheng='數學') t3 where t3.名次 between 2 and 3 order by t3.fenshu desc;
10、求出李四的數學成績的排名
select name,fenshu, (select count(*) from ims_ewei_score t1 where kecheng='數學' and t1.fenshu >t2.fenshu)+1 as 名次 from ims_ewei_score t2 where kecheng='數學' and name='李四' order by fenshu desc;
11、統計如下
select kecheng 科目,sum(case when fenshu between 0 and 59 then 1 else 0 end) as 不及格, sum(case when fenshu between 60 and 80 then 1 else 0 end) as 良, sum(case when fenshu between 81 and 100 then 1 else 0 end) as 優秀 from ims_ewei_score group by kecheng;
查詢結果
12、
declare @s varchar(1000) set @s='' select @s =@s+','+name+'('+convert(varchar(10),fenshu)+'分)' from ims_ewei_score where kecheng='數學' set @s=stuff(@s,1,1,' ')print '數學:'+@s