MySQL Chapter 4-Querying Data by Using Joins and Subqueries

2 篇文章 0 订阅
use globaltoyz;

/*Exercise 1*/
select t.vToyName, b.cBrandName,c.cCategory from toys as t Join toybrand as b
on t.cBrandId=b.cBrandId JOIN category as c on t.cCategoryId=c.cCategoryId;

/*Exercise 2*/
SELECT toys.vToyName,shoppingcart.cCartId FROM toys 
LEFT OUTER JOIN shoppingcart ON toys.cToyID=shoppingcart.cToyId;/*'join''s left is full*/

/*Exercise 3*/
SELECT shopper.cshopperId, shopper.cState,orders.cOrderNo  FROM orders RIGHT OUTER JOIN 
shopper ON shopper.cShopperId=orders.cShopperId;/*joinis right is full*/

/*Exercise 4*/
SELECT s.cShopperId, s.vFirstName,s.vLastName,count(o.cShopperId) 
FROM shopper as s cross JOIN orders as o on s.cShopperId=o.cShopperId group by s.cshopperid;

/*Exercise 5*/
select o.cOrderNo, o.ctoyId, w.vDescription from orderdetail as o Join wrapper as w
on o.cWrapperId=w.cWrapperId;

/*Exercise 6*/
select t.cToyId,t.vToyName,s.siQty from 
shoppingcart as s inner join toys as t on s.cToyId=t.cToyId 
where cCartId=000001;

/*Exercise 7*/
SELECT * FROM toys RIGHT OUTER JOIN 
category ON toys.cCategoryId=category.cCategoryId;

/*Exercise 8*/
SELECT country.cCountryId, cModeId, mRatePerPound FROM country 
left OUTER JOIN shippingrate ON country.cCountryId=shippingrate.cCountryId;

/*Exercise 9*/
select cShopperId, vFirstName, vLastName from shopper join country on shopper.cCountryId=country.cCountryId 
where cCountry ='United States of America';

/*Exercise 10*/
select r.cCountryId,r.cModeId,m.cMode from shippingrate r Join shippingmode as m
on r.cModeId=m.cModeId;

/*Exercise 11*/
select * from orderdetail where cOrderNo IN (select cOrderNo from orders where dOrderDate='2001/05/20');

/*Exercise 12*/
select * from category WHERE EXISTS (
SELECT * FROM toys WHERE category.cCategoryId=toys.cCategoryId);

/*Exercise 13*/
select vToyName
from toys where cCategoryId IN (select cCategoryId from category where cCategory='Stuffed Toys');

/*Exercise 14*/
select cOrderNo from orders WHERE EXISTS (
SELECT * FROM shipment WHERE orders.cOrderNo=shipment.cOrderNo and cDeliveryStatus='d');

/*Exercise 15*/
SELECT cOrderNo, mTotalCost FROM orders WHERE mTotalCost > (SELECT Avg(mTotalCost) FROM orders);

/*Exercise 16*/
select cToyId,siMonth,iYear,iTotalSold from pickofmonth where iTotalSold>
all (select iTotalSold from pickofmonth where cToyId=0000015);

/*Exercise 17*/
SELECT cBrandName FROM toybrand Where cBrandId IN 
(SELECT cBrandId FROM toys Where 
cToyId IN (SELECT cToyId FROM orders Where cOrderNo=000005));

/*Exercise 18*/
select cOrderNo,dOrderDate, cShopperId from orders where mTotalCost>
all (select mTotalCost from orders where cCartId=000002);

/*Exercise 19*/
SELECT * FROM toys WHERE mToyRate> Any ( SELECT mToyRate from toys where cCategoryId=000002);

/*Exercise 20*/
select * from shoppingcart where cToyId=(select cToyId from toys where vToyName='Robby the whale');

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值