INSTRUCCIONES INNER JOIN, LEFT JOIN, RIGHT JOIN

SQL TRANSACT: Consultas Multitablas

Como crear consultas de varias tablas usando las instrucciones “inner Join”, “Left join”, “right join”, “cross join”.

 
use northwind
–Inner Join
Select c.customerid, c.companyname
, o.orderid, o.orderdate
from customers as c inner join orders as o
on c.customerid=o.customerid
–left Join
Select c.customerid, c.companyname
, o.orderid, o.orderdate
from customers as c left outer join orders as o
on c.customerid=o.customerid
where OrderID is null
–right Join
Select c.customerid, c.companyname
, o.orderid, o.orderdate
from orders as o right outer join customers as c
on c.customerid=o.customerid
where OrderID is null
–cross join
Select c.customerid, c.companyname
, o.orderid, o.orderdate
from orders as o cross join customers as c
–union de varias tablas
Select c.customerid, c.companyname
, o.orderid, o.orderdate, p.ProductName
,od.Quantity, od.UnitPrice
from customers as c inner join orders as o
on c.customerid=o.customerid
inner join [Order Details] as od
on o.OrderID=od.OrderID
inner join products as p
on od.ProductID=p.ProductID

Leave a Reply

Your email address will not be published. Required fields are marked *