文章目录
- 一、检索数据
- 二、排序检索数据
- 三、过滤数据
- 四、高级数据过滤
- 五、用通配符进行过滤
- 六、创建计算字段
- 七、使用函数处理数据
- 八、汇总数据
- 九、分组数据
- 十、使用子查询
- 十一、联结表
- 十二、创建高级联结
- 十三、组合查询
一、检索数据
【题目1:从 Customers 表中检索所有的 ID】
select cust_id from Customers;
【题目2:检索并列出已订购产品的清单】
select
prod_id
from OrderItems
group by prod_id
;
【题目3:检索所有列】
select * from Customers;
二、排序检索数据
【题目1:检索顾客名称并且排序】
select
cust_name
from Customers
order by cust_name desc
;
【题目2:对顾客ID和日期排序】
select
cust_id
,order_num
from Orders
order by cust_id asc,order_date desc
;
【题目3:按照数量和价格排序】
select
quantity
,item_price
from OrderItems
order by quantity desc,item_price desc
;
【题目4:检查SQL语句】
#-- 修改前
SELECT vend_name,
FROM Vendors
ORDER vend_name DESC;
#-- 修改后
SELECT vend_name
FROM Vendors
ORDER BY vend_name DESC;
三、过滤数据
【题目1:返回固定价格的产品】
select
prod_id
,prod_name
from Products
where prod_price='9.49'
;
【题目2:返回更高价格的产品】
select
prod_id
,prod_name
from Products
where prod_price>='9'
;
【题目3:返回产品并且按照价格排序】
select
prod_name
,prod_price
from Products
where prod_price between '3' and '6'
order by prod_price
;
【题目4:返回更多的产品】
select
order_num
from OrderItems
group by order_num
having sum(quantity)>=100
;
四、高级数据过滤
【题目1:检索供应商名称】
select
vend_name
from Vendors
where vend_country='USA' and vend_state='CA'
;
【题目2:检索并列出已订购产品的清单】
select
order_num
,prod_id
,quantity
from OrderItems
where prod_id in ('BR01','BR02','BR03')
and quantity>100
;
【题目3:返回所有价格在 3美元到 6美元之间的产品的名称和价格】
select
prod_name
,prod_price
from Products
where prod_price between 3 and 6
order by prod_price
;
【题目4:纠错2】
#-- 修改前
SELECT vend_name
FROM Vendors
ORDER BY vend_name
WHERE vend_country = 'USA' AND vend_state = 'CA';
#-- 修改后
SELECT vend_name
FROM Vendors
WHERE vend_country = 'USA' AND vend_state = 'CA'
ORDER BY vend_name;
五、用通配符进行过滤
【题目1:检索产品名称和描述 (一)】
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%'
;
【题目2:检索产品名称和描述(二)】
select
prod_name
,prod_desc
from Products
where prod_desc not like '%toy%'
order by prod_name
;
【题目3:检索产品名称和描述(三)】
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%' and prod_desc like '%carrots%'
;
【题目4:检索产品名称和描述 (四)】
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%carrots%'
;
六、创建计算字段
【题目1:别名】
select
vend_id
,vend_name vname
,vend_address vaddress
,vend_city vcity
from Vendors
order by vend_name
;
【题目2:打折】
select
prod_id
,prod_price
,prod_price*0.9 sale_price
from Products
;
七、使用函数处理数据
【题目1:顾客登录名】
select
cust_id
,cust_name
,upper(concat(substr(cust_contact,1,2),substr(cust_city,1,3))) user_login
from Customers
;
【题目2:返回2020年1月的所有订单的订单号和订单日期】
select
order_num
,order_date
from Orders
where substr(order_date,1,7)='2020-01'
order by order_date
;
八、汇总数据
【题目1:确定已售出产品的总数】
select
sum(quantity) items_ordered
from OrderItems
;
【题目2:确定已售出产品项 BRO1 的总数】
select
sum(quantity) items_ordered
from OrderItems
where prod_id='BR01'
;
【题目3:确定Products表中价格不超过10美元的最贵产品的价格】
select
max(prod_price) max_price
from Products
where prod_price<=10
;
九、分组数据
【题目1:返回每个订单号各有多少行数】
select
order_num
,count(order_num) order_lines
from OrderItems
group by order_num
order by order_lines
;
【题目2:每个供应商成本最低的产品】
select
vend_id
,min(prod_price) cheapest_item
from Products
group by vend_id
order by cheapest_item
;
【题目3:确定最佳顾客】
select
order_num
from OrderItems
group by order_num
having sum(quantity)>=100
order by order_num
;
【题目4:确定最佳顾客的另一种方式 (一)】
select
order_num
,sum(item_price*quantity) total_price
from OrderItems
group by order_num
having sum(item_price*quantity)>=1000
order by order_num
;
【题目5:纠错3】
#-- 修改前
SELECT order_num, COUNT(*) AS items
FROM OrderItems
GROUP BY items
HAVING COUNT(*) >= 3
ORDER BY items, order_num;
#-- 修改后
SELECT order_num, COUNT(*) AS items
FROM OrderItems
GROUP BY order_num
HAVING COUNT(*) >= 3
ORDER BY items, order_num;
十、使用子查询
【题目1:返回购买价格为10美元或以上产品的顾客列表】
select
cust_id
from Orders
where order_num in (
select
order_num
from OrderItems
where item_price>=10
)
;
【题目2:确定哪些订单购买了 prod id 为 BRO1 的产品 (一)】
select
cust_id
,order_date
from Orders
where order_num in (
select
order_num
from OrderItems
where prod_id='BR01'
)
order by order_date
;
【题目3:返回购买 prod id 为 BRO1 的产品的所有顾客的电子邮件 (一)】
select
cust_email
from Customers
where cust_id in (
select
cust_id
from Orders
where order_num in (
select
order_num
from OrderItems
where prod_id='BR01'
)
)
;
【题目4:返回每个顾客不同订单的总金额】
select
a.cust_id
,sum(item_price*quantity) total_ordered
from Orders a
left join OrderItems b
on a.order_num=b.order_num
group by a.cust_id
order by total_ordered desc
;
【题目5:从 Products 表中检索所有的产品名称以及对应的销售总数】
select
prod_name
,sum(quantity) quant_sold
from Products a
left join OrderItems b
on a.prod_id=b.prod_id
group by prod_name
;
十一、联结表
【题目1:返回顾客名称和相关订单号】
select
cust_name
,order_num
from Customers a
join Orders b
on a.cust_id=b.cust_id
order by cust_name,order_num
;
【题目2:返回顾客名称和相关订单号以及每个订单的总价】
select
cust_name
,b.order_num
,sum(quantity*item_price) OrderTotal
from Customers a
join Orders b on a.cust_id=b.cust_id
join OrderItems c on b.order_num=c.order_num
group by cust_name,b.order_num
order by cust_name,b.order_num
;
【题目3:确定哪些订单购买了 prod id 为 BRO1 的产品 (二)】
select
cust_id
,order_date
from Orders
where order_num in (
select
order_num
from OrderItems
where prod_id='BR01'
)
order by order_date
;
【题目4:返回购买 prod id 为 BRO1 的产品的所有顾客的电子邮件 (二)】
select
cust_email
from OrderItems a
join Orders b on a.order_num=b.order_num
join Customers c on b.cust_id=c.cust_id
where a.prod_id='BR01'
;
【题目5:确定最佳顾客的另一种方式 (二)】
select
cust_name
,sum(item_price*quantity) total_price
from Customers a
join Orders b on a.cust_id=b.cust_id
join OrderItems c on b.order_num=c.order_num
group by cust_name
having sum(item_price*quantity)>1000
order by total_price
;
十二、创建高级联结
【题目1:检索每个顾客的名称和所有的订单号 (一)】
select
cust_name
,order_num
from Orders a
join Customers b on a.cust_id=b.cust_id
order by cust_name
;
【题目2:检索每个顾客的名称和所有的订单号 (二)】
select
cust_name
,order_num
from Customers a
left join Orders b on a.cust_id=b.cust_id
order by cust_name
;
【题目3:返回产品名称和与之相关的订单号】
select
prod_name
,order_num
from Products a
left join OrderItems b on a.prod_id=b.prod_id
order by prod_name
;
【题目4:返回产品名称和每一项产品的总订单数】
select
prod_name
,count(order_num) order_num
from Products a
left join OrderItems b on a.prod_id=b.prod_id
group by prod_name
order by prod_name
;
【题目5:列出供应商及其可供产品的数量】
select
a.vend_id
,count(prod_id) prod_id
from Vendors a
left join Products b on a.vend_id=b.vend_id
group by a.vend_id
order by a.vend_id
;
十三、组合查询
【题目1:将两个 SELECT 语结合起来 (一)】
select
prod_id
,quantity
from OrderItems
where quantity=100
union all
select
prod_id
,quantity
from OrderItems
where prod_id like 'BNBG%'
order by prod_id
;
【题目2:将两个 SELECT 语结合起来 (二)】
select
prod_id
,quantity
from OrderItems
where quantity=100 or prod_id like 'BNBG%'
;
【题目3:组合 Products 表中的产品名称和 Customers 表中的顾客名称】
select
prod_name prod_name
from Products
union all
select
cust_name prod_name
from Customers
order by prod_name
;
【题目4:纠错4】
#-- 修改前
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'MI'
ORDER BY cust_name;
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'IL'ORDER BY cust_name;
#-- 修改后
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'MI'
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state = 'IL'
ORDER BY cust_name;