sql server 2008语言基础: 连接查询习题

declare @t table(n int)
declare @i int=1
while(@i<1000)
begin
    insert into @t select @i
    set @i=@i+1
end
--select * from @t

--1. copy all employees five times.
--select e.empid,e.firstname,e.lastname, t.n from hr.Employees e, @t t where t.n<6

--2. 为每个雇员和从09年6月12日至09年6月16日范围内的每天返回一行.
--select e.empid,e.firstname,e.lastname,DATEADD(day, t.n,'2009-06-12') from hr.Employees e, @t t 
--where t.n<6 order by empid

--3. 返回来自美国的客户, 并为每个客户返回其订单总数和商品交易总数量
--select c.custid, count(o.orderid), sum(od.qty) from 
--Sales.Customers c , Sales.Orders o, Sales.OrderDetails od
--where c.country='usa' and c.custid=o.custid and o.orderid=od.orderid group by c.custid

--4. 返回客户及其订单信息,  包括没有下过任何订单的客户.
--select c.custid,c.companyname, o.orderid, o.orderdate 
--from Sales.Customers c left join Sales.Orders o on c.custid=o.custid

--4. 返回没有下过订单的客户
--select c.custid,c.companyname, o.orderid, o.orderdate 
--from Sales.Orders o right join  Sales.Customers c on c.custid=o.custid where o.orderid is null

--5. 返回在07年2月12日下过订单的客户, 以及他们的订单
--select c.custid,c.companyname, o.orderid, o.orderdate 
--from Sales.Orders o right join  Sales.Customers c on c.custid=o.custid 
--where o.orderdate='2007-02-12'

--6. 返回在2007年2月12日下过订单的客户, 以及他们的订单. 也返回在2007年2月12日没有下过订单的客户
select c.custid,c.companyname, 
case when o.orderid IS null then 'no' else  'yes' end
from Sales.Orders o right join  Sales.Customers c on 
(c.custid=o.custid and o.orderdate='2007-02-12')
本人在长沙, 有工作可以加我QQ4658276
原文地址:https://www.cnblogs.com/jianjialin/p/2438626.html