《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;