SQL Review Part 2

Learning outcomes:

  • Write a SQL statement that joins multiple tables using foreign keys and primary keys
  • List resources for continuing to learn SQL
  • Describe how one-to-one and one-to-many relationships are used

Would you like to download my PowerPoint to follow along?

  • Table joins
    • Data is organized in tables
    • Tables will have a primary key for each item/record that is unique
    • Joining tables is so that you can get data from two tables
    • There are different types of joins depending how you want your data presented
    • The relationship of the tables is based on the primary key and foreign key
    • Joins are typically done between primary and foreign keys, but can involve other values as well
    • To have a join, you need a related column otherwise the join won't know how to work
  • Examples of Table joins
    • Let's say we have a table of books and a table of library patrons
    • From what we know about SQL so far we can look at the contents of each table individually and manipulate the data on each single table
    • Adding in joins allows us to look at combos, such as who borrowed a book or the borrowing history of each patron
    • SQL Join types explained visually
  • Why do we have so many table join types
    • Tables will have different relationships
    • The results of the different joins will show combinations of data
    • Depending on the problem we're trying to solve with our query we might need different combos of the data
    • For example, maybe we want to see the winner of a reading campaign. If we have a table of library patrons who signed up, and a table of books checked out for the campaign, we can do an inner join on them to see who the winner of the campaign is by ordering the results by descending(desc)
                      SELECT "Patrons"."Name", COUNT(*) 
                      FROM "Patrons" 
                      INNER JOIN "CampaignCheckouts" 
                      ON "Patrons"."PatronID" = "CampaignCheckouts"."Patron" 
                      GROUP BY "Patrons"."Name" 
                      ORDER BY COUNT(*) DESC";
    • SQL Join types explained visually
    • A Visual Explanation of SQL Joins, includes venn diagrams, sample SQL queries and example tables
  • Inner Join
    • Tables have columns of information you need to know the names of the columns so that you can figure out what needs to match for the join
    • An Inner Join is going to be where the information on tables A and B match looking at a particular column
    • This is ONLY where they match
    • Example:If we wanted to know patrons of the library who borrowed books in the last 6 months we would look at where patron's table and the books checkouts tables as seen in the example query on slide 1 or above
  • Outer Join
    • An Outer Join is all information in both tables including where they match
    • This is less common then inner joins
    • Example: We can take a books table and a patrons table and look at both, not all books have been checked out, not all patrons have borrowed a book, but we want all the data anyway, anything not filled in will have a NULL value
  • Left and Right Joins
    • Left Outer Join is all results from Table A including where there are matches on Table B
    • Left Outer join where we won't have the matches from Table B is done by using the WHERE to specify that if Table A has matches in Table B mark it as null in our results
    • We can do the same for the right with Right Outer Join by switching the order of the tables in the query
  • Where to go next for more SQL
  • NoSQL
    • NoSQL stands for Not Only SQL
    • NoSQL databases are not relational
    • Examples of NoSQL databases are document, key-value, graph, or wide-column stores
    • Many people argue NoSQL is more flexible and scalable then SQL based Databases
    • NoSQL can handle unstructured and semi-structured data
    • Examples of implementations are MongoDB, Redis and Cassandra
    • An example of where you might want to use NoSQL instead of SQL might be if you wanted a database of images or videos (Large Items)
    • Explanation of NoSQL by Google
  • Table Relationships
    • Relationships for tables are how connect to each other, so you can organize and link data across multiple tables
    • There are 4 types of relationships, One-to-one, One-to-many, Many-to-many, and self-referencing
    • Examples of relationships
      • One-to-one relationships
        • If we assume two tables, Table A and Table B
        • One-to-one means for each record in Table A, there is only one reference for Table B
        • The same is true for each record in Table B, there is only one reference for Table A
        • Example: There is only one person associated with each Employee profile
      • One-to-many relationships
        • Each record for Table A can have multiple associations with Table B. But each record for Table B only has 1 association with Table A
        • Example: There are many employees, but only a few departments
      • Many-to-many
        • Each record in table A can have multiple associations with Table B, and that goes both ways
        • Example: There are many students, in many classes
      • Self-referencing
        • A table foreign key references it's primary key
        • Sometimes referred to as a recursive relationship
        • Example: All employees are staff, including managers, but each employee also has a manager

Suggested Activities and Discussion Topics:

Would you like to see some more classes? Click here