Complex joins
1. Complex joins
What do you do when the data you need to answer a question is scattered across many tables in your database? How do you find it and put it together?2. A complex question
Let's take this example question: you are interested in finding out how many videos were rented in each city. What data will you need to answer this question?3. Connecting the data
A good start would be the rental table. This contains information about rentals but nothing about what city they were rented in.4. Connecting the data
This means you need to find another table that contains city information.5. Connecting the data
The address table appears to be a good source. However, there is no overlap in columns between these two tables and hence there isn't a direct way to connect them.6. Connecting the data
so there is likely a need to use an intermediate table to unite them.7. Entity Relationship Diagram (ERD)
The table that does this is the customer table. As you can see it has the customer_id column from the rental table and the address_id column from the address table. Joining these three tables together will give you the opportunity you need to determine how many videos were rented in each city. By following this process of identifying the correct tables you need for your query and visually drawing connections between them we just created what is known as an entity relationship diagram or ERD for short. The ERD can be an invaluable tool for making sense of the tables in your database. In some cases this diagram may be available to you, in others you would have to build one for the question at hand.8. Tools for finding your data
With the tools you learned in this chapter you can do exactly that. You can explore your database to find the data elements that are necessary to answer the questions you have.9. Your turn!
Now it's your turn to use these tools to build an entity relationship diagram and answer a question requiring multiple joins.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.