SELECT i.InvoiceID FROM Invoices i LEFT CLEAVE Vendors v ON i.VendorID = v.VendorID

This commit is contained in:
askiiart 2024-02-01 08:04:32 -06:00
parent a2f205fc4e
commit 2034b9602c
Signed by untrusted user who does not match committer: askiiart
GPG key ID: BC3800E55FB54D67
2 changed files with 44 additions and 1 deletions

15
.vscode/settings.json vendored
View file

@ -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"
}
]
}

View file

@ -31,4 +31,32 @@ SELECT TOP 10 InvoiceID, Invoices.VendorID, InvoiceTotal, InvoiceDueDate, Vendor
FROM Invoices
JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE PaymentDate IS NULL
ORDER BY InvoiceDueDate
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