牛客SQL 必知必会 50套题代码及解析

练习链接:
https://www.nowcoder.com/exam/oj?page=1&tab=SQL%E7%AF%87&topicId=298

1.检索数据

SQL60 从Customers表中检索所有的ID

select *
from Customers

SQL61 检索并列出已订购产品的清单

select distinct prod_id
from OrderItems

SQL62 检索所有列

select *
from Customers

2.排序检索数据

SQL63 检索顾客名称并且排序

select cust_name
from Customers
order by cust_name desc

SQL64 对顾客ID和日期排序

select cust_id,order_num
from Orders
order by cust_id,order_date desc

SQL65 按照数量和价格排序

select quantity, item_price
from OrderItems
order by quantity desc,item_price desc

SQL66 检查SQL语句

SELECT vend_name
FROM Vendors 
ORDER by vend_name DESC;

3.过滤数据

SQL67 返回固定价格的产品

select prod_id,prod_name
from Products
where prod_price=9.49

SQL68 返回更高价格的产品

select prod_id,prod_name
from Products
where prod_price >=9

SQL69 返回产品并且按照价格排序

select prod_name,prod_price
from Products
where prod_price between 3 and 6
order by prod_price

SQL70 返回更多的产品

select order_num
from OrderItems
where quantity>=100
group by order_num

4.高级数据过滤

SQL71 检索供应商名称

select vend_name
from Vendors
where vend_country='USA' and vend_state='CA'

SQL72 检索并列出已订购产品的清单

select order_num,prod_id,quantity
from OrderItems
where prod_id in ('BR01','BR02','BR03') and quantity>100

SQL73 返回所有价格在3美元到6美元之间的产品的名称和价格

select prod_name, prod_price
from Products
where prod_price>=3 and prod_price<=6
order by prod_price

SQL74 纠错

SELECT vend_name 
FROM Vendors 
WHERE vend_country = 'USA' AND vend_state = 'CA'
ORDER BY vend_name 

5.用通配符进行过滤

SQL75 检索产品名称和描述(一)

select prod_name,prod_desc
from Products
where prod_desc like '%toy%'

SQL76 检索产品名称和描述(二)

select prod_name,prod_desc
from Products
where prod_desc not like '%toy%'
order by prod_name

SQL77 检索产品名称和描述(三)

select prod_name,prod_desc
from Products
where prod_desc like '%toy%' and prod_desc like '%carrots%'

SQL78 检索产品名称和描述(四)

# 方法一
select prod_name,prod_desc
from Products
where prod_desc like '%toy%carrots%' 
# 方法二
select prod_name,prod_desc
from Products
where prod_desc regexp '.*toy.*carrots.*' 

6.创建计算字段

SQL79 别名

select vend_id,
vend_name as vname,
vend_address as vaddress,
vend_city as vcity
from Vendors
order by vname

SQL80 打折

select prod_id,
prod_price,
prod_price*0.9 as sale_price
from Products

7.使用函数处理数据

SQL81 顾客登录名1.concat用于拼接 ; 2. upper用于转换大写

select cust_id, cust_name,
upper(concat(substring(cust_contact,1,2),substring(cust_city,1,3))) as user_login
from Customers

SQL82 返回2020年1月的所有订单号和订单日期

select order_num,order_date
from Orders
where year(order_date)='2020' and month(order_date)='01'
order by order_date

8.汇总数据

SQL83 确定已售出产品的总数

select sum(quantity) as items_ordered
from OrderItems

SQL84 确定已售出产品项BR01的总数

select sum(quantity) as items_ordered
from OrderItems
where prod_id= 'BR01'

SQL85 确定Products表中价格不超过10美元的最贵产品的价格

select max(prod_price) as max_price
from Products
where prod_price <=10

9.分组数据

SQL86 返回每个订单号各有多少行数

select order_num,
count(order_num) as order_lines
from OrderItems
group by order_num
order by order_lines

SQL87 每个供应商成本最低的产品

select vend_id,
min(prod_price) as cheapest_item
from Products
group by vend_id
order by cheapest_item

SQL88 确定最佳顾客

select order_num
from OrderItems
where quantity >=100
group by order_num
order by order_num

