SQL语句每日一练二

1.给出 Customers 表 如下:
cust_id cust_name cust_contact cust_city
a1 Andy Li Andy Li Oak Park
a2 Ben Liu Ben Liu Oak Park
a3 Tony Dai Tony Dai Oak Park
a4 Tom Chen Tom Chen Oak Park
a5 An Li An Li Oak Park
a6 Lee Chen Lee Chen Oak Park
a7 Hex Liu Hex Liu Oak Park

【问题】编写 SQL 语句,返回顾客 ID(cust_id)、顾客名称(cust_name)和登录名(user_login),其中登录名全部为大写字母,并由顾客联系人的前两个字符(cust_contact)和其所在城市的前三个字符(cust_city)组成。提示:需要使用函数、拼接和别名。

select cuet_id,cust_name,upper(concat(substring(cust_contact,1,2),substring(cust_city,1,3))) from Customers;
2.Orders 订单表如下:
order_num order_date
a0001 2020-01-01 00:00:00
a0002 2020-01-02 00:00:00
a0003 2020-01-01 12:00:00
a0004 2020-02-01 00:00:00
a0005 2020-03-01 00:00:00

【问题】编写 SQL 语句,返回 2020 年 1 月的所有订单的订单号(order_num)和订单日期(order_date),并按订单日期升序排序

select order_num,order_date from Orders where year(order_date)=='2020' and month(order_date)=='01' order by order_date;
select order_num,order_date from Orders where order_date like '2020-01%' order by order_date;
3.OrderItems 表代表售出的产品,quantity 代表售出商品数量。
quantity
10
100
1000
10001
2
15

【问题】编写 SQL 语句,确定已售出产品的总数。

select sum(quantity) from OrderItems;
4.OrderItems 表代表售出的产品,quantity 代表售出商品数量,产品项为 prod_id
quantity prod_id
10 AR01
100 AR10
1000 BR01
10001 BR010

【问题】修改创建的语句,确定已售出产品项(prod_id)为"BR01"的总数

select sum(quantity) from OrderItems where prod_id = 'BR01';
5.Products 表如下,prod_price 代表商品的价格。
prod_price
9.49
600
1000

【问题】编写 SQL 语句,确定 Products 表中价格不超过 10 美元的最贵产品的价格(prod_price)。将计算所得的字段命名为 max_price

select max(prod_price) as max_price from Products where prod_price<=10;
6.OrderItems 表包含每个订单的每个产品
order_num
a002
a002
a002
a004
a007

【问题】编写 SQL 语句,返回每个订单号(order_num)各有多少行数(order_lines),并按 order_lines 对结果进行升序排序。

select order_num,count(order_num) as order_lines from OrderItems group by order_num order by order_lines;
7.有 Products 表,含有字段 prod_price 代表产品价格,vend_id 代表供应商 id
vend_id prod_price
a0011 100
a0019 0.1
b0019 1000
b0019 6980
b0019 20

【问题】编写 SQL 语句,返回名为 cheapest_item 的字段,该字段包含每个供应商成本最低的产品(使用 Products 表中的 prod_price),然后从最低成本到最高成本对结果进行升序排序。

select vend_id,min(prod_price) as cheapest_item from Products group by vend_id order by cheapest_item;
8.OrderItems 代表订单商品表,包括:订单号 order_num 和订单数量 quantity
order_num quantity
a1 105
a2 1100
a2 200
a4 1121
a5 10
a2 19
a7 5

【问题】请编写 SQL 语句,返回订单数量总和不小于 100 的所有订单号,最后结果按照订单号升序排序。

select order_num from OrderItems group by order_num having sum(quantity) >= 100 order by order_num;
9OrderItems 表代表订单信息,包括字段:订单号 order_numitem_price 商品售出价格、quantity 商品数量。
order_num item_price quantity
a1 10 105
a2 1 1100
a2 1 200
a4 2 1121
a5 5 10
a2 1 19
a7 7 5

【问题】编写 SQL 语句,根据订单号聚合,返回订单总价不小于 1000 的所有订单号,最后的结果按订单号进行升序排序。

select order_num,sum(item_price*quantity) as total_price from OrderItems group by order_num having total_price>= 1000 order by order_num;
10.OrderItems 表含有 order_num 订单号
order_num
a002
a002
a002
a004
a007

【问题】将下面代码修改正确后执行

SELECT order_num, COUNT(*) AS items
FROM OrderItems
GROUP BY items
HAVING COUNT(*) >= 3
ORDER BY items, order_num;
select order_num,count(*) as item group by order_num having items>=3 order by items,order_num;

相关推荐

  1. SQL语句每日

    2024-03-22 06:04:04       23 阅读
  2. SQL语句每日

    2024-03-22 06:04:04       17 阅读
  3. SQL语句每日

    2024-03-22 06:04:04       15 阅读
  4. SQL语句每日

    2024-03-22 06:04:04       9 阅读
  5. SQL语句每日十六

    2024-03-22 06:04:04       12 阅读
  6. C语言每日之37

    2024-03-22 06:04:04       28 阅读
  7. C语言程序每日(7)

    2024-03-22 06:04:04       10 阅读
  8. C语言程序每日(6)

    2024-03-22 06:04:04       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 06:04:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 06:04:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 06:04:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 06:04:04       18 阅读

热门阅读

  1. 电商平台客户反馈的数据分析与应用

    2024-03-22 06:04:04       17 阅读
  2. 华岳M9制造企业管理软件业务流程 4/4

    2024-03-22 06:04:04       18 阅读
  3. c++石头剪刀布游戏

    2024-03-22 06:04:04       18 阅读
  4. 如何进行Web应用的版本控制和部署?

    2024-03-22 06:04:04       25 阅读
  5. cad vba 打开excel并弹窗打开指定文件

    2024-03-22 06:04:04       16 阅读
  6. 383.赎金信

    2024-03-22 06:04:04       17 阅读
  7. bert_base_chinese入门

    2024-03-22 06:04:04       20 阅读
  8. python 之 装饰器(Decorators)

    2024-03-22 06:04:04       16 阅读