Left , Right, Full Outer Joins and Unions.

Left Joins :
Gets everything from the left table as well as entries from the right table that matches the left table.
SQLITE dont have right join rather we use the left join by just switching the positions of the table.

example :
SELECT c.customername
 ,o.orderid
FROM customers c
LEFT JOIN orders o ON c.customerid = o.customerid
ORDER BY customername;

--the intention of the above query is to get the customer detail even if they have a order or not, but do return order details too id the customer had an order.

Right Join :
Gets everything from the right table as well as entries from the left table that matches the right table.

example :
SELECT o.orderid
 ,e.lastname
 ,e.firstname
FROM orders o
RIGHT JOIN employee e ON o.emplyeeid = e.employeeid
ORDER BY o.orderid

Full Outer Join :
Its type of join will return all records where there is a match in either table one or there's a match in table two .
SELECT o.orderid
 ,e.lastname
 ,e.firstname
FROM orders o
FULL OUTER JOIN employee e ON o.emplyeeid = e.employeeid
ORDER BY o.order

Unions :
Union is used to stack results of two or more queries into one table , remember its stacking of the output .
So always check that both the results must have same number of columns with same data type more specifically in same order . 


SELECT city
 ,country
FROM customers
WHERE country = 'germany'

UNION

SELECT city
 ,country
FROM suppliers
WHERE country = 'germany'
ORDER BY city;

difference between union and all union union vs all union 

Comments

Popular Posts