Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blog post on SQL joins. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions posts/sql-joins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: "SQL Joins"
tags:
- sql
- joins
- database
date: "2019-10-27T05:44:08.920Z"
layout: post
---
#### Author: [Kimberly Collins](https://github.com/kacollins)

## Database Normalization
* Separate data into different tables
* Usually represent each entity with an ID
* Optimize for data storage efficienty, not human readability

## Purposes of Joins
* Use data from multiple tables
* "Un-normalize" data
* Make data human readable
* View information about the entity, not just the ID

## How to Get Started
* Install SQL Server Management Studio or another query tool (maybe LinqPad!)
* Install AdventureWorks sample database

## Types of Joins

### [CROSS JOIN](https://clips.twitch.tv/HelpfulRelatedMarrowMikeHogu)
* Usage
* Returns all possible combinations of rows between two tables
* If one table has 2 rows and the other has 3 rows, cross joining them returns 6 rows
* Used for generating test data and for reports
* Otherwise, use it as a last resort
* Example
* Department table has 16 rows
* Shift table has 3 rows
* CROSS JOIN will return the 48 possible combinations

### [INNER JOIN](https://clips.twitch.tv/UnsightlyRichDogeDuDudu)
* [Usage](https://clips.twitch.tv/AmusedGracefulLampRickroll)
* Returns rows that match on criteria
* Can (unintentionally) limit results or introduce duplicates
* "INNER" is optional but recommended
* Generally joining tables on one or more columns that exist in both
* Usually the primary key in one table and a foreign key in the other
* Other criteria can be used
* Example
* ProductSubcategory table has a column for ProductCategoryID
* We want to see the name of the product category for each subcategory, not the ID

### [LEFT OUTER JOIN](https://clips.twitch.tv/PlumpGoldenDaikonStrawBeary)
* Usage
* Returns rows in the first table even if they don't have a match in the second table
* Can unintentionally introduce duplicates
* "OUTER" is optional
* Often used with a WHERE clause to only get the rows in the first table that don't have a match in the second table
* Examples
* Show the subcategory for each product, if it has one
* Get the products that have never been sold (with WHERE clause)

### RIGHT OUTER JOIN
* Analogous to LEFT OUTER JOIN
* Rarely used

### FULL OUTER JOIN
* Usage
* Returns rows from both tables even if they don't have matches in the other
* "OUTER" is optional
* Often used for comparison reports
* Example
* Show salespeople assigned to each territory
* Include salespeople not assigned to any territory
* Include territories without any salespeople assigned

## Learning More
* [Slides](https://docs.google.com/presentation/d/1JzqXzFljkzZ5gTVrgzIV7Hhgv70Ffpi2unwnNWxbyjE/edit)
* [Video](https://www.youtube.com/watch?v=2IGQFucnGR4)
* [Examples](github.com/kacollins/intro-to-sql-joins)
* OKC SQL meetup group - meets on the third Monday evening of the month
* Techlahoma's Slack
* Sign up at slack.techlahoma.org
* #databases and #okcsql channels
* @kimberlycollins

-- [Kimberly](https://github.com/kacollins)