《SQL必知必会》--第4课过滤数据及课后练习

部分示例语句

where子句 用于过滤数据

①从products表中查询prod_price为3.49的prod_name和prod_price。

select prod_name,prod_price from [dbo].[Products]
where prod_price=3.49;

在这里插入图片描述

order by子句用于排序,但要放在where子句之后

②对①的查询结果按prod_name排序

select prod_name,prod_price from [dbo].[Products]
where prod_price=3.49
order by prod_name;

在这里插入图片描述
③从products表中查询prod_price小于10美元的prod_name和prod_price。

select prod_name,prod_price from [dbo].[Products]
where prod_price<10;

在这里插入图片描述

[!=]不匹配查询

④从products表中查询vend_id,prod_name,返回vend_id不是‘DLL01’的数据。

select vend_id,prod_name from [dbo].[Products]
where vend_id != 'DLL01';

在这里插入图片描述

between…and… 用于范围值的检查

⑤从products表中查询prod_name和prod_price,并返回prod_price在5到10之间的数据。

select prod_name,prod_price from [dbo].[Products]
where prod_price between 5 and 10;

在这里插入图片描述

is null 用于空值的检查

⑥从customers表中查询cust_name,并返回cust_email为空值的数据。

select cust_name from [dbo].[Customers]
where cust_email is null;

在这里插入图片描述

课后练习

①从products表中查询prod_id和prod_name。只返回价格为9.49美元的产品。

select prod_id,prod_name from [dbo].[Products]
where prod_price=9.49;

在这里插入图片描述
②从products表中查询prod_id和prod_name。只返回价格为大于等于9美元的产品。

select prod_id ,prod_name from [dbo].[Products]
where prod_price>=9;

在这里插入图片描述
③从orderItems表中查询order_num(要求只返回不同的订单号),只返回quantity大于等于100的数据

select distinct order_num from [dbo].[OrderItems]
where quantity>=100;

在这里插入图片描述
④从products表中查询prod_name,prod_price,只返回价格在3到9美元之间的数据,并按照prod_price排序。

select prod_name ,prod_price from [dbo].[Products]
where prod_price between 3 and 9
order by prod_price;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值