diff --git a/.vscode/settings.json b/.vscode/settings.json index 6581664..b7ebe78 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } ] } \ No newline at end of file diff --git a/notes/database-prog/sql-scripts/2024-01-31.sql b/notes/database-prog/sql-scripts/2024-01-31.sql index ece071b..ba730a0 100644 --- a/notes/database-prog/sql-scripts/2024-01-31.sql +++ b/notes/database-prog/sql-scripts/2024-01-31.sql @@ -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 \ No newline at end of file +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 \ No newline at end of file