+ All Categories
Home > Documents > Sql basics joi ns and common commands (1)

Sql basics joi ns and common commands (1)

Date post: 25-Dec-2014
Category:
Upload: johnnygoodman
View: 202 times
Download: 7 times
Share this document with a friend
Description:
 
56
SQL Basics Common Commands and JOINs
Transcript
Page 1: Sql basics  joi ns and common commands (1)

SQL BasicsCommon Commands and JOINs

Page 2: Sql basics  joi ns and common commands (1)

Common Commands

Page 3: Sql basics  joi ns and common commands (1)

SharkTbl

WhaleTblfe

Common Command Example Tables

ID Name Street

1 Alf 111 Street

2 Barry 222 Street

3 Charlie 333 Street

4 Dirk 444 Street

ID Name Street Greeting

1 Antenna Assessor 5 Ave howdy

2 Bear Barber 6 Ave hello

3 Chorizo Chef 7 Ave hi

4 Database Diver 8 Ave salve citizen

Page 4: Sql basics  joi ns and common commands (1)

Multi Table SELECTs

They look like this:

SELECT * FROM SharkTbl, WhaleTbl;

But you can run into trouble if they share common column names.

Page 5: Sql basics  joi ns and common commands (1)

Table Defined Columns

This is going to break...

SELECT Name, Street FROM SharkTbl, WhaleTbl;

...because both SharkTbl and WhaleTbl have columns named Name and Street.

Result:

Page 6: Sql basics  joi ns and common commands (1)

Table Defined Columns

For more than one table, use this format:

SELECT <tbl 1>.<col 1>, <tbl 2>.<col 2>FROM <tbl 1>, <tbl 2>

Here's how it looks in action:

SELECT SharkTbl.Name, WhaleTbl.Street FROM SharkTbl, WhaleTbl;

Page 7: Sql basics  joi ns and common commands (1)

Table Defined Columns

If some columns are unique and some are ambiguous, you can only include table definitions where you must, like so:

SELECT Greeting, SharkTbl.NameFROM SharkTbl, WhaleTbl;

Page 8: Sql basics  joi ns and common commands (1)

The "AS" Column Alias Command

You use this to clean up table names.

Structure:

SELECT <tbl 1>.<col 1> AS <alias 1>FROM <tbl 1>, <tbl 2>

Page 9: Sql basics  joi ns and common commands (1)

The "AS" Column Alias Command

Example:

SELECT SharkTbl.Name AS Pudding, WhaleTbl.Street AS BrowniesFROM SharkTbl, WhaleTbl;

Changes The Column Names:

Page 10: Sql basics  joi ns and common commands (1)

"AND" vs "OR"

Page 11: Sql basics  joi ns and common commands (1)

"AND" vs "OR"

AND Concept:

If A and B, then true

You've got to have both A and B before true

Page 12: Sql basics  joi ns and common commands (1)

"AND" vs "OR"

OR Concept:

If A or B, then true

Having either A or B will make it true

You don't need both, just one or the other.

Page 13: Sql basics  joi ns and common commands (1)

"AND" vs "OR"

This makes OR a more wide search

This makes AND a narrow and exact search

Page 14: Sql basics  joi ns and common commands (1)

"AND" vs "OR"

Lets run some examples in phpMyAdmin starting with this query:

SELECT Name, Street FROM SharkTblWHERE Street = '222 Street'OR Name = 'Barry';

Page 15: Sql basics  joi ns and common commands (1)

The "AND" Command

What It Looks Like:

SELECT Name, Street FROM SharkTblWHERE Street = '222 Street'AND Name = 'Barry';

Page 16: Sql basics  joi ns and common commands (1)

The "AND" Command

Rules:

ANDs are an extension of the WHERE command.

You can use as many as you please, one per condition.

Page 17: Sql basics  joi ns and common commands (1)

The "OR" Command

What It Looks Like:

SELECT Name, Street FROM SharkTblWHERE Street = '222 Street'OR Name = 'Barry';

ORs are also an extension of the WHERE command.

Page 18: Sql basics  joi ns and common commands (1)

COUNT Command

Say you want a count of the number of rows returned instead of the raw data.

Structure:

SELECT COUNT(<col 1>) FROM <tbl 1>;

