首页 > 试题广场 >

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

[单选题]
29.返回订单数量总和不小于100的所有订单的订单号
OrderItems代表订单商品表,包括:订单号order_num和订单数量quantity
order_num quantity
a1 105
a2
1100
a2
200
a4
1121
a5
10
a2
19
a7 5
【问题】请编写 SQL 语句,返回订单数量总和不小于100的所有订单号,最后结果按照订单号升序排序。
【示例结果】返回order_num订单号。
order_num
a1
a2
a4
【示例解析】
订单号a1、a2、a4的quantity总和都大于等于100,按顺序为a1、a2、a4。
  • select order_num
    from OrderItems
    where quantity >= 100
    group by order_num
    order by order_num
  • select order_num
    from OrderItems
    group by order_num
    having sum(quantity) >= 100
  • select order_num from
    (select sum(quantity) as order_num from OrderItems group by order_num) as a
    where order_num>=100
  • select order_num
    from OrderItems
    where quantity>100;
b答案才是正确的,having是去聚合函数的过滤,where不能这样
发表于 2022-05-28 22:42:22 回复(5)
a离谱了吧
发表于 2024-04-14 17:03:42 回复(0)
A用where就会在group by之前运行,那就不是订单号的quantity总和,而是每一行的quantity,答案居然能是A,离谱
发表于 2022-09-27 17:25:06 回复(0)
牛客里的题怎么漏洞百出的😪
发表于 2022-07-28 21:15:59 回复(0)
把我都要整蒙了

发表于 2024-04-16 22:43:58 回复(0)
额,求教,C为啥不对啊
发表于 2023-04-04 11:28:41 回复(4)
A选项只是筛选出单个订单数量不小于100的订单号,B选项才是符合题意的答案 
发表于 2022-07-09 18:15:49 回复(1)
发表于 2022-06-26 10:51:33 回复(0)
不是要订单数量总和≥100?1能实现吗?如果a1现在为两个订单,分别是95-10的数量,题目的意思,最后表也要输出a1吧?
发表于 2022-05-25 05:17:52 回复(0)