select cust_id from Customers;
select
prod_id
from OrderItems
group by prod_id
;
select * from Customers;
select
cust_name
from Customers
order by cust_name desc
;
select
cust_id
,order_num
from Orders
order by cust_id asc,order_date desc
;
select
quantity
,item_price
from OrderItems
order by quantity desc,item_price desc
;
#-- 修改前
SELECT vend_name,
FROM Vendors
ORDER vend_name DESC;
#-- 修改后
SELECT vend_name
FROM Vendors
ORDER BY vend_name DESC;
select
prod_id
,prod_name
from Products
where prod_price='9.49'
;
select
prod_id
,prod_name
from Products
where prod_price>='9'
;
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
;
select
vend_name
from Vendors
where vend_country='USA' and vend_state='CA'
;
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
;
#-- 修改前
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;
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%'
;
select
prod_name
,prod_desc
from Products
where prod_desc not like '%toy%'
order by prod_name
;
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%' and prod_desc like '%carrots%'
;
select
prod_name
,prod_desc
from Products
where prod_desc like '%toy%carrots%'
;
select
vend_id
,vend_name vname
,vend_address vaddress
,vend_city vcity
from Vendors
order by vend_name
;
select
prod_id
,prod_price
,prod_price*0.9 sale_price
from Products
;
select
cust_id
,cust_name
,upper(concat(substr(cust_contact,1,2),substr(cust_city,1,3))) user_login
from Customers
;
select
order_num
,order_date
from Orders
where substr(order_date,1,7)='2020-01'
order by order_date
;
select
sum(quantity) items_ordered
from OrderItems
;
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
;
select
order_num
,count(order_num) order_lines
from OrderItems
group by order_num
order by order_lines
;
select
vend_id
,min(prod_price) cheapest_item
from Products
group by vend_id
order by cheapest_item
;
select
order_num
from OrderItems
group by order_num
having sum(quantity)>=100
order by order_num
;
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
;
#-- 修改前
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;
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'
)
)
;
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
;
select
cust_name
,order_num
from Customers a
join Orders b
on a.cust_id=b.cust_id
order by cust_name,order_num
;
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'
;
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
;
select
cust_name
,order_num
from Orders a
join Customers b on a.cust_id=b.cust_id
order by cust_name
;
select
cust_name
,order_num
from Customers a
left join Orders b on a.cust_id=b.cust_id
order by cust_name
;
select
prod_name
,order_num
from Products a
left join OrderItems b on a.prod_id=b.prod_id
order by prod_name
;
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
;
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
;
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
;
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
;
#-- 修改前
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;