|
| 1 | + |
| 2 | +-- INNER JOIN syntax and example |
| 3 | + |
| 4 | +SELECT column_name(s) |
| 5 | +FROM table1 |
| 6 | +INNER JOIN table2 |
| 7 | +ON table1.column_name = table2.column_name; |
| 8 | + |
| 9 | +-- Example |
| 10 | + |
| 11 | +SELECT Orders.OrderID, Customers.ContactName, Orders.OrderDate |
| 12 | +FROM Orders |
| 13 | +INNER JOIN Customers |
| 14 | +ON Orders.CustomerID = Customers.CustomerID; |
| 15 | + |
| 16 | +-- This query retrieves Order ID, Customer's contact name and Order Date by matching CustomerID in Orders and Customers tables. |
| 17 | + |
| 18 | +-- Order ID ContactName Phone OrderDate |
| 19 | +-- 10249 Karin Josephs 0251-031259 1996-07-05 00:00:00.000 |
| 20 | + |
| 21 | +-- Example with 3 tables |
| 22 | + |
| 23 | +SELECT Orders.OrderID, Customers.ContactName, [Orders Details].ProductID, Orders.OrderDate |
| 24 | +FROM ((Orders |
| 25 | +INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) |
| 26 | +INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID); |
| 27 | + |
| 28 | +-- This query retrieves Order ID, Customer's contact name, Order Product ID and Order Date by matching CustomerID in Orders and Customer tables and matching OrderID in Orders and Order Details tables. |
| 29 | + |
| 30 | +-- Order ID ContactName ProductID OrderDate |
| 31 | +-- 10294 Paula Wilson 1 1996-08-30 00:00:00.000 |
| 32 | + |
| 33 | + |
| 34 | +-- LEFT JOIN syntax and example |
| 35 | + |
| 36 | +SELECT column_name(s) |
| 37 | +FROM table1 |
| 38 | +LEFT JOIN table2 |
| 39 | +ON table1.column_name = table2.column_name; |
| 40 | + |
| 41 | +-- Example |
| 42 | + |
| 43 | +SELECT Orders.OrderID, Customers.ContactName |
| 44 | +FROM Orders |
| 45 | +LEFT JOIN Customers |
| 46 | +ON Orders.CustomerID = Customers.CustomerID |
| 47 | +ORDER BY Customers.ContactName; |
| 48 | + |
| 49 | +-- This query retrieves all Order ID and their corresponding Customer's contact name including orders with no associated customer. |
| 50 | + |
| 51 | +-- Order ID ContactName |
| 52 | +-- 10281 Alejandra Camino |
| 53 | + |
| 54 | + |
| 55 | +-- RIGHT JOIN syntax and example |
| 56 | + |
| 57 | +SELECT column_name(s) |
| 58 | +FROM table1 |
| 59 | +RIGHT JOIN table2 |
| 60 | +ON table1.column_name = table2.column_name; |
| 61 | + |
| 62 | +-- Example |
| 63 | + |
| 64 | +SELECT Orders.OrderID, Customers.CompanyName |
| 65 | +FROM Orders |
| 66 | +RIGHT JOIN Customers |
| 67 | +ON Orders.CustomerID = Customers.CustomerID; |
| 68 | + |
| 69 | +-- This query retrieves all Company Name and their corresponding Order IDs including customers with no associated orders. |
| 70 | + |
| 71 | +-- Order ID CompanyName |
| 72 | +-- 10643 Alfreds Futterkiste |
| 73 | + |
| 74 | + |
| 75 | +-- FULL OUTER JOIN syntax and example |
| 76 | + |
| 77 | +SELECT column_name(s) |
| 78 | +FROM table1 |
| 79 | +FULL OUTER JOIN table2 |
| 80 | +ON table1.column_name = table2.column_name; |
| 81 | + |
| 82 | +-- Example |
| 83 | + |
| 84 | +SELECT Orders.OrderID, Customers.CompanyName, Customers.ContactName |
| 85 | +FROM Orders |
| 86 | +FULL OUTER JOIN Customers |
| 87 | +ON Orders.CustomerID = Customers.CustomerID |
| 88 | +ORDER BY Customers.ContactName; |
| 89 | + |
| 90 | +-- This query retrieves all Order IDs, Customer Company Names and Contact Names including orders with no associated customer and customers with no associated orders. |
| 91 | + |
| 92 | +-- Order ID CompanyName ContactName |
| 93 | +-- 10281 Romero y tomillo Alejandra Camino |
| 94 | + |
| 95 | + |
| 96 | +-- SELF JOIN syntax and example |
| 97 | + |
| 98 | +SELECT column_name(s) |
| 99 | +FROM table1 AS t1 |
| 100 | +JOIN table1 AS t2 |
| 101 | +ON t1.column_name = t2.column_name; |
| 102 | + |
| 103 | +-- Example (this example has done without db sample) |
| 104 | + |
| 105 | +SELECT e1.name as employee, e2.name as manager |
| 106 | +FROM Employees as e1 |
| 107 | +INNER JOIN Employees as e2 |
| 108 | +ON e1.ManagerID = e2.EmployeeID; |
| 109 | + |
| 110 | +-- This query retrieves Employee names and their corresponding manager names by joining the employees table with itself. |
| 111 | + |
| 112 | +-- employee manager |
| 113 | +-- Ali Mehmet Doruk Çoralı |
| 114 | + |
| 115 | + |
| 116 | +-- CROSS JOIN syntax and example |
| 117 | + |
| 118 | +SELECT column_name(s) |
| 119 | +FROM table1 |
| 120 | +CROSS JOIN table2; |
| 121 | + |
| 122 | +-- Example |
| 123 | + |
| 124 | +SELECT Products.ProductID, Products.ProductName, Categories.CategoryID, Categories.CategoryName |
| 125 | +FROM Products |
| 126 | +CROSS JOIN Categories; |
| 127 | + |
| 128 | +-- This query retrieves all possible combinations of products IDs and names and categories IDs and names. |
| 129 | + |
| 130 | +-- ProductID ProductName CategoryID CategoryName |
| 131 | +-- 1 Chai 1 Beverages |
0 commit comments