SQL Multiple Tables Flashcards

1
Q

why might we want to use multiple tables in a database schema?

A

if you store a lot of data in columns and have many entries (rows) much of this data will be repeated which will make your table hard to work with. this is called “avoiding data redundancy.”

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

what is joining two tables?

A

will combine rows from different tables if the join condition is true.

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

what is the purpose of a JOIN sequence?

A

to combine 2 tables

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

When doing an INNER JOIN, are columns matched on NULL values?

A

No, when you have NULL values in a column, these are never matched to other NULL values. This is because NULL signifies the absence of any value, and cannot be compared as they will never equal anything. Doing say NULL = NULL results in False.

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

What happens if you try to perform a join but the rows don’t match?

A

When we perform a simple JOIN (often called an inner join) our result only includes rows that match our ON condition.

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

When we write a JOIN query, does the order we specify the tables matter?

A

Generally, no, the order of the tables in the JOIN will not affect the overall results of the query.

As long as you specify what columns to select, the results should appear essentially the same, just that the rows will be ordered according to the appearance in the first table.

However, if you do not specify specific columns, then the order of columns will be different depending on the order of the tables. Without specifying columns in your SELECT, by using just SELECT *, the result query will order the columns by the first table’s columns followed by the second table’s columns.

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

what is a left join?

A

ill return every row in the left table, and if the join condition is not met, NULL values are used to fill in the columns from the right table.

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

what is the syntax of the left join?

A

select *
from table1
left join table2
on table1.c2=table2.c2

where the last line specifie which columns to match the table on.

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

when should we use inner join vs. left join?

A

Generally, we use INNER JOIN when we want to select only rows that match an ON condition. If no rows match the ON condition, then it will not return any results. This can be somewhat stricter than using a LEFT JOIN.

We use a LEFT JOIN when we want every row from the first table, regardless of whether there is a matching row from the second table. This is similar to saying,
“Return all the data from the first table no matter what. If there are any matches with the second table, provide that information as well, but if not, just fill the missing data with NULL values.”

In a way, LEFT JOIN is less strict than INNER JOIN. Furthermore, the results of a LEFT JOIN will actually include all results that an INNER JOIN would have provided for the same given condition.

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

what are the requirements for a primary key?

A

None of the values can be NULL.
Each value must be unique (i.e., you can’t have two customers with the same customer_id in the customers table).
A table can not have more than one primary key column.

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

define foreign key

A

When the primary key for one table appears in a different table

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

what is a common type of join to be familiar with?

A

The most common types of joins will be joining a foreign key from one table with the primary key from another table.

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

what is a cross join?

A

lets us combine all rows of one table with all rows of another table.

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

can you cross join more than 2 tables?

A

Yes, you can CROSS JOIN as many tables as you want.

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

what are the 2 rules for a union (appending data to other data)?

A

1 Tables must have the same number of columns.
2 The columns must have the same data types in the same order as the first table.

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

What happens if tables we perform UNION on have duplicate rows?

A

duplicate rows will be excluded.

17
Q

what if you want to union tables and include duplicates?

A

some versions of sql have the UNION ALL operator.

18
Q

what does a WITH statement do?

A

allows us to perform a separate query (such as aggregating customer’s subscriptions)

Essentially, we are putting a whole first query inside the parentheses () and giving it a name. After that, we can use this name as if it’s a table and write a new query using the first query.

19
Q

what does Previous_results mean?

A

previous_results is the alias that we will use to reference any columns from the query inside of the WITH clause

20
Q

what is a union?

A

UNION stacks one dataset on top of another.

21
Q

what is a with statement?

A

WITH allows us to define one or more temporary tables that can be used in the final query.

22
Q

when do we use a join/inner join statement?

A

Use JOIN, or INNER JOIN, when you want to strictly select rows of data that match some condition, provided by an ON clause. For example, if we had tables of employee information, and require their company information, we could JOIN the tables together so that we only obtain results that match, and provide us all the information we need, excluding results that are missing information.

23
Q

when do we use a left join statement?

A

LEFT JOIN can be used when you want to see all the results from the first table no matter what, but also want to include matches, if any, with the second table. For example, say we had a table for customers and another table for purchase information. If we wanted to obtain all customer information, and any purchase information, then a LEFT JOIN might be useful. If the customers made no purchases, their information will still be returned.

so all of left table and matches, if any, with right table. if no data in right table corresponding to left table’s records, left table’s info will still be returned.

24
Q

when do we use a cross join?

A

CROSS JOIN can be used when you want to get all possible combinations of rows from a table with other tables. A simplified example of using this would be, say we wanted to get every possible combination of a meal, which consists of an appetizer, main course, and dessert. Using a CROSS JOIN can give us every possible combination.

25
Q

when do we use a union?

A

Although not necessarily a JOIN, UNION can be used when you wish to combine multiple tables together quickly. One helpful feature of a UNION is that it will only return unique rows, so there will be no duplicates in the combined table.

26
Q

what is the syntax of a cross join?

A

SELECT * FROM [Table1] CROSS JOIN [Table2]

NOTE: There is no need for a join condition as all rows are returned.

27
Q

what is the union syntax?

A

SELECT *
FROM table1
UNION
SELECT *
FROM table2

28
Q

what is the difference between join/inner join and left join?

A

left join combines rows from two or more tables, but unlike inner join, does not require the join condition to bet met.

29
Q
A