SQL89 确定最佳顾客的另一种方式(一)
1. group by、where、having、order by的使用顺序:where、group by 、having、order by;2. where 是对记录的限定,having 是对分组后结果的限定,所以where在group by之前,having在group by之后。3.where后的条件表达式里不允许使用聚合函数,而having可以,所以遇到聚和函数用having。

select order_num,
sum(quantity * item_price) as total_price
from OrderItems
group by order_num
having total_price >=1000
order by order_num

SQL90 纠错(三)

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING COUNT(*) >= 3 
ORDER BY items, order_num;

10.使用子查询

SQL91 返回购买价格为10美元或以上产品的顾客列表

select cust_id
from Orders
where order_num in (
        select order_num
        from OrderItems
        where item_price >=10
)

SQL92 确定哪些订单购买了 prod id 为BR01的产品(一)

select cust_id,order_date
from Orders
where order_num in (
    select order_num
    from OrderItems
    where prod_id='BR01'
)
order by order_date

SQL93 返回购买 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'
    )
)

SQL94 返回每个顾客不同订单的总金额

select cust_id,
(select sum(quantity*item_price)
from OrderItems 
where OrderItems.order_num=Orders.order_num) as total_ordered
from Orders 
order by total_ordered desc

SQL95 为Products表中检索所有的产品名称以及对应的销售总数

select prod_name,
(select sum(quantity)
from OrderItems
where OrderItems.prod_id=Products.prod_id) as quant_sold
from Products

11.联结表

SQL96 返回顾客名称和相关订单号

#方法一
select cust_name,order_num
from Customers
join Orders using(cust_id)
order by cust_name,order_num

#方法二
select cust_name,order_num
from Customers as a,Orders as b
where a.cust_id=b.cust_id
order by cust_name,order_num

#方法三
select cust_name,order_num
from Customers as a
inner join Orders as b
on a.cust_id=b.cust_id
order by cust_name,order_num

SQL97 返回顾客名称和相关订单号以及每个订单的总价

select cust_name,order_num,
sum(quantity*item_price) as OrderTotal
from Customers
join Orders using(cust_id)
join OrderItems using(order_num)
group by cust_name,order_num
order by cust_name,order_num

SQL98 确定哪些订单购买了 prod id 为BR01的产品(二)

select cust_id,order_date
from OrderItems
join Orders using(order_num)
where prod_id='BR01'
order by order_date

SQL99 返回购买 prod id 为BR01的产品的所有顾客的电子邮件(二)

select cust_email
from Customers
inner join Orders using(cust_id)
inner join OrderItems using(order_num)
where prod_id='BR01'
group by cust_email 

SQL100 确定最佳顾客的另一种方式(二)

select cust_name,
sum(item_price * quantity) as total_price
from Customers
inner join Orders using(cust_id)
inner join OrderItems using(order_num)
group by  cust_name
having total_price >=1000
order by total_price

12.创建高级联结

SQL101 检索每个顾客的名称和所有订单号(一)

select cust_name,order_num
from Customers
inner join Orders using(cust_id)
order by cust_name

SQL102 检索每个顾客的名称和所有订单号(二)

select cust_name,order_num
from Customers
left join Orders using(cust_id)
order by cust_name

SQL103 返回产品名称和与之相关的订单号

select prod_name,order_num
from Products
left join OrderItems using(prod_id)
order by prod_name

SQL104 返回产品名称和每一项产品的总订单数

select prod_name,
count(order_num) as orders
from Products
left join OrderItems using(prod_id)
group by prod_name
order by prod_name

SQL105 列出供应商及其可供应产品的数量

select vend_id,
count(prod_id) as prod_id
from Products
right join Vendors using(vend_id)
group by vend_id
order by vend_id

13.组合查询

SQL106 将两个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

SQL107 将两个select语句结合起来(二)

select prod_id,quantity
from OrderItems
where quantity=100 or prod_id like'BNBG%'
order by prod_id

SQL108 组合Products表中的产品名称和Customers表中的顾客名称

select  prod_name
from Products
union all
select cust_name as prod_name
from Customers
order by prod_name

SQL109 纠错(四)
在组合查询中,排序只能放在所有语句的最后,且出现一次

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;

猜你喜欢

转载自blog.csdn.net/qq118640594X/article/details/126914912