පේළි දෙකක් හෝ
වැඩි ගණනක් ඒකාබද්ධ කිරීම සඳහා JOIN වගන්තියක් භාවිතා
කරයි.
SQL සම්බන්ධතා වල විවිධ වර්ග
- (INNER)
JOIN: වගු දෙකෙහිම ගැලපෙන අගයන් ඇති වාර්තා
ලබා දෙයි
- LEFT (OUTER) JOIN: සියලුම
වාර්තා වම් වගුවෙන් සහ ගැලපෙන වාර්තා දකුණු වගුවෙන් ලබා දෙයි
- RIGHT (OUTER) JOIN:සියලුම
වාර්තා දකුණු වගුවෙන් ද, ගැලපෙන
වාර්තා වම් වගුවෙන් ද ලබා දෙයි
- FULL (OUTER) JOIN: වම් හෝ
දකුණු වගුවේ ගැලපීමක් ඇති විට සියලුම වාර්තා ලබා දෙයි
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example-
JOIN Two
Tables-
SELECT Orders.OrderID,
Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
JOIN Three
Tables-
SELECT Orders.OrderID,
Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example-
SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example-
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Example-
SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
වගු
දෙකකින් එකවර දත්ත දත්ත ලබා ගැනීම - UNION
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION
SELECT column_name(s) FROM table2;
හෝ
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
UNION ALL
SELECT column_name(s) FROM table2;
Example-
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
UNION
SELECT City FROM Suppliers
ORDER BY City;
දත්ත කාණ්ඩ
කිරීම - GROUP BY
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example-
SELECT COUNT(CustomerID),
Country
FROM Customers
GROUP BY Country;
FROM Customers
GROUP BY Country;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
No comments:
Post a Comment
Thank You..