SELECT i.InvoiceID FROM Invoices i LEFT CLEAVE Vendors v ON i.VendorID = v.VendorID
This commit is contained in:
parent
a2f205fc4e
commit
2034b9602c
2 changed files with 44 additions and 1 deletions
15
.vscode/settings.json
vendored
15
.vscode/settings.json
vendored
|
@ -30,6 +30,21 @@
|
|||
"database": "WideWorldImporters",
|
||||
"username": "ben",
|
||||
"password": "pass"
|
||||
},
|
||||
{
|
||||
"mssqlOptions": {
|
||||
"appName": "SQLTools",
|
||||
"useUTC": true,
|
||||
"encrypt": true
|
||||
},
|
||||
"previewLimit": 50,
|
||||
"server": "localhost",
|
||||
"port": 1433,
|
||||
"driver": "MSSQL",
|
||||
"name": "server (Examples)",
|
||||
"database": "Examples",
|
||||
"username": "ben",
|
||||
"password": "pass"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -32,3 +32,31 @@ FROM Invoices
|
|||
JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
|
||||
WHERE PaymentDate IS NULL
|
||||
ORDER BY InvoiceDueDate
|
||||
|
||||
-- Get all vendors with invoices
|
||||
-- inter-JOINs only return rows that match on both tables
|
||||
SELECT DISTINCT v.VendorID, VendorName FROM Vendors v
|
||||
JOIN Invoices i ON v.VendorID = i.VendorID
|
||||
|
||||
-- Outer-JOIN: Returns all of a table plus anything in the other
|
||||
-- Get all vendors and invoices if they have any
|
||||
SELECT v.VendorID, VendorName, InvoiceID FROM Vendors v
|
||||
LEFT JOIN Invoices i ON v.VendorID = i.VendorID
|
||||
-- LEFT or RIGHT determines which is the "main one" that the other is joined to
|
||||
|
||||
-- CROSS JOIN - combine the tables???
|
||||
SELECT v.VendorID, VendorName, InvoiceId
|
||||
FROM Vendors v CROSS JOIN Invoices i
|
||||
ORDER BY VendorID
|
||||
|
||||
-- Get employees and their managers
|
||||
SELECT e.EmployeeID, e.LastName, e.FirstName, m.FirstName + ' ' + m.LastName ManagerName
|
||||
FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID
|
||||
|
||||
-- Customers table has FK Cities, Cities FK State, State FK Countries
|
||||
-- To get full address of customers
|
||||
SELECT c.CustomerId, c.CustomerName, PostalAddressLine1, ci.CityName, sp.StateProvinceName, cty.CountryName
|
||||
FROM Sales.Customers c
|
||||
LEFT JOIN application.Cities ci ON c.DeliveryCityID = ci.CityID
|
||||
JOIN application.StateProvinces sp ON sp.StateProvinceID = ci.StateProvinceID
|
||||
JOIN application.Countries cty ON cty.CountryID = sp.CountryID
|
Loading…
Reference in a new issue