chapter eight

8 Querying multiple tables

 

Back in chapter 2, we discussed how relational database management systems (RDBMSes) store data in objects known as tables, and since then, we’ve been examining ways to query these tables. I don’t know whether you’ve been wondering what makes an RDBMS “relational,” but I’m going to answer that very question.

One of the primary features of an RDBMS is that it allows a set of data to be stored so that it can relate to other sets of data—hence, the use of the word relational. This way of storing data is incredibly powerful because we can not only put the data we have in logical groupings of tables but can also easily retrieve related data from multiple tables with a single query.

Retrieving data this way is done by joining tables, which means combining the data in two tables using the values that form the relationship between the tables. Although joining tables is common and relatively easy, you must follow some specific rules to get the desired results. You’ll soon learn those rules and see how to write join tables in SQL correctly. First, though, you need to consider a few vital concepts of relational databases and the way they are designed.

8.1 The rules of data relationships

8.1.1 Data without relationships

8.1.2 Data with relationships

8.2 The way to join data

8.2.1 Joining two tables

8.2.2 Joining more tables

8.3 Table aliases

8.4 The other way to join data

8.5 Lab

8.6 Lab answers