1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 牛客网刷题之SQL篇二:SQL必知必会 50T

牛客网刷题之SQL篇二:SQL必知必会 50T

时间:2022-03-29 07:16:39

相关推荐

牛客网刷题之SQL篇二:SQL必知必会 50T

文章目录

一、SQL 知识点1、SQL编写顺序2、SQL执行顺序3、having和where的区别4、group by 多字段分组5、union all 和 union6、 二、牛客网题1、SQL22 顾客登录名2、SQL23 返回 年 1 月的所有订单的订单号和订单日期3、SQL26 确定 Products 表中价格不超过 10 美元的最贵产品的价格4、SQL28 每个供应商成本最低的产品5、SQL29 返回订单数量总和不小于100的所有订单的订单号6、SQL30 计算总和7、SQL38 返回顾客名称和相关订单号以及每个订单的总价8、SQL39 确定哪些订单购买了 prod_id 为 BR01 的产品(二)9、SQL41 确定最佳顾客的另一种方式(二 三、SQL进阶挑战1、

一、SQL 知识点

1、SQL编写顺序

select、from、where、group by、having、order by、limit

2、SQL执行顺序

Mysql里面select、from、where、group by、having、order by的执行顺序

1、执行from和where得到第一个结果集

2、对第一个结果集执行group by分组操作,得到第二个结果集

3、对第二个结果集进行select操作,得到第三个结果集

4、对第三个结果集进行having操作,得到第四个结果集

5、对第四个结果集进行order by操作,得到最终结果

3、having和where的区别

1、having必须在执行了group by操作之后才能运行

2、where的执行顺序在group by之前,也就必然在having之前。

3、having后面可以加上聚合函数,而where后面不能加聚和函数如count(),sum(),avg(),max(),min()等函数。

举例:按由高到低的顺序显示个人平均分在70分以上的学生姓名和平均分,为了尽可能地提高平均分,在计算平均分前不包括分数在60分以下的成绩

select name,ave(score) from scoretable where score >= 60 group by name having ave(score) > 70 order by ave(score) desc;

4、group by 多字段分组

group by 学习

5、union all 和 union

6、

知识点:SELECT 语句的输出用 ORDER BY 子句排序。在用 UNION 组合查询时,只能使用一条 ORDER BY 子句,它必须出现在最后一条 SELECT 语句之后。对于结果集,不存在用一种方式排序一部分,而又用另一种方式排序另一

部分的情况,因此不允许使用多条 ORDER BY 子句。

错误的SQL

只需去掉 第一行的 ORDER BY cust_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*from (SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state = 'MI' UNION ALLSELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state = 'IL') tORDER BY cust_name;

二、牛客网题

1、SQL22 顾客登录名

SQL22 顾客登录名:

selectcust_id,cust_name,upper(concat(substring(cust_contact,1,2), substring(cust_city, 1, 3))) as user_loginfrom Customers

upper():变成大写字母

concat():连接字符串函数

substring:字符串截取函数

2、SQL23 返回 年 1 月的所有订单的订单号和订单日期

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

//方法一selectorder_num,order_datefrom Orderswhere order_date like '%-01%'order by order_date//方法二selectorder_num,order_datefrom Orderswhere year(order_date) = ''and month(order_date) = '1'order by order_date

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

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

// 方式一select max(prod_price) as max_pricefrom Productswhere prod_price <= 10// 方式二select prod_price as max_pricefrom Productswhere prod_price <= 10order by prod_price desclimit 1

4、SQL28 每个供应商成本最低的产品

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

分组使用聚合函数,可以记住幼:min()+group by

select vend_id,min(prod_price) as cheapest_itemfrom Productsgroup by vend_idorder by cheapest_item

5、SQL29 返回订单数量总和不小于100的所有订单的订单号

SQL29 返回订单数量总和不小于100的所有订单的订单号

group by + having + 聚合函数sum()

selectorder_numfrom OrderItemsgroup by order_numhaving sum(quantity) > 100order by order_num

6、SQL30 计算总和

SQL30 计算总和

聚合函数sum() + 列乘 + group by + having + order by

select order_num,sum(item_price * quantity) as total_pricefrom OrderItemsgroup by order_numhaving sum(item_price * quantity) >= 1000order by order_num

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

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

三表连接、聚合函数、列乘、group by 、order by 、选择多列、select后的字段 应在group by之后(除聚合函数)

select cust_name,o.order_num,sum(quantity * item_price) as OrderTotalfrom Customers cleft join Orders o on c.cust_id = o. cust_idleft join OrderItems oo on o.order_num = oo.order_numgroup by cust_name, order_numorder by cust_name, o.order_num, OrderTotal

8、SQL39 确定哪些订单购买了 prod_id 为 BR01 的产品(二)

SQL39 确定哪些订单购买了 prod_id 为 BR01 的产品(二)

//方法一select cust_id,order_datefrom OrderItems o1left join Orders o2 on o1.order_num = o2.order_numwhere prod_id = 'BR01'order by order_date asc #方法二select cust_id, order_datefrom Orders,(select order_numfrom OrderItemswhere prod_id="BR01") twhere t.order_num = Orders.order_numorder by order_date;#方法三select cust_id,order_datefrom Orders where order_num in (select order_numfrom OrderItemswhere prod_id="BR01")order by order_date;#方法四select cust_id,order_datefrom Orders o join OrderItems oi on o.order_num=oi.order_numwhere prod_id="BR01"order by order_date;//方法四 select cust_id,order_date from Orders a,OrderItems b where a.order_num =b.order_num and b.prod_id ='BR01' order by order_date

9、SQL41 确定最佳顾客的另一种方式(二

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

聚合函数sum、列成、inner join、group by、having、order by

selectcust_name,sum(item_price * quantity) as total_pricefrom OrderItems o1inner join Orders o2 on o1.order_num = o2.order_numinner join Customers c on c.cust_id = o2.cust_idgroup by cust_namehaving total_price > 1000order by total_price

三、SQL进阶挑战

1、

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。