当前位置: 首页 > article >正文

MySQL - OJ 练习(23道)

SQL60 从 Customers 表中检索所有的 ID

select 
    cust_id 
from 
    Customers;

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

select 
    distinct prod_id 
from 
    OrderItems;

SQL62 检索所有列

select 
    cust_id, 
    cust_name
from 
    Customers;

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;

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
    distinct order_num
from
    OrderItems
where 
    quantity >= 100;

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 between 3 and 6
order by 
    prod_price;

SQL74 纠错2

select 
    vend_name
from 
    Vendors
WHERE 
    vend_country = 'USA' 
AND 
    vend_state = 'CA'
ORDER BY 
    vend_name ;

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%';

SQL79 别名

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

SQL80 打折

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

SQL81 顾客登录名

-- 其中登录名全部为大写字母,并由顾客联系人的前两个字符(cust_contact)和其所在城市的前三个字符(cust_city)组成
-- 取字符串的前 n 个字符
-- substr(字段, 1, n);
-- 大写
-- upper(...)
-- 连接字符串
-- concat(...)
select
    cust_id,
    cust_name,
    upper(
        concat (
            substr (cust_contact, 1, 2),
            substr (cust_city, 1, 3)
        )
    ) user_login
from
    Customers;

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

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

-- 第二种方法

select 
    order_num,
    order_date
from   
    Orders
where 
    order_date between '2020-01-01 00:00:00' and '2020-01-31 23:59:59'
order by 
    order_date;


http://www.kler.cn/a/37629.html

相关文章:

  • 按键精灵、auto.js等一些移动端脚本 如何连接云服务器的数据库, 进行读写操作
  • spring复习:(18)给bean的属性赋值
  • 再学JavaScript
  • 接收请求数据和数据的回显
  • JS相关介绍
  • 【Vue H5项目实战】从0到1的自助点餐系统—— 搭建脚手架(Vue3.2 + Vite + TS + Vant + Pinia + Node.js)
  • Ubuntu 安装 Docker
  • K210开发实例-现场可编程GPIO阵列、通用GPIO与高速GPIO
  • 银行安全用电监管平台可行性研究及解决方案
  • 【Rust 基础篇】Rust Cargo 自定义构建
  • 【微信小程序-uniapp】CustomPicker 自定义单项选择器组件
  • WebSocket使用记录
  • MongoDB初体验-安装使用教程2023.7
  • 求助Claude GPT | PyPDF2 亲测可用
  • 综合实验---基于卷积神经网络的目标分类案例
  • 【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(6月 30 日论文合集)
  • 橙河网络:怎么搭建海外问卷网站呢?
  • 基于springboot+Redis的前后端分离项目(九)-【黑马点评】
  • RPM包详解以及如何制作RPM包
  • 1.17 什么是微服务