sql分组操作符与聚集函数
对于表结构:
course(cno,cname,credit)
统计 course表中学分数(credit)大于2的课程门数
select count(cno) from course where credit>2;
统计所有专业必修课(BT开头的课程代码)的学分总数
select sum(credit) from course where cno in (select cno from course where left(cno,2)='BT')
按课程类别统计每个类别课程的门数,如课程代码BT001,BT002都是专业必修课
select left(cno,2),count(cno) from course group by left(cno,2)
对于表结构:
printer(model,color,type,price)
model:打印机型号;
color:是否彩色, T 彩色,F 黑白
type:类型,ink-jet 表示喷墨, laser 表示激光;
price:单价
统计激光彩色打印机有多少种型号
select count(model) from printer where type='laser' and color = 'T'
找出最便宜的喷墨打印机价格
select min(price) from printer where type = 'ink-jet'
找出最贵的激光打印机型号和价格
select model,price from printer where price in (select max(price) from printer where type='laser')
对于表结构:
product(maker,model,type)
maker:表示生产厂商
model:生产的产品型号
type:产品类型,有pc laptop两种
pc(model,speed,ram,hd,price)
表示型号,速度,内存大小,硬盘大小,价格
laptop(model,speed,ram,hd,screen,price)
表示型号,速度,内存大小,硬盘大小,屏幕大小和价格
查询在一种或两种电脑(含PC和laptop)中出现过的硬盘的容量
select hd from V_test group by hd having count(model)<3
统计各生产厂商生产的电脑(不区分pc和laptop)的平均处理速度的最大值
select max(avg_speed) from
(select maker, avg(speed) as avg_speed from V_test group by maker)
as a;
统计出各厂商生产价格高于1000的产品数量,不用区分是pc还是laptop
select maker,count(the_model) from
(select model as the_model,maker from V_test where price>1000) as a
group by maker
别统计各厂商生产的pc,laptop的平均价格
select maker, type, avg(price) from
(select * from V_test) a group by maker, type;