Page 19: Sql basics  joi ns and common commands (1)

COUNT Command

Example:

SELECT COUNT(Name) AS NameCountFROM SharkTbl;

Returns:

Page 20: Sql basics  joi ns and common commands (1)

COUNT Command

Lets try it without the count to double check:

SELECT NameFROM SharkTbl;

Yup, it works as 4 rows are returned:

Page 21: Sql basics  joi ns and common commands (1)

JOINsThe Greatest SQL Command

Page 22: Sql basics  joi ns and common commands (1)
Page 23: Sql basics  joi ns and common commands (1)

JOIN Example Tables

PersonTbl

JobTbl

ID Name JobID

1 Alf 3

2 Barry 3

3 Charlie 1

4 Dirk 2

ID Name

1 Antenna Assessor

2 Bear Barber

3 Chorizo Chef

4 Database Diver

Page 24: Sql basics  joi ns and common commands (1)

The Shape Of Things In Our World

● Oceans○ Swimming Creatures

■ Orcas● Transient● Resident● Offshore

■ Sharks● Hammer Heads● Great White Sharks

■ Other Less Interesting Fish○ Reefs

■ Coral ■ Cement

Page 25: Sql basics  joi ns and common commands (1)

The Shape Of Things In Our World

● Orders○ Products

■ Product Attributes■ Manufacturers■ Billing Codes■ Part Numbers

○ Addresses○ Tracking Numbers○ Faxes○ Notes

Page 26: Sql basics  joi ns and common commands (1)

The Shape Of Things In Our World

So, just about anything you look at can be broken down into "A has Bs which have Cs":

● As○ have Bs

■ which in turn have Cs○ have Fs○ have Gs○ have Hs

Page 27: Sql basics  joi ns and common commands (1)

The Shape Of Things In Our World

To turn this pattern into databases:

1. Make A, B and C into tables

2. Relate the tables to one another

3. Describe any object or relation of objects

Page 28: Sql basics  joi ns and common commands (1)

The Shape Of Things In Our World

These three things allow table relations:

1. Primary Keys

2. Foreign Keys

3. JOINs in your SQL statements

Page 29: Sql basics  joi ns and common commands (1)

Primary Keys

Remember that tables have IDs?

Like so:

All the IDs we have seen so far are formally known as Primary Keys.

ID Name

1 Alf

2 Barry

3 Charlie

4 Dirk

Page 30: Sql basics  joi ns and common commands (1)

Primary Keys

Primary keys are the IDs used to identify a row.

PersonTbl

^ Primary key of the PersonTbl

ID Name

1 Alf

2 Barry

3 Charlie

4 Dirk

Page 31: Sql basics  joi ns and common commands (1)

Primary Keys are going to reference rows in this table

Foreign Keys are going to reference rows in another table

Sometimes called Secondary Keys

Foreign Keys

Page 32: Sql basics  joi ns and common commands (1)

Foreign Keys

They look like this:

JobID is a Foreign Key...

...and ID is the Primary Key

ID Name JobID

1 Alf 3

2 Barry 3

3 Charlie 1

4 Dirk 2

Page 33: Sql basics  joi ns and common commands (1)

Foreign Keys

Why? Because:

JobID talks about another table

...but ID talks about this table

ID Name JobID

1 Alf 3

2 Barry 3

3 Charlie 1

4 Dirk 2

Page 34: Sql basics  joi ns and common commands (1)

Formal:

Foreign Keys are keys that relate back to the Primary Keys of other tables.

Boiled Down:

You can use the JobID in PersonTbl to "look up" values in JobTbl

Foreign Keys

Page 35: Sql basics  joi ns and common commands (1)

Use A Foreign Key, Get A Value

Page 36: Sql basics  joi ns and common commands (1)

Use A Foreign Key, Get A Value

So, what just happened?

● PersonTbl's 4th row had a JobID of 2

● We looked the JobTbl row with ID of 2

● The name of JobTbl ID = 2 is Bear Barber

● So JobID 2 is the same as Bear Barber!

Page 37: Sql basics  joi ns and common commands (1)

Use A Foreign Key, Get A Value

Big Concept:

The arrow is the JOIN action.

Page 38: Sql basics  joi ns and common commands (1)

Use A Foreign Key, Get A Value

The arrow drawn between PersonTbl and JobTbl represents a JOIN.

A JOIN needs to know which Foreign Key in PersonTbl relates to which Primary Key in JobTbl.

Page 39: Sql basics  joi ns and common commands (1)

Breaking Down A JOIN

Structure:

SELECT * FROM <table 1>JOIN <table 2>ON <table 1>.<col 1> = <table 2>.<col 2>

Page 40: Sql basics  joi ns and common commands (1)

Breaking Down A JOIN

SELECT * FROM <table 1>JOIN <table 2>ON <table 1>.<col 1> = <table 2>.<col 2>

What each part does:

SELECT...FROM - Read all cols from table 1JOIN - Read all cols from table 2 as wellON - Use these keys to link the two tables

Page 41: Sql basics  joi ns and common commands (1)

Breaking Down A JOIN

In English:

Select all columns from the one or more tables named where the primary key of one table is equal to the secondary key of the other.

Further boiled down:

Replace the JobID in PersonTbl with its name in JobTbl

Page 42: Sql basics  joi ns and common commands (1)

Breaking Down A JOIN

Live SQL Example:

SELECT PersonTbl.ID, PersonTbl.Name AS PersonName, JobTbl.Name AS JobName FROM PersonTblJOIN JobTblON PersonTbl.JobID = JobTbl.ID;

Page 43: Sql basics  joi ns and common commands (1)

Breaking Down A JOIN

Result:

Boiled Down Concept:

We replaced JobIDs with Job Names using JOIN

Page 44: Sql basics  joi ns and common commands (1)

Implicit vs Explicit JOIN Statements

Implicit:

SELECT * FROM PersonTbl, JobTblWHERE PersonTbl.JobID = JobTbl.ID

Explicit:

SELECT * FROM PersonTblINNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID

Page 45: Sql basics  joi ns and common commands (1)

Implicit vs Explicit JOIN Statements

My examples have all be explicit.

Explicit is better, because the reader does not have to guess if or how you are joining.

You write it out on this line:

INNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID

Page 46: Sql basics  joi ns and common commands (1)

JOINs Put Another Way

When getting your head around JOINs, it is good to hear the same thing in many different ways.

Here's a lady on youtube explaining it:

http://www.youtube.com/watch?v=oWHO4lJlX54

Page 47: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

Goal:

Query all the available sizes of the ComfortGel Original

Page 48: Sql basics  joi ns and common commands (1)

PartsTbl

PartsAttributeTbl

Real World Example From MadCow

PNum PName

1802 ComfortGel Original Nasal CPAP Mask with Headgear

PAID PNum Description

44 4266 Petite

45 4266 Small

46 4266 Medium

47 4266 Large

Page 49: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

The SQL:

SELECT PartsTbl.PNum, PartAttributeTbl.PAID, PartsTbl.PName, PartAttributeTbl.Description FROM PartsTblJOIN PartAttributeTbl ON PartsTbl.PNum = PartAttributeTbl.PNumWHERE PartsTbl.PNum = 1802

Page 50: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

The Result:

Page 51: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

Break Down This SQL:

SELECT PartsTbl.PNum, PartAttributeTbl.PAID, PartsTbl.PName, PartAttributeTbl.Description FROM PartsTbl

It Means:

Select columns from PartsTbl

Page 52: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

Break Down This SQL:

JOIN PartAttributeTbl

It Means:

We are also wanting columns from the PartAttributeTbl

Page 53: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

Break Down This SQL:

ON PartsTbl.PNum = PartAttributeTbl.PNum

It Means:

Match up the PNums in both tables

Page 54: Sql basics  joi ns and common commands (1)

Real World Example From MadCow

Break Down This SQL:

WHERE PartsTbl.PNum = 1802

It Means:

Look only for PNum 1802, which is the ComfortGel Original's PartTbl PNum ID.

Page 55: Sql basics  joi ns and common commands (1)

For The Next SQL Class...

● Advanced JOINs○ INNER○ LEFT○ RIGHT

● UNIONS● More Helpful Commands● Advanced Real World Examples

Page 56: Sql basics  joi ns and common commands (1)

Classes Next Thursday

Thurs Feb 21st @ Noon

John Nelson of IT presenting Virtualization 101.

Someone from Marketing presenting...something.


Recommended