《SQL必知必会》题解+我的SQL笔记
题单来源,牛客网
select cust_id from Customers;select distinct prod_id from OrderItems;62.检索所有列
select cust_id,cust_name from Customers;select cust_name from Customers
order by cust_name desc;select cust_id,order_num
from Orders
order by cust_id ,order_date desc;select *
from OrderItems
order by quantity desc,item_price desc;select vend_name
from Vendors
order by vend_name desc;67. 返回固定价格的产品
select prod_id,prod_name
from Products
where prod_price=9.49select 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 ;select distinct order_num
from OrderItems
where 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;select prod_name,prod_price
from Products
where prod_price between 3 and 6
order by prod_price asc;1select vend_name
from Vendors
where vend_country='USA' AND vend_state='CA'select prod_name,prod_desc
from Products
where prod_desc like '%toy';select prod_name,prod_desc
from Products
where not prod_desc 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 as vname,
vend_address as vaddress,
vend_city as vcity
from Vendors
order by vname asc;select
prod_id,
prod_price,
prod_price*0.9 as sale_price
from Products;select
cust_id,
cust_name,
upper(concat(left(cust_contact,2),left(cust_city,3)))
as user_login
from Customers;select order_num,order_date
from Orders
where year (order_date)=2020
and month (order_date)=1
order by order_date asc;select sum(quantity) as items_ordered
from OrderItems;select sum(quantity) as items_ordered
from OrderItems
where prod_id like 'BR01'select max(prod_price) as max_price
from Products
where prod_price <=10 ;select order_num, count(*) as order_lines
from OrderItems
group by order_num
order by order_lines asc;select vend_id,
min(prod_price) as cheapest_item
from Products
group by vend_id
order by cheapest_item asc;select order_num
from OrderItems
group by order_num
having sum(quantity)>=100
order by order_num asc;select order_num,
sum(item_price*quantity ) as total_price
from OrderItems
group by order_num
having total_price>=1000
order by order_num asc;select order_num,count(*) as items
from OrderItems
group by order_num
having count(*)>=3select cust_id
from Orders
where order_num in
(
select order_num from OrderItems
where item_price >=10
)select cust_id,order_date
from Orders
where order_num in
(
select order_num
from OrderItems
where prod_id ='BR01')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'))- 坑点:
OrderItems.order_num = Orders.order_num需要考虑,2个订单是否完整
select
cust_id,
(
select sum( item_price*quantity ) as total_ordered
from OrderItems
where OrderItems.order_num = Orders.order_num
group by order_num
) as total_ordered
from
Orders
order by total_ordered descselect prod_name,
(
select sum(quantity) as quant_sold
from OrderItems
where Products.prod_id=OrderItems.prod_id
group by prod_name
)as quant_sold
from Productsselect cust_name,order_num
from Orders,Customers
where Customers.cust_id=Orders.cust_id
order by cust_name asc;select
Customers.cust_name,
Orders.order_num,
sum(item_price*quantity)as OrderToal
from Customers,Orders,OrderItems
where Customers.cust_id=Orders.cust_id
and Orders.order_num=OrderItems.order_num
group by cust_name,order_num
order by cust_name asc,order_num asc;select cust_id,order_date
from Orders
where Orders.order_num in
(
select order_num from OrderItems
where prod_id='BR01'
)
order by order_date asc;方法1:inner join
select cust_email
from Customers
inner join Orders on Customers.cust_id=Orders.cust_id
inner join OrderItems on Orders.order_num=OrderItems.order_num
where OrderItems.prod_id='BR01';方法2
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'
)
);inner join
select Customers.cust_name,sum(item_price*quantity) as total_price
from Customers
inner join Orders on Customers.cust_id=Orders.cust_id
inner join OrderItems on Orders.order_num=OrderItems.order_num
group by Customers.cust_name
having total_price >=1000;select cust_name,order_num
from Customers
inner join Orders on Customers.cust_id=Orders.cust_id
order by cust_name asc;select cust_name,order_num
from Customers
left outer join Orders on Customers.cust_id=Orders.cust_id
order by cust_name asc;select prod_name,order_num
from Products
left outer join OrderItems on Products.prod_id=OrderItems.prod_id
order by prod_name asc;select prod_name,count(order_num)as orders
from Products
left outer join OrderItems on Products.prod_id=OrderItems.prod_id
group by prod_name
order by prod_name asc;select Vendors.vend_id,count(Products.prod_id) as prod_id
from Vendors
left outer join Products on Vendors.vend_id=Products.vend_id
group by Vendors.vend_id
order by Vendors.vend_id ;(select prod_id,quantity
from OrderItems
where quantity =100)
union
select prod_id,quantity
from OrderItems
where prod_id like 'BNBG%'
order by prod_idselect *
from OrderItems
where quantity=100
or prod_id like 'BNBG%';select prod_name
from Products
union all
select cust_name
from Customers
order by prod_name;select cust_name,cust_contact,cust_email
from Customers
where cust_state in ('MI','IL')
order by cust_name;