Combination of grouping and aggregate functions

1. Data Preparation

-- 创建 "京东" 数据库
create database jing_dong charset=utf8;

-- 使用 "京东" 数据库
use jing_dong;

-- 创建一个商品goods数据表
create table goods(
    id int unsigned primary key auto_increment not null,
    name varchar(150) not null,
    cate_name varchar(40) not null,
    brand_name varchar(40) not null,
    price decimal(10,3) not null default 0,
    is_show bit not null default 1,
    is_saleoff bit not null default 0
);

-- 向goods表中插入数据

insert into goods values(0,'r510vc 15.6英寸笔记本','笔记本','华硕','3399',default,default); 
insert into goods values(0,'y400n 14.0英寸笔记本电脑','笔记本','联想','4999',default,default);
insert into goods values(0,'g150th 15.6英寸游戏本','游戏本','雷神','8499',default,default); 
insert into goods values(0,'x550cc 15.6英寸笔记本','笔记本','华硕','2799',default,default); 
insert into goods values(0,'x240 超极本','超级本','联想','4880',default,default); 
insert into goods values(0,'u330p 13.3英寸超极本','超级本','联想','4299',default,default); 
insert into goods values(0,'svp13226scb 触控超极本','超级本','索尼','7999',default,default); 
insert into goods values(0,'ipad mini 7.9英寸平板电脑','平板电脑','苹果','1998',default,default);
insert into goods values(0,'ipad air 9.7英寸平板电脑','平板电脑','苹果','3388',default,default); 
insert into goods values(0,'ipad mini 配备 retina 显示屏','平板电脑','苹果','2788',default,default); 
insert into goods values(0,'ideacentre c340 20英寸一体电脑 ','台式机','联想','3499',default,default); 
insert into goods values(0,'vostro 3800-r1206 台式电脑','台式机','戴尔','2899',default,default); 
insert into goods values(0,'imac me086ch/a 21.5英寸一体电脑','台式机','苹果','9188',default,default); 
insert into goods values(0,'at7-7414lp 台式电脑 linux )','台式机','宏碁','3699',default,default); 
insert into goods values(0,'z220sff f4f06pa工作站','服务器/工作站','惠普','4288',default,default); 
insert into goods values(0,'poweredge ii服务器','服务器/工作站','戴尔','5388',default,default); 
insert into goods values(0,'mac pro专业级台式电脑','服务器/工作站','苹果','28888',default,default); 
insert into goods values(0,'hmz-t3w 头戴显示设备','笔记本配件','索尼','6999',default,default); 
insert into goods values(0,'商务双肩背包','笔记本配件','索尼','99',default,default); 
insert into goods values(0,'x3250 m4机架式服务器','服务器/工作站','ibm','6888',default,default); 
insert into goods values(0,'商务双肩背包','笔记本配件','索尼','99',default,default);

Table structure description:

  • id means the primary key is incremented
  • name represents the name of the product
  • cate_name represents the category name
  • brand_name represents the brand name
  • price indicates the price
  • is_show indicates whether to show
  • is_saleoff indicates whether it is sold out

 

2. SQL statement walkthrough

  1. Query the name and price of the product whose type cate_name is'Superbook'

     select name,price from goods where cate_name = '超级本';
    
  2. Show product category

     select cate_name from goods group by cate_name;
    
  3. Find the average price of all computer products, and keep two decimal places

     select round(avg(price),2) as avg_price from goods;
    
  4. Show the average price of each item

     select cate_name,avg(price) from goods group by cate_name;
    
  5. Query the most expensive, cheapest, average price, and quantity of each type of commodity

     select cate_name,max(price),min(price),avg(price),count(*) 
     from goods group by cate_name;
    
  6. Query all products whose prices are greater than the average price, and sort them in descending order of price

     select id,name,price from goods 
     where price > (select round(avg(price),2) as avg_price from goods) 
     order by price desc;

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115239199