Skip to content

Instantly share code, notes, and snippets.

View dnasca's full-sized avatar

Derrik dnasca

View GitHub Profile
@dnasca
dnasca / joining_more_than_2_tables.sql
Created January 27, 2016 09:33
Joining more than two tables
--INNER JOIN multi-table join example
SELECT
p.Name AS ProductName,
pc.Name AS CategoryName,
ps.Name AS SubcategoryName
FROM
Production.Product AS p
INNER JOIN
@dnasca
dnasca / multi-attribute_joins.sql
Created January 27, 2016 09:18
Multi-attribute Join - The join condition can involve more than one column from each input. Multi-attribute joins are commonly used for PK/FK associations between data sources involving more than one attribute on each side
--which rows are in the production table that are not in the archive table?
SELECT
bea.BusinessEntityId,
bea.AddressId,
bea.AddressTypeId,
bea.rowguid,
bea.ModifiedDate
FROM
@dnasca
dnasca / equi_vs_non-equi.sql
Created January 27, 2016 09:13
Equi vs Non-Equi Join
SELECT
sod.SalesOrderId,
sod.SalesOrderDetailId,
sod.ProductId,
sod.OrderQty,
so.SpecialOfferId,
sod.ModifiedDate,
so.StartDate,
so.EndDate,
so.Description
@dnasca
dnasca / self_joins.sql
Created January 27, 2016 09:07
Self Joins - joining a data source to itself
SELECT
e.BusinessEntityId,
e.HireDate,
e.ManagerId,
m.HireDate
FROM
HumanResources.Employee AS e
LEFT OUTER JOIN
@dnasca
dnasca / cross_join.sql
Created January 27, 2016 09:03
Cross Joins - cross-product of two data sources, aka a Cartesian product. Each row from a data source is matched with ALL rows in the other data source. (DataSource1 * DataSource2)
--how many rows?
SELECT COUNT(*)
FROM HumanResources.Employee;
--what gets returned?
SELECT e.BusinessEntityId, edh.DepartmentId
FROM HumanResources.Employee AS e
CROSS JOIN HumanREsources.EmployeeDepartmentHistory AS edh;
--practical use?
@dnasca
dnasca / outer_joins.sql
Created January 27, 2016 08:56
Outer Joins
--Put the predicate that determines the join condition in the ON clause (ie. p.ProductId = od.ProductId)
--Put the predicate that specifies the outer row filter in the WHERE clause
--LEFT OUTER returns all rows from the left table that match the right table and also rows from the left table that do NOT match
SELECT
p.Name,
od.SalesOrderDetailId,
od.OrderQty
FROM
@dnasca
dnasca / inner_join.sql
Created January 27, 2016 08:43
Inner Joins - take 2 data sources and return all matching pairs of rows based on the join condition. unmatched rows are discarded
--ansi sql-92 standard syntax (filter rows in the ON clause)
SELECT
p.Name,
od.ProductId,
od.SalesOrderDetailId,
od.OrderQty
FROM
Production.Product AS p
@dnasca
dnasca / querypaging.sql
Created January 27, 2016 08:12
query paging
-- returning the first 25 rows
SELECT
e.FirstName,
e.LastName,
e.AddressLine1
FROM
HumanResources.vEmployee AS e
ORDER BY
@dnasca
dnasca / predicatestoagroup.sql
Created January 27, 2016 08:02
Applying predicates to a group
-- applying predicates to a group
SELECT
SOD.ProductID,
SOD.SpecialOfferID,
SUM(SOD.OrderQty) AS OrderQtyByProductId
FROM
Sales.SalesOrderDetail AS SOD
@dnasca
dnasca / array_cleanup.js
Created December 9, 2015 19:35
jq array iterating, removing white space, and tolowercase
$.each(parsedCsv, function (key, val) {
key = key.toLowerCase().replace(/\s+/g, '');
$('#pool').append('<li id="' + key + '"class="list-group-item">' + val + '</li>');
});