2-6 SOL JOIN之练习JOIN 问题 第一部分
首个JOIN的练习
# 问题一 别名的练习
SELECT a.primary_poc,w.occurred_at,w.channel,a.name
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
WHERE a.name='Walmart';
#问题二 注意需要对于新建的列建立列名称
SELECT r.name region, s.name rep, a.name account
FROM accounts a
JOIN Sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON s.region_id = r.id
ORDER BY a.name
# 问题三
SELECT r.name region,a.name account,o.total_amt_usd/(o.total+0.01) unit_price
FROM region r
JOIN sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.sales_rep_id
JOIN orders o
ON a.id = o.account_id;
2-6 SOL JOIN之练习JOIN 问题 最后的检测
# 问题一
SELECT r.name region, s.name rep_name, a.name account
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
WHERE r.name = 'Midwest'
ORDER BY a.name;
# 问题二
SELECT r.name region, s.name rep_name, a.name account
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
WHERE r.name = 'Midwest' AND s.name LIKE 'S%'
ORDER BY a.name;
# 问题三
SELECT r.name region, s.name rep_name, a.name account
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
WHERE r.name = 'Midwest' AND s.name LIKE 'K%'
ORDER BY a.name;
# 问题四
SELECT r.name region, a.name account, o.total_amt_usd/(total+0.01) unit_price
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
JOIN orders o
ON a.id = o.account_id
WHERE o.standard_qty > 100 ;
# 问题五
SELECT r.name region, a.name account, o.total_amt_usd/(total+0.01) unit_price
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
JOIN orders o
ON a.id = o.account_id
WHERE o.standard_qty > 100 AND o.poster_qty > 50
ORDER BY unit_price;
# 问题六
SELECT r.name region, a.name account, o.total_amt_usd/(total+0.01) unit_price
FROM region r
JOIN Sales_reps s
ON r.id = s.region_id
JOIN accounts a
ON s.id = a.Sales_rep_id
JOIN orders o
ON a.id = o.account_id
WHERE o.standard_qty > 100 AND o.poster_qty > 50
ORDER BY unit_price DESC;
# 问题七
SELECT DISTINCT a.id account,w.channel channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
WHERE a.id = 1001;
## SELECT DISTINCT 可以避免重复值的出现,去掉数据冗余的现象
# 问题八
SELECT w.occurred_at occurred_at,a.name account_name,o.total order_total,o.total_amt_usd order_total_amt_usd
FROM web_events w
JOIN accounts a
ON w.account_id = a.id
JOIN orders o
ON a.id = o.account_id
WHERE w.occurred_at BETWEEN '2015-01-01' AND '2015-12-31';
# 官方给出的答案
解决方案
为每个销售代表对应的区域以及相关的客户创建一个表格,这次仅针对 Midwest 区域。最终表格应该包含三列:区域名称、销售代表姓名,以及客户名称。根据客户名称按字母顺序 (A-Z) 排序。
SELECT r.name region, s.name rep, a.name account
FROM sales_reps s
JOIN region r
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
WHERE r.name = 'Midwest'
ORDER BY a.name;
为每个销售代表对应的区域以及相关的客户创建一个表格,这次仅针对 Midwest 区域,并且销售代表的名字以 S 开头。最终表格应该包含三列:区域名称、销售代表姓名,以及客户名称。根据客户名称按字母顺序 (A-Z) 排序。
SELECT r.name region, s.name rep, a.name account
FROM sales_reps s
JOIN region r
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
WHERE r.name = 'Midwest' AND s.name LIKE 'S%'
ORDER BY a.name;
为每个销售代表对应的区域以及相关的客户创建一个表格,这次仅针对 Midwest 区域,并且销售代表的姓以 K 开头。最终表格应该包含三列:区域名称、销售代表姓名,以及客户名称。根据客户名称按字母顺序 (A-Z) 排序。
SELECT r.name region, s.name rep, a.name account
FROM sales_reps s
JOIN region r
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
WHERE r.name = 'Midwest' AND s.name LIKE '% K%'
ORDER BY a.name;
提供每个订单的每个区域的名称,以及客户名称和所支付的单价 (total_amt_usd/total)。但是,只针对标准订单数量超过 100 的情况提供结果。最终表格应该包含三列:区域名称、客户名称和单价。为了避免除以 0 个订单,这里可以在分母上加上 0.01,即:(total_amt_usd/(total+0.01))。
SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price
FROM region r
JOIN sales_reps s
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
WHERE o.standard_qty > 100;
提供每个订单的每个区域的名称,以及客户名称和所支付的单价 (total_amt_usd/total)。但是,只针对标准订单数量超过 100 且广告纸数量超过 50 的情况提供结果。最终表格应该包含三列:区域名称、客户名称和单价。按照最低的单价在最之前排序。为了避免除以 0 个订单,这里可以在分母上加上 0.01,即:(total_amt_usd/(total+0.01))。
SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price
FROM region r
JOIN sales_reps s
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
WHERE o.standard_qty > 100 AND o.poster_qty > 50
ORDER BY unit_price;
提供每个订单的每个区域的名称,以及客户名称和所支付的单价 (total_amt_usd/total)。但是,只针对标准订单数量超过 100 且广告纸数量超过 50 的情况提供结果。最终表格应该包含三列:区域名称、客户名称和单价。按照最高的单价在最之前排序。为了避免除以 0 个订单,这里可以在分母上加上 0.01,即:(total_amt_usd/(total+0.01))。
SELECT r.name region, a.name account, o.total_amt_usd/(o.total + 0.01) unit_price
FROM region r
JOIN sales_reps s
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
WHERE o.standard_qty > 100 AND o.poster_qty > 50
ORDER BY unit_price DESC;
account id 为 1001 的客户使用了哪些不同的渠道。最终表格应该包含 2 列:客户名称和不同的渠道。你可以尝试使用 SELECT DISTINCT 使结果仅显示唯一的值。
SELECT DISTINCT a.name, w.channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
WHERE a.id = '1001';
找出发生在 2015 年的所有订单。最终表格应该包含 4 列:occurred_at、account name、order total 和 order total_amt_usd。
SELECT w.occurred_at, a.name, o.total, o.total_amt_usd
FROM accounts a
JOIN orders o
ON o.account_id = a.id
JOIN web_events w
ON a.id = w.account_id
WHERE w.occurred_at BETWEEN '01-01-2015' AND '01-01-2016'
ORDER BY w.occurred_at DESC;