《天池龙珠 - SQL训练营》06.SQL进阶:综合练习题-10道经典SQL题目,配套数据与解答
本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql
0 下载相关创建表格和插入数据脚本
1 练习题1
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测数据集《Income Statement.xls》和《Company Operating.xlsx》和《Market Data.xlsx》,以Market Data为主表,将三张表中的TICKER_SYMBOL为600383和600048的信息合并在一起。只需要显示以下字段。
表名 | 字段名 |
---|---|
Income Statement | TICKER_SYMBOL |
Income Statement | END_DATE |
Income Statement | T_REVENUE |
Income Statement | T_COGS |
Income Statement | N_INCOME |
Market Data | TICKER_SYMBOL |
Market Data | END_DATE_ |
Market Data | CLOSE_PRICE |
Company Operating | TICKER_SYMBOL |
Company Operating | INDIC_NAME_EN |
Company Operating | END_DATE |
Company Operating | VALUE |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT DISTINCT
MD.*,
CO.INDIC_NAME_EN,
CO.VALUE,
incom.N_INCOME,
incom.T_COGS,
incom.T_REVENUE
FROM
( SELECT TICKER_SYMBOL, END_DATE, CLOSE_PRICE FROM `market data` WHERE TICKER_SYMBOL = 600383 OR TICKER_SYMBOL = 600048 ) MD # market data
LEFT JOIN
( SELECT TICKER_SYMBOL, INDIC_NAME_EN, END_DATE, VALUE FROM `company operating` WHERE TICKER_SYMBOL = 600383 OR TICKER_SYMBOL = 600048 ) CO ON MD.TICKER_SYMBOL = CO.TICKER_SYMBOL AND MD.END_DATE = CO.END_DATE # company operating
LEFT JOIN
( SELECT TICKER_SYMBOL, END_DATE, T_REVENUE, T_COGS, N_INCOME FROM `income statement` WHERE TICKER_SYMBOL = 600383 OR TICKER_SYMBOL = 600048 ) incom ON CO.TICKER_SYMBOL = incom.TICKER_SYMBOL AND CO.END_DATE = incom.END_DATE # income statement
ORDER BY
MD.TICKER_SYMBOL,
MD.END_DATE
2 练习题2
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=44
请使用 Wine Quality Data 数据集《winequality-red.csv》,找出 pH=3.03的所有红葡萄酒,然后,对其 citric acid 进行中式排名(相同排名的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”)
这句话说的不清楚,总结来说就是,列出pH=3.03的所有红葡萄酒,并且对按照 citric acid 进行排名,值相同的排名也相同(一共三列输出)。
1
2
3
4
5
6
7
8
SELECT
pH,
`citric acid`,
RANK() OVER ( ORDER BY `citric acid` ) AS rankn
FROM
`winequality-red`
WHERE
pH = 3.03;
3 练习题3
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_offline_stage1_test_revised.csv》,试分别找出在2016年7月期间,发放优惠券总金额最多和发放优惠券张数最多的商家。
这里只考虑满减的金额,不考虑打几折的优惠券。
1
2
3
4
5
6
7
-- 优惠力度最大的商家
SELECT Merchant_id,SUM(SUBSTRING_INDEX(`Discount_rate`,':',-1)) AS discount_num
FROM `ccf_offline_stage1_test_revised`
WHERE Date_received BETWEEN '2016-07-01' AND '2016-07-31'
GROUP BY Merchant_id
ORDER BY discount_num DESC
LIMIT 1;
1
2
3
4
5
6
7
-- 优惠券数量最多的商家
SELECT Merchant_id,SUM(1) AS coupon_num
FROM `ccf_offline_stage1_test_revised`
WHERE Date_received BETWEEN '2016-07-01' AND '2016-07-31'
GROUP BY Merchant_id
ORDER BY coupon_num DESC
LIMIT 1;
4 练习题4
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测中的数据集《Macro&Industry.xlsx》中的sheet-INDIC_DATA,请计算全社会用电量:第一产业:当月值在2015年用电最高峰是发生在哪月?并且相比去年同期增长/减少了多少个百分比?
1
2
3
4
5
6
7
-- 用电量最高的月份
SELECT CONCAT(YEAR(PERIOD_DATE),'年',MONTH(PERIOD_DATE),'月') AS '年月',MAX(DATA_VALUE) 用电量
FROM `macro industry`
WHERE INDIC_ID = '2020101522' AND YEAR(PERIOD_DATE) = 2015
GROUP BY CONCAT(YEAR(PERIOD_DATE),'-',MONTH(PERIOD_DATE),'-%')
ORDER BY 用电量 DESC
LIMIT 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- 同期增长百分比
-- 同期增长百分比
SELECT ThisYear.PERIOD_DATE,CONCAT(((ThisYear.DATA_VALUE / LastYear.DATA_VALUE)-1)*100,'%')
FROM
(SELECT PERIOD_DATE,DATA_VALUE
FROM `macro industry`
WHERE INDIC_ID = '2020101522' AND YEAR(PERIOD_DATE) = 2015
GROUP BY PERIOD_DATE
ORDER BY MAX(DATA_VALUE) DESC LIMIT 1) ThisYear
LEFT JOIN
(SELECT PERIOD_DATE,DATA_VALUE
FROM `macro industry`
WHERE INDIC_ID = '2020101522' AND YEAR(PERIOD_DATE) = 2014
GROUP BY PERIOD_DATE
) LastYear
ON YEAR(LastYear.PERIOD_DATE) = YEAR(ThisYear.PERIOD_DATE)-1
WHERE MONTH(LastYear.PERIOD_DATE) = MONTH(ThisYear.PERIOD_DATE);
5 练习题5
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》,试统计在2016年6月期间,线上总体优惠券弃用率为多少?并找出优惠券弃用率最高的商家。
弃用率 = 被领券但未使用的优惠券张数 / 总的被领取优惠券张数
1
2
3
SELECT SUM(Date IS NULL AND Coupon_id IS NOT NULL)/SUM(Coupon_id IS NOT NULL) '优惠券弃用率'
FROM `ccf_online_stage1_train`
WHERE Date_received BETWEEN '2016-06-01' AND '2016-06-30'
1
2
3
4
5
6
7
8
9
10
11
12
13
select Merchant_id,rate
FROM (
SELECT Merchant_id,rate,DENSE_RANK() OVER (ORDER BY rate DESC) my_rank
FROM (
SELECT Merchant_id,SUM(Date IS NULL AND Coupon_id IS NOT NULL)/SUM(Coupon_id IS NOT NULL) rate
FROM `ccf_online_stage1_train`
WHERE Date_received BETWEEN '2016-06-01' AND '2016-06-30'
GROUP BY Merchant_id
) A
) B
WHERE my_rank = 1
这里讲一下我的心路历程,刚开始查到所有商家的优惠券弃用率,这一点还是很简单的,就不赘述了。 其实,选出来第一名也很简单,只是,头脑发热,没想起来用rank函数,老是想把表格自连接起来,后来咨询了天池官方钉钉群的大佬,提供了rank思路,立马就通了~感谢@Finn.
6 练习题6
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=44
请使用 Wine Quality Data 数据集《winequality-white.csv》,找出 pH=3.63的所有白葡萄酒,然后,对其 residual sugar 量进行英式排名(非连续的排名)
1
2
3
SELECT `residual sugar`,pH,RANK() OVER (ORDER BY `residual sugar` DESC) my_rank
FROM `winequality-white`
WHERE pH = 3.63
7 练习题7
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测中的数据集《Market Data.xlsx》中的sheet-DATA,
计算截止到2018年底,市值最大的三个行业是哪些?以及这三个行业里市值最大的三个公司是哪些?(每个行业找出前三大的公司,即一共要找出9个)
1
2
3
4
5
6
7
SELECT TYPE_NAME_CN,TYPE_NAME_EN,my_rank AS "rank"
FROM (
SELECT *,(RANK() OVER (ORDER BY MARKET_VALUE DESC)) my_rank
FROM `market data`
WHERE END_DATE < '2019-01-01'
GROUP BY TYPE_NAME_CN) A
WHERE my_rank<=3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT TYPE_NAME_CN,TICKER_SYMBOL,SUM(MARKET_VALUE)
FROM `market data`
WHERE (TYPE_NAME_CN,TICKER_SYMBOL) in (
SELECT MD.TYPE_NAME_CN,MD.TICKER_SYMBOL
FROM (SELECT TYPE_NAME_CN,TICKER_SYMBOL, MARKET_VALUE, (ROW_NUMBER() OVER(PARTITION BY TYPE_NAME_CN ORDER BY MARKET_VALUE) )r
FROM `market data` ) MD
LEFT JOIN
(SELECT TYPE_NAME_CN, SUM(MARKET_VALUE) AS s
FROM `market data`
WHERE END_DATE < '2019-01-01'
GROUP BY TYPE_NAME_CN
ORDER BY s DESC
LIMIT 3) top3
ON top3.TYPE_NAME_CN = MD.TYPE_NAME_CN
WHERE r <= 3
AND top3.TYPE_NAME_CN IS NOT NULL)
GROUP BY TICKER_SYMBOL
8 练习题8
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》和《ccf_offline_stage1_train.csv》,试找出在2016年6月期间,线上线下累计优惠券使用次数最多的顾客。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT User_id,sum(Discount_num) 使用优惠券数量
FROM
(SELECT User_id,count(*) AS discount_num
FROM `ccf_offline_stage1_train`
WHERE Coupon_id IS NOT NULL AND Date IS NOT NULL AND Date = "201606%"
GROUP BY User_id
UNION ALL
SELECT User_id,count(*) AS discount_num
FROM `ccf_online_stage1_train`
WHERE Coupon_id IS NOT NULL AND Date IS NOT NULL AND YEAR(Date) = 2016 AND MONTH(Date) = 06
GROUP BY User_id) A
GROUP BY User_id
ORDER BY sum(Discount_num) DESC
LIMIT 1;
9 练习题9
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测数据集《Income Statement.xls》中的sheet-General Business和《Company Operating.xlsx》中的sheet-EN。
找出在数据集所有年份中,按季度统计,白云机场旅客吞吐量最高的那一季度对应的净利润是多少?(注意,是单季度对应的净利润,非累计净利润。)
1
2
3
4
5
6
7
8
9
10
SELECT income.TICKER_SYMBOL,SUM(`N_INCOME`)
FROM `income statement` AS income,(
SELECT CEILING(MONTH(END_DATE)/3) AS Season,TICKER_SYMBOL,END_DATE
FROM `company operating`
WHERE INDIC_NAME_EN = "Baiyun Airport:Passenger throughput"
GROUP BY Season
ORDER BY SUM(`VALUE`) DESC
LIMIT 1
) A
WHERE MONTH(income.END_DATE) = A.Season * 3 AND income.TICKER_SYMBOL = A.TICKER_SYMBOL
10 练习题10
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》和《ccf_offline_stage1_train.csv》,试找出在2016年6月期间,线上线下累计被使用优惠券满减最多的前3名商家。
比如商家A,消费者A在其中使用了一张200减50的,消费者B使用了一张30减1的,那么商家A累计被使用优惠券满减51元。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT Merchant_id,sum(Discount_num) 使用优惠券数量
FROM
(SELECT Merchant_id,count(*) AS discount_num
FROM `ccf_offline_stage1_train`
WHERE Coupon_id IS NOT NULL AND Date = "201606%"
GROUP BY Merchant_id
UNION ALL
SELECT Merchant_id,count(*) AS discount_num
FROM `ccf_online_stage1_train`
WHERE Coupon_id IS NOT NULL AND YEAR(Date) = 2016 AND MONTH(Date) = 06
GROUP BY Merchant_id) A
GROUP BY Merchant_id
ORDER BY sum(Discount_num) DESC
LIMIT 1;