实操套娃一天-进阶课程Ⅱ 我好像顿悟了

进阶课程虽然我学了两天,但是感觉还有点囫囵吞枣。今天准备拎几个重要的点再找些习题巩固一下

准备每两天把下面的题目做一遍,扎实基础

1、子查询

1.1查找比生菜(lettuce)更贵的产品

首先我们打开products表

SELECT * 
FROM products
where unit_price > (select unit_price from products where product_id=3)

1.2查找一个所有收入(salary)在平均线以上的演员

打开sql_hr表

2.1查找products表中的product id不在order_items 表中的记录

order_item表

products表

2.2查找没开发票的用户 (和上文差不多 不截表图了)

3.1找到预定了生菜的客户

select * from custom

3.2找到高于该商品种类的平均销售单价的商品

1、从product表中选取出各商品种类中高于该商品种类的平均销售单价的商品,得到这个结果

3.3工资超过部门平均工资的员工

select* from  sql_hr.employees e1
where salary >
(SELECT avg(salary)
FROM sql_hr.employees 
where office_id=e1.office_id
group by office_id)

3.4获取高于平均值的发票

3.5、查询client表与invoices表连接 取出id、姓名、求和列、平均值列和差值列

SELECT client_id,
name,
(select sum(invoice_total)from sql_invoicing.invoices i
where client_id=c.client_id) as total_sales,
(select avg(invoice_total)from sql_invoicing.invoices i
) as avg_sales,
(select total_sales-avg_sales)as diffrants
 FROM sql_invoicing.clients c

3.6在from里的子查询

select *from
(SELECT client_id,
name,
(select sum(invoice_total)from sql_invoicing.invoices i
where client_id=c.client_id) as total_sales,
(select avg(invoice_total)from sql_invoicing.invoices i
where client_id=c.client_id) as avg_sales,
(select total_sales-avg_sales)as diffrants
 FROM sql_invoicing.clients c) as cc
 where cc.total_sales is not null

上面的计算项都查出来之后如果想要跑数据会发现系统识别不出是那张表的 所以可以重新select 它当作另一张表 然后命个名

再加筛选条件,当然创建视图也可以嘛

3.7提取各商品种类的平均单价得到以下结果

出现group by之后就要就会返回报错提示:意思是返回得值不止一行所以就要设定个条件就是两个表之间相等得品类才返回

如果得出得只是总得平均值就直接当个列好了

4.1  case when

SELECT CONCAT(first_name,' ', last_name) as customer,
points,
  CASE 
  WHEN points >3000 THEN 'GOLD' 
       WHEN points >=2000 THEN 'silver'
       ELse 'bronze'
       END AS 'c'
FROM sql_store.customers

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值