W3. SQL Inner Join Flashcards
Q: What does the INNER JOIN keyword do?
A: It selects records with matching values in both tables.
Q: Write a query to join Products and Categories on CategoryID to select ProductID, ProductName, and CategoryName.
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Q: What happens to records without a match in an INNER JOIN?
A: They are not returned in the result.
Q: What is the syntax for using INNER JOIN?
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Q: Why is it good practice to specify table names when using JOIN?
A: To avoid ambiguity, especially when columns with the same name exist in both tables.
Q: Write a query that specifies table names to join Products and Categories on CategoryID.
SELECT Products.ProductID, Products.ProductName, Categories.CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Q: Are JOIN and INNER JOIN equivalent?
A: Yes, INNER JOIN is the default, so JOIN without INNER has the same effect.
Q: Write an example query using JOIN (without INNER) to join Products and Categories.
SELECT Products.ProductID, Products.ProductName, Categories.CategoryName
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Q: Can you JOIN more than two tables? If yes, how?
A: Yes, by joining additional tables in sequence using INNER JOIN with relevant keys.
Q: Write a query to join Orders, Customers, and Shippers to select OrderID, CustomerName, and ShipperName.
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);