If a query must be answered using data that appears in more than one table then the query requires a database join.
Suppose we wish to produce a list of member names and the call numbers of books they have borrowed. Important points about this query:
- The Loan table has the loan information we need
- The Member table has the member names we need.
Before we compose the query consider how you would produce the results if you were to do this manually. If you had two listings showing the rows of each table in front of you on your desk you could proceed as follows going through the Loan table listing row by row starting with the first row:
- For the current row in Loan:
- Write down the call number of this loan.
- Let N stand for the value of the member`s id for this loan.
- Now look at the Member listing row by row starting with the first row:
- Examine the row to determine if the row is for member N, and if it is, write down the member`s name beside the call number.
- If there are more rows in Loan, advance to the next row and go back to step 1
In the above algorithm we have determined a member id at step 1.2) and we next look for a matching member id in step 1.3). For a human the process is simple but tedious. We could say we are trying to go from a row in Loan to a row in Member based on rows having the same value for member id. In database terminology we say we are joining Loan to Member based on a common value of member id. A tedious but well-defined task is something a computer can excel at, and fortunately we can get the database system to do the job of joining rows, based on values of a common attribute, for us. Construct the query in the following way:
- Create a new query
- Right-click the Relationships Area and select both the Member and Loan tables from the popup window:
- Note the line connecting the two tables. This is called a relationships line which causes MS Access to join pairs of rows - a row in Member is joined to a row in Loan where the two rows have the same value for id.
- Select the call number, first name, and last name fields by double-clicking them to obtain:
- Run the query and you see the results:
HQ 1143 P68 1975
QA 76.73 S67C46 1997
R 141 E45 2006
R 141 E45 2006
In each of the following exercises the necessary data is in more than one table and so it is necessary to specify a join.
For each loan show the title of the book and the date it was borrowed. Note that the title is in the Book table and the date borrowed is in the Loan table.
Modify the previous query to produce a listing that is in order by title and then by date.
Produce a list that shows for each loan the book title, the name of the member who borrowed the book, and the dates the book was borrowed and then returned. Note that 3 tables are needed for this query.
Produce a list of members and the books they have taken out on loan. Include the member’s last name, first name, and titles of the books. The information to be displayed is in 2 tables, but it is necessary to specify 3 tables for this query:
Member joins to Loan
Book joins to loan
Modify the previous query to produce a listing that is in order by last name and then by first name.
For member id 2, list the person`s name and the titles borrowed.
Produce a list of book titles and member names for those books that are due back May 18, 2014.
Produce a list of book titles and member names for those books that have not been returned. In this case you must give the criteria for dateReturned as null. Null is a special keyword that represents no value.