Tuesday, 9 June 2020

how to join two tables

select * from foods,somedata;
for example food has 7 columns
some data has 3 columns
final table has 7*3 21 columns
each column of food allocated to some data all columns




SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Around the Horn' AND Customers.CustomerID=Orders.CustomerID;





orders.orderid select the table order and select the column order id
customers.customername select the table customer from that select the customername table
SELECT Orderid , orderdata, customers.customername columns form customers orders table to display it has to pass the conditon customer.customername is match with aroound the horn , it has 1000 records out of that It has again match with customers.customerid is equal to orders.customer id, two filters to get the results
modify the names of the table
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
how to join two tables
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, orders.customerid, customers.customerid
FROM Orders,customers
where Orders.CustomerID=Customers.CustomerID; ist join tables and display who have matched two columns , that matched columns only display
innerjoin
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders two tables will join and display with condition
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
inner join write from two table who has this conditon matched








left join
its like beofre but write only one table but matched with two tables
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;













right join
SELECT column_name(s) SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM table1 FROM Orders
RIGHT JOIN table2 RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ON table1.column_name = table2.column_name; ORDER BY Orders.OrderID;






full join
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
self join
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
union
select a two tables, get the all elements which is common in both tables and if repeat, write only one
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;

  • Each SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in each SELECT statement must also be in the same order
union all write duplicate values also
for example we have a names with 10 times repeat, we want to know how many times it repeated
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
I want to know on same address or same country how many body are there
using group by we can know
descind order write
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
ist it joins orders and shippers , to display it has condion where both ids are matched, that
where both ids are matched order rows only we will show combination and left element order only
we got all rows, from that rows it will show orders.orderid and shippers.shippername
shippers.shippername is repeat how manytimes, that number will show and shipper name will show
sql having
we shown how manytimes shipper repeats, if shipper repeates greater than 5 times that willonly show
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
where matches at that joins means combination got, how many times repeats of last name writes andwrites if count>10 only
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;




No comments:

Post a Comment