Sql basics joi ns and common commands (1)

603 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
603
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql basics joi ns and common commands (1)

  1. 1. SQL BasicsCommon Commands and JOINs
  2. 2. Common Commands
  3. 3. Common Command Example TablesSharkTbl ID Name Street 1 Alf 111 Street 2 Barry 222 Street 3 Charlie 333 Street 4 Dirk 444 StreetWhaleTblfe 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
  4. 4. Multi Table SELECTsThey look like this:SELECT *FROM SharkTbl, WhaleTbl;But you can run into trouble if they sharecommon column names.
  5. 5. Table Defined ColumnsThis is going to break...SELECT Name, Street FROM SharkTbl, WhaleTbl;...because both SharkTbl and WhaleTbl have columnsnamed Name and Street.Result:
  6. 6. Table Defined ColumnsFor more than one table, use this format:SELECT <tbl 1>.<col 1>, <tbl 2>.<col 2>FROM <tbl 1>, <tbl 2>Heres how it looks in action:SELECT SharkTbl.Name, WhaleTbl.StreetFROM SharkTbl, WhaleTbl;
  7. 7. Table Defined ColumnsIf some columns are unique and some areambiguous, you can only include tabledefinitions where you must, like so:SELECT Greeting, SharkTbl.NameFROM SharkTbl, WhaleTbl;
  8. 8. The "AS" Column Alias CommandYou use this to clean up table names.Structure:SELECT <tbl 1>.<col 1> AS <alias 1>FROM <tbl 1>, <tbl 2>
  9. 9. The "AS" Column Alias CommandExample:SELECT SharkTbl.Name AS Pudding,WhaleTbl.Street AS BrowniesFROM SharkTbl, WhaleTbl;Changes The Column Names:
  10. 10. "AND" vs "OR"
  11. 11. "AND" vs "OR"AND Concept:If A and B, then trueYouve got to have both A and B before true
  12. 12. "AND" vs "OR"OR Concept:If A or B, then trueHaving either A or B will make it trueYou dont need both, just one or the other.
  13. 13. "AND" vs "OR"This makes OR a more wide searchThis makes AND a narrow and exact search
  14. 14. "AND" vs "OR"Lets run some examples in phpMyAdminstarting with this query:SELECT Name, StreetFROM SharkTblWHERE Street = 222 StreetOR Name = Barry;
  15. 15. The "AND" CommandWhat It Looks Like:SELECT Name, StreetFROM SharkTblWHERE Street = 222 StreetAND Name = Barry;
  16. 16. The "AND" CommandRules:ANDs are an extension of the WHEREcommand.You can use as many as you please, one percondition.
  17. 17. The "OR" CommandWhat It Looks Like:SELECT Name, StreetFROM SharkTblWHERE Street = 222 StreetOR Name = Barry;ORs are also an extension of the WHEREcommand.
  18. 18. COUNT CommandSay you want a count of the number of rowsreturned instead of the raw data.Structure:SELECT COUNT(<col 1>)FROM <tbl 1>;
  19. 19. COUNT CommandExample:SELECT COUNT(Name) AS NameCountFROM SharkTbl;Returns:
  20. 20. COUNT CommandLets try it without the count to double check:SELECT NameFROM SharkTbl;Yup, it works as 4 rows are returned:
  21. 21. JOINsThe Greatest SQL Command
  22. 22. JOIN Example TablesPersonTbl ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2JobTbl ID Name 1 Antenna Assessor 2 Bear Barber 3 Chorizo Chef 4 Database Diver
  23. 23. 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
  24. 24. The Shape Of Things In Our World● Orders ○ Products ■ Product Attributes ■ Manufacturers ■ Billing Codes ■ Part Numbers ○ Addresses ○ Tracking Numbers ○ Faxes ○ Notes
  25. 25. The Shape Of Things In Our WorldSo, just about anything you look at can bebroken down into "A has Bs which have Cs":● As ○ have Bs ■ which in turn have Cs ○ have Fs ○ have Gs ○ have Hs
  26. 26. The Shape Of Things In Our WorldTo turn this pattern into databases:1. Make A, B and C into tables2. Relate the tables to one another3. Describe any object or relation of objects
  27. 27. The Shape Of Things In Our WorldThese three things allow table relations:1. Primary Keys2. Foreign Keys3. JOINs in your SQL statements
  28. 28. Primary KeysRemember that tables have IDs?Like so: ID Name 1 Alf 2 Barry 3 Charlie 4 DirkAll the IDs we have seen so far are formallyknown as Primary Keys.
  29. 29. Primary KeysPrimary keys are the IDs used to identify a row.PersonTbl ID Name 1 Alf 2 Barry 3 Charlie 4 Dirk ^ Primary key of the PersonTbl
  30. 30. Foreign KeysPrimary Keys are going to reference rows inthis tableForeign Keys are going to reference rows inanother tableSometimes called Secondary Keys
  31. 31. Foreign KeysThey look like this: JobID is a Foreign Key... ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2...and ID is the Primary Key
  32. 32. Foreign KeysWhy? Because: JobID talks about another table ID Name JobID 1 Alf 3 2 Barry 3 3 Charlie 1 4 Dirk 2...but ID talks about this table
  33. 33. Foreign KeysFormal:Foreign Keys are keys that relate back to thePrimary Keys of other tables.Boiled Down:You can use the JobID in PersonTbl to "lookup" values in JobTbl
  34. 34. Use A Foreign Key, Get A Value
  35. 35. Use A Foreign Key, Get A ValueSo, what just happened?● PersonTbls 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!
  36. 36. Use A Foreign Key, Get A ValueBig Concept:The arrow is the JOIN action.
  37. 37. Use A Foreign Key, Get A ValueThe arrow drawn between PersonTbl andJobTbl represents a JOIN.A JOIN needs to know which Foreign Key inPersonTbl relates to which Primary Key inJobTbl.
  38. 38. Breaking Down A JOINStructure:SELECT * FROM <table 1>JOIN <table 2>ON <table 1>.<col 1> = <table 2>.<col 2>
  39. 39. Breaking Down A JOINSELECT * 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
  40. 40. Breaking Down A JOINIn English:Select all columns from the one or more tablesnamed where the primary key of one table isequal to the secondary key of the other.Further boiled down:Replace the JobID in PersonTbl with its namein JobTbl
  41. 41. Breaking Down A JOINLive SQL Example:SELECT PersonTbl.ID,PersonTbl.Name AS PersonName, JobTbl.Name AS JobNameFROM PersonTblJOIN JobTblON PersonTbl.JobID = JobTbl.ID;
  42. 42. Breaking Down A JOINResult:Boiled Down Concept:We replaced JobIDs with Job Names usingJOIN
  43. 43. Implicit vs Explicit JOIN StatementsImplicit:SELECT * FROM PersonTbl, JobTblWHERE PersonTbl.JobID = JobTbl.IDExplicit:SELECT * FROM PersonTblINNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID
  44. 44. Implicit vs Explicit JOIN StatementsMy examples have all be explicit.Explicit is better, because the reader does nothave to guess if or how you are joining.You write it out on this line:INNER JOIN JobTbl on JobTbl.ID = PersonTbl.JobID
  45. 45. JOINs Put Another WayWhen getting your head around JOINs, it isgood to hear the same thing in many differentways.Heres a lady on youtube explaining it:http://www.youtube.com/watch?v=oWHO4lJlX54
  46. 46. Real World Example From MadCowGoal:Query all the available sizes of the ComfortGelOriginal
  47. 47. Real World Example From MadCowPartsTbl PNum PName 1802 ComfortGel Original Nasal CPAP Mask with HeadgearPartsAttributeTbl PAID PNum Description 44 4266 Petite 45 4266 Small 46 4266 Medium 47 4266 Large
  48. 48. Real World Example From MadCowThe SQL:SELECT PartsTbl.PNum, PartAttributeTbl.PAID, PartsTbl.PName, PartAttributeTbl.Description FROM PartsTblJOIN PartAttributeTblON PartsTbl.PNum = PartAttributeTbl.PNumWHERE PartsTbl.PNum = 1802
  49. 49. Real World Example From MadCowThe Result:
  50. 50. Real World Example From MadCowBreak Down This SQL:SELECT PartsTbl.PNum, PartAttributeTbl.PAID, PartsTbl.PName, PartAttributeTbl.Description FROM PartsTblIt Means:Select columns from PartsTbl
  51. 51. Real World Example From MadCowBreak Down This SQL:JOIN PartAttributeTblIt Means:We are also wanting columns from thePartAttributeTbl
  52. 52. Real World Example From MadCowBreak Down This SQL:ON PartsTbl.PNum = PartAttributeTbl.PNumIt Means:Match up the PNums in both tables
  53. 53. Real World Example From MadCowBreak Down This SQL:WHERE PartsTbl.PNum = 1802It Means:Look only for PNum 1802, which is theComfortGel Originals PartTbl PNum ID.
  54. 54. For The Next SQL Class...● Advanced JOINs ○ INNER ○ LEFT ○ RIGHT● UNIONS● More Helpful Commands● Advanced Real World Examples
  55. 55. Classes Next ThursdayThurs Feb 21st @ NoonJohn Nelson of IT presenting Virtualization 101.Someone from Marketing presenting...something.

×