Linking Multiple Tables Flashcards

1
Q

What is a relational database?

A

A relational database is a system where data is stored in tables. These tables are structured in a way that makes it easy to access and combine the data in various ways without needing to change the tables themselves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are the data in the tables related to each other?

A

The tables in a relational database are “related” to one
another through their primary and foreign keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a one-to-one relationship in a relational database?

A

In a one-to-one relationship, each row in one table is linked to one and only one row in another table. For example, an Employee ID belongs to only one name (or one record in another table).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a one-to-many relationship in a relational database?

A

In a one-to-many relationship, a single row in one table can be linked to multiple rows in another table, but a child
record can only have one parent record. For example a customer in the Customers table can have multiple orders in the Orders table. An artist can have multiple albums.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s a many-to-many relationship in a relational database?

A

Multiple rows in one table can be linked to multiple rows in another table. This relationship requires a third table, known as a junction or linking table, to connect the two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s an Entity Relationship Diagram?

A

An Entity-Relationship Diagram (ERD) visually represents how tables in a database are related to one another via primary and foreign keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you want to join tables, what clause must you use to join them from?

A

If you want to pull data from multiple tables then they must all be included in the FROM clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is the JOIN keyword used?

A

The JOIN keyword is used to define relationships between tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the ON Keyword in a JOIN clause?

A

The ON keyword determines the criteria for matching rows between the tables. Without the ON clause, the database would not know which columns to use for joining the tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does an INNER JOIN do?

A

It returns values that have records in both tables you’re joining.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain this example:

SELECT Orders.OrderID, Customers.Name
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

A

SELECT Statement:

Specifies which columns to retrieve from the combined result set.
Orders.OrderID: Retrieves the OrderID column from the Orders table.
Customers.Name: Retrieves the Name column from the Customers table.
FROM Clause:

Specifies the primary table (Orders) from which data will be retrieved.
Indicates the starting point of the query.
INNER JOIN Operation:

Combines data from two tables (Orders and Customers) based on a common column (CustomerID).
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID:
This line specifies that you want to join the Orders table with the Customers table based on matching CustomerID columns.
It ensures that each order (Orders) is associated with the corresponding customer (Customers) based on their CustomerID.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a LEFT OUTER JOIN and RIGHT OUTER and when is it used?

A

A LEFT OUTER JOIN returns all records from the table on left side of the join, and only records with a match on the right side of the join

Imagine you have two tables: one containing information about customers and their orders (Customers table), and another detailing payments made for those orders (Payments table). When you perform a LEFT OUTER JOIN from Customers to Payments, you are instructing the database to retrieve all records from the Customers table regardless of whether there are matching records in the Payments table.

Here’s how it works: Each row from the Customers table will appear in the result set, even if there are no corresponding payment records in the Payments table. If a customer has made payments (and thus has matching records in the Payments table), those payment details will be included alongside the customer’s information. However, if a customer has not made any payments, the fields related to payments in the result set will typically be NULL, indicating the absence of matching data from the Payments table

A RIGHT OUTER JOIN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s a FULL OUTER JOIN?

A

A FULL OUTER JOIN returns all records from the table on left side of the join, and all records from the right side of the join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s a A CROSS JOIN?

A

A CROSS JOIN matches every row in table A with every row in table B. It is used when you need to generate all possible combinations between two sets of data. It’s particularly useful for scenarios like generating product combinations, creating test datasets, or exploring every possible pairing between items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s the result of using AND and OR keywords with a join clause?

A

In SQL, the AND and OR keywords can be used after the ON keyword in a JOIN clause allows you to add additional conditions that determine which rows from the joined tables should be included in the result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why would you join a table?

A

The purpose of joining tables in SQL is to combine data from multiple tables into a single result set based on a related column or condition.

16
Q

What’s a self-join?

A

A self join is a specific type of SQL join where a table is joined with itself. This means that you treat a single table as if it were two separate tables for the purpose of the join operation. It’s useful when you want to compare rows within the same table, often to find related records or hierarchical relationships.

17
Q

What kind of joins can you do with a WHERE clause? How do you do it?

A

Only an inner join. You add the tables you are joining to the FROM clause, separated by commas. Use the WHERE operators

18
Q
A