Skip to main content
Engineering LibreTexts

4.8: Joins

  • Page ID
    23880
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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:

    1. For the current row in Loan:
      1. Write down the call number of this loan.
      2. Let N stand for the value of the member`s id for this loan.
      3. Now look at the Member listing row by row starting with the first row:
        1. 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.
    2. 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:

    1. Create a new query
    2. Right-click the Relationships Area and select both the Member and Loan tables from the popup window:
      Relationships line.
    3. 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.
    4. Select the call number, first name, and last name fields by double-clicking them to obtain:
      Selected fields.
    5. Run the query and you see the results:

      callNo

      firstName

      lastName

      HQ 1143 P68 1975

      John

      Smith

      QA 76.73 S67C46 1997

      David

      Martin

      R 141 E45 2006

      David

      Martin

      R 141 E45 2006

      Betty

      Freeman

    Exercises

    In each of the following exercises the necessary data is in more than one table and so it is necessary to specify a join.

    Exercise \(\PageIndex{1}\)

    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.

    Exercise \(\PageIndex{2}\)

    Modify the previous query to produce a listing that is in order by title and then by date.

    Exercise \(\PageIndex{3}\)

    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.

    Exercise \(\PageIndex{4}\)

    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

    Exercise \(\PageIndex{5}\)

    Modify the previous query to produce a listing that is in order by last name and then by first name.

    Exercise \(\PageIndex{6}\)

    For member id 2, list the person`s name and the titles borrowed.

    Exercise \(\PageIndex{7}\)

    Produce a list of book titles and member names for those books that are due back May 18, 2014.

    Exercise \(\PageIndex{8}\)

    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.


    This page titled 4.8: Joins is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ron McFadyen.

    • Was this article helpful?