SQL Joins: Inner, Left, Right, Outer

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which type of SQL join returns only the rows that have matching values in both tables?

  • Inner Join (correct)
  • Full Outer Join
  • Right Join
  • Left Join

A left join returns all rows from the right table and the matching rows from the left table, with NULL values for non-matching columns from the left table.

False (B)

In a SQL query, what values are returned when there is no match between the tables in a FULL OUTER JOIN?

null

The word 'OUTER' is ______ in LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.

<p>optional</p> Signup and view all the answers

Match the type of SQL join with their respective description:

<p>Inner Join = Returns rows only when there is a match in both tables. Left Join = Returns all rows from the left table, and matching rows from the right table. If no match, fill with NULL. Right Join = Returns all rows from the right table, and matching rows from the left table. If no match, fill with NULL. Full Outer Join = Returns all rows from both tables. If no match, fill with NULL.</p> Signup and view all the answers

Which join type is essentially a combination of a left join and a right join?

<p>Full Outer Join (D)</p> Signup and view all the answers

Switching the order of tables in a LEFT JOIN clause will always produce the exact same result.

<p>False (B)</p> Signup and view all the answers

Consider two tables: Customers and Orders. You want to retrieve all customers and any orders they may have placed. If a customer has not placed any orders, you still want their information included in the result. Which join type should you use?

<p>Left Join (B)</p> Signup and view all the answers

Tables can be ______ using the AS keyword to shorten the table names in a query.

<p>aliased</p> Signup and view all the answers

What type of diagram is commonly used to help visualize the operation of different SQL joins?

<p>venn diagrams</p> Signup and view all the answers

Flashcards

Inner Join

Returns only rows with matching values in both tables; it's the intersection.

Left Join

Returns all rows from the left table, and matching rows from the right. Non-matching right table data will be NULL.

Right Join

Returns all rows from the right table, and matching rows from the left. Non-matching left table data will be NULL.

Full Outer Join

Returns all rows from both tables, with NULL values for non-matching columns.

Signup and view all the flashcards

SQL Joins

Combines rows from two or more tables based on a related column.

Signup and view all the flashcards

The word 'OUTER'

Optional keyword in LEFT, RIGHT, and FULL OUTER JOIN clauses.

Signup and view all the flashcards

Aliasing Tables

Using AS keyword to shorten table names in a query.

Signup and view all the flashcards

WHERE clause in OUTER JOINs (ON clause)

Filters records before they are joined to another table

Signup and view all the flashcards

Study Notes

Types of SQL Joins

  • SQL joins are categorized into four primary types: inner, left, right, and full outer.
  • Joins serve to combine rows from multiple tables based on a shared column.
  • Venn diagrams provide a useful way to visually represent the mechanics of different joins.

Inner Join

  • Inner joins yield only the rows with matching values across all joined tables.
  • This join type returns the intersection of the tables involved.
  • Rows lacking a corresponding match in the other table are excluded from the final result.
  • For example, joining a customer table and a city table on a city ID will return only the customers who have a matching city in the city table.
  • SELECT * FROM customer INNER JOIN city ON customer.cityID = city.cityID; is the SQL syntax.

Left Join

  • Left joins return all rows from the left table and only matching rows from the right table
  • If a match is absent in the right table, NULL values populate the columns from the right table
  • It returns all the matches, plus everything from the left table, even if there is no match in the right table.
  • For instance, a left join would include Mary Smith in the results, but the city information would be NULL if no match exists.
  • SELECT * FROM customer LEFT JOIN city ON customer.cityID = city.cityID; is the SQL syntax.
  • In the SQL query, the "left" table refers to the table positioned on the left side of the JOIN clause.

Right Join

  • Right joins return all rows from the right table and only matching rows from the left table.
  • NULL values populate the columns from the left table when no match is found.
  • Includes all matches, plus everything from the right table, even if there is no match in the left table
  • For example, using a right join, Houston would be included in the results, but the customer information would be NULL if no match exists.
  • SELECT * FROM customer RIGHT JOIN city ON customer.cityID = city.cityID; is the SQL syntax.
  • Switching the order of tables in the FROM clause allows rewriting a right join as a left join.

Full Outer Join

  • Full outer joins return all rows from both tables, regardless of matches.
  • Returns matches, as well as all records from both tables where there isn't a match
  • With no match between tables, NULL values fill the columns from the non-matching table.
  • It is essentially a combination of left and right joins.
  • For example, Mary Smith and Houston would be included, with NULL values for the columns in the non-matching table
  • SELECT * FROM customer FULL OUTER JOIN city ON customer.cityID = city.cityID; is the SQL syntax.

Additional notes on Joins

  • The word "OUTER" is optional in LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.
  • Tables can be aliased using the AS keyword to shorten table names within the query.
    • SELECT * FROM customer AS cs INNER JOIN city AS cy ON cs.cityID = cy.cityID; illustrates table aliasing.
  • Filtering with the WHERE clause can affect the results differently depending on whether it is used with an inner join or an outer join.
  • When using a WHERE clause with an OUTER JOIN, placing the filter on the ON clause filters the table before the join happens, potentially leading to different results
  • The order of tables in LEFT and RIGHT JOIN clauses can be switched to achieve the same result.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser