Initial commit

This commit is contained in:
askiiart 2024-01-31 20:20:15 -06:00
commit 41868ff6c7
Signed by untrusted user who does not match committer: askiiart
GPG key ID: BC3800E55FB54D67
7 changed files with 349 additions and 0 deletions

View file

@ -0,0 +1,105 @@
-- T-SQL
-- Keywords - UPPERCASE, Tables/Columns/etc - PascalCase, everything else - doesn't matter
-- Comments with double-hyphen
USE AP
GO
SELECT VendorID, VendorName, LEFT(VendorName, 10) VendorShortName,
VendorAddress1, VendorAddress2, VendorCity
FROM Vendors
-- batch run everything before it
-- GO 2
SELECT *
FROM Terms
SELECT InvoiceID, InvoiceNumber, InvoiceTotal, PaymentTotal, CreditTotal,
CASE PaymentTotal
WHEN InvoiceTotal THEN 1
ELSE 0
END IsPaidFull
FROM Invoices
-- includes credit
SELECT InvoiceID, InvoiceNumber, InvoiceTotal, PaymentTotal, CreditTotal, InvoiceTotal - CreditTotal PaidTotal,
CASE PaymentTotal
WHEN InvoiceTotal - CreditTotal THEN 1
ELSE 0
END IsPaidFull
FROM Invoices
-- better
SELECT TOP 500
InvoiceID, InvoiceNumber, InvoiceTotal, PaymentTotal, CreditTotal, InvoiceTotal - CreditTotal PaidTotal,
CASE WHEN PaymentTotal = InvoiceTotal - CreditTotal THEN 1
ELSE 0
END IsPaidFull
FROM Invoices
ORDER BY VendorID, InvoiceDueDate
SELECT TOP 50 InvoiceID, VendorID FROM Invoices
ORDER BY VendorID
-- 2024-01-29
-- paging
-- skip first offset rows, return next fetch rows
SELECT InvoiceID, InvoiceNumber, InvoiceDueDate
FROM Invoices
ORDER BY InvoiceDueDate
OFFSET 110 ROWS
FETCH NEXT 10 ROWS ONLY
-- Filtering
-- just return what you need, instead of filtering in the application
SELECT InvoiceID, VendorID, InvoiceDueDate, InvoiceTotal
FROM Invoices
-- Note that equality is a single equals sign
WHERE InvoiceTotal >= 1000 OR VendorID = 110
ORDER BY VendorID, InvoiceDueDate
SELECT VendorID, VendorName, VendorCity, VendorState
FROM Vendors
-- Note strings use apostrephes, not quotation marks
-- Keep in mind *there is no short-circuit evaluation*, so this will run every single thing and it's messy and slow
--WHERE VendorState = 'CA' OR VendorState = 'AZ' OR VendorState = 'OH' OR VendorState = 'NY'
-- Instead, use IN
WHERE VendorState IN ('CA', 'AZ', 'OH', 'NY')
ORDER BY VendorName
SELECT InvoiceID, InvoiceNumber, InvoiceTotal
FROM Invoices
--WHERE InvoiceTotal >= 100 AND InvoiceTotal <= 500
--less typing and more optimized
--keep in mind it's an inclusive between, not exclusive
WHERE InvoiceTotal BETWEEN 100 AND 500
ORDER BY InvoiceDueDate
-- Haven't paid
-- if either VendorAddress1 or VendorAddress2 are `NULL`, then the concatenation will just be `NULL`
-- so this filters out all the nulls
-- (note that the VendorAddress1 one isn't actually needed because the dataset doesn't contain any that has VendorAddress1 as `NULL` but VendorAddress2 defined)
SELECT VendorID, VendorName, VendorAddress1, VendorAddress2, VendorAddress1 + ' ' + VendorAddress2 FROM Vendors
WHERE VendorAddress1 IS NOT NULL AND VendorAddress2 IS NOT NULL
-- or ISNULL to just return a string instead of NULL
SELECT VendorID, VendorName, VendorAddress1, VendorAddress2, ISNULL(VendorAddress1, '') + ' ' + ISNULL(VendorAddress2, '') FROM Vendors
-- or COALESCE
SELECT VendorID, VendorName, VendorAddress1, VendorAddress2, COALESCE(VendorAddress1, '') + ' ' + COALESCE(VendorAddress2, '') FROM Vendors
-- All vendors with PO Boxes
SELECT VendorID, VendorName, VendorAddress1, VendorAddress2 FROM Vendors
-- '%PO BOX%' is equivalent to '.*PO BOX.*' in regex
WHERE VendorAddress1 LIKE '%PO BOX%' OR VendorAddress2 LIKE '%PO BOX%'
-- Get current date
--SELECT GETDATE(), GET UTCDATE()
-- Get all invoices for January
SELECT InvoiceId, InvoiceNumber, InvoiceDueDate FROM Invoices
WHERE MONTH(InvoiceDueDate) = 1
--WHERE DAY(InvoiceDueDate) IN (30, 31)
-- Get all invoices for 2019
--WHERE YEAR(InvoiceDeDate) = 2019
-- More efficient version, also more flexible, but longer
--WHERE InvoiceDueDate BETWEEN '1/1/2019' AND '12/31/2019'

View file

@ -0,0 +1,19 @@
-- #### Start of class stuff ####
-- Get the InvoiceId, InvoiceTotal, and InvoiceDueDate for all invoices for vendor 110
SELECT InvoiceId, InvoiceTotal, InvoiceDueDate
FROM Invoices
WHERE VendorID = 110
-- Get the latest 10 invoices for vendor 123: InvoiceId, InvoiceDueDate, InvoiceTotal
SELECT TOP 10 InvoiceId, InvoiceTotal, InvoiceDueDate
FROM Invoices
WHERE VendorID = 123
ORDER BY InvoiceDueDate DESC
-- Get the InvoiceId, VendorId, InvoiceDueDate, and InvoiceTotal for all invoices due between 1/1/2020 and 1/31/2020
SELECT InvoiceId, VendorId, InvoiceDueDate, InvoiceTotal
FROM Invoices
WHERE InvoiceDueDate BETWEEN '1/1/2020' AND '1/31/2020'
-- ######################################################################## --

View file

@ -0,0 +1,44 @@
# SQL Syntax
## Keywords
`|`: Use for designating different things that can be used as arguments for the keywords
`( )`: Optional keywords
`[ ]`: Used for values, like integers, or column or table names.
| Keyword | Explanation | Example |
| ------- | ----------- | ------- |
| `USE` | Specify the database to be used | `USE WideWorldImporters` |
| `SELECT {ColumnID \| *}` | selects the column to return (or can use wildcard `*`) | `SELECT ProductID FROM Products` |
| `FROM` | specify the thing to be selected from |
| `GO {count}` | Runs everything before it `count` times minus 1 | `GO 5` (runs everything before it 4 more times) |
| `TOP {n} (PERCENT - optional)` | Returns the top *n* (or *n*%) results - must be an integer . Order is undefined, but in most implementations is listed in the order they were inserted - NOT guaranteed behavior, just how most implementations use it. | `SELECT TOP 10 PERCENT VendorName FROM Vendors` |
| `ORDER BY {column1, column2 (1 required, can use infinitely many)}` | Orders by some column (alphanumerically sorted, letters first); `TOP` is useless without this | `SELECT TOP 50 InvoiceID, VendorID FROM Invoices ORDER BY VendorID` |
| `DISTINCT` | Goes before the column names (and before TOP in MS SQL server, at least); removes rows with duplicate values | `SELECT DISTINCT VendorName FROM Invoices` |
<!-- Needs to be redone into more detailed sub-headings -->
## `CASE`/`WHEN`/`THEN`/`ELSE`/`END`
## Other syntax
- Brackets `[ ]`: Delineates that something is a table, usually not needed but allows for stuff like using keywords as table names.
## Other notes
- `SELECT * FROM Table`: Shouldn't be used normally, only in learning or if you're not sure what you need yet.
- Bad because: it's requests every column of data in the table, and that's rarely needed, wasting time and resources.
- Most database systems will just skip all qury optimizations if you do this, wasting even more time and resources.
- Column ordering is undefined - the columns could be returned in any random order.
- Should usually do one keyword per line, except for simple stuff like `SELECT ColumnID FROM Table`
- Column aliases: Useful for:
- Multi-table connections and for "renaming" columns
- Making it more convenient to use (e.g. `Person` -> `Name` as an alias of `Person` -> `PersonName`)
- To name an unnamed column (e.g. `SELECT LEFT(VendorName, 10) VendorShortName FROM Vendors`)
## No
Don't:
- Put spaces in a table's name (why would you???)
- Use `SELECT * FROM Table`