SQL Multiple Tables Flashcards
why might we want to use multiple tables in a database schema?
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.”
what is joining two tables?
will combine rows from different tables if the join condition is true.
what is the purpose of a JOIN sequence?
to combine 2 tables
When doing an INNER JOIN, are columns matched on NULL values?
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.
What happens if you try to perform a join but the rows don’t match?
When we perform a simple JOIN (often called an inner join) our result only includes rows that match our ON condition.
When we write a JOIN query, does the order we specify the tables matter?
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.
what is a left join?
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.
what is the syntax of the left join?
select *
from table1
left join table2
on table1.c2=table2.c2
where the last line specifie which columns to match the table on.
when should we use inner join vs. left join?
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.
what are the requirements for a primary key?
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.
define foreign key
When the primary key for one table appears in a different table
what is a common type of join to be familiar with?
The most common types of joins will be joining a foreign key from one table with the primary key from another table.
what is a cross join?
lets us combine all rows of one table with all rows of another table.
can you cross join more than 2 tables?
Yes, you can CROSS JOIN as many tables as you want.
what are the 2 rules for a union (appending data to other data)?
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.