CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Laboratory Exercise on Joins of Multiple SQL Tables

Overview: This laboratory exercise is the second of four labs that provides practice with processing that involves joining tables in a database.

Background

As preparation for this lab, be sure you have read pages 201-207 in the textbook.

Joining Three (or more) Tables

The previous lab discussed the use of the JOIN operator to bring two tables, using the general framework:

  SELECT * FROM Table1 JOIN Table2 ON Table1.field=Table2.field

SQL allows three to be joined, by joining two and then the third. This may be accomplished in several ways:

Notes on JOIN Operators

Examples here and in the previous lab have used both ON and WHERE clauses:

The discussions throughout this course focus upon the INNER JOIN (or JOIN) operator that returns only records that have common fields that match. If a record in one table has no matches in the other, that record is not reported in the JOIN.

For those interested in further reading, SQL also provides some other operators, such as the following:

Faculty and Courses

  1. Each of the following SQL statements combine information about faculty, courses, and the class schedule. Type each statement into MySQL and describe the outcome.

    select * from faculty JOIN courseSchedule ON faculty.facultyID=courseSchedule.facultyID JOIN courses ON courseSchedule.courseID=courses.courseID;
    
    select * from (faculty JOIN courseSchedule ON faculty.facultyID=courseSchedule.facultyID) JOIN courses ON courseSchedule.courseID=courses.courseID;
    
    select * from faculty JOIN (courseSchedule JOIN courses ON courseSchedule.courseID=courses.courseID) ON faculty.facultyID=courseSchedule.facultyID;
          
  2. Choose one of these queries, and modify it to obtain only the faculty member's first and last name and the course program, number and title. (e.g., change SELECT * to indicate the needed fields).

  3. Add a WHERE clause to one of the queries in step 1 to obtain only those courses from instructor Walker.

  4. Revise one of the queries in step 1 to obtain the published course schedule for computer science for Spring, as found in the Registrar's course listings. For this problem, you need not achieve the same format as on the Registrar's page, and you may omit course enrollments/capacities, prerequisites, and section number. Also, the listing may include either the instructor's first initial or first name, as you wish.

Grocery Store Inventory

The previous lab utilized a grocery table that indicated various grocery items, including their category and price. Suppose the grocery store chain selling these items has several separate shops, perhaps:

  1. Salem downtown
  2. West Salem
  3. Sunnyside
  4. Keizer
  5. Four Corners

Each grocery item is available in at least one of these shops, but not all items are available everywhere. Thus, the MySQL database contains two more tables:

  1. Create and run an SQL query that joins the storeGrocery table with the stores table, based on storeID, and joins that result with the groceries table, based on itemID and id.

  2. Modify the previous SQL query to find the inventory for store 1.

  3. Modify the SQL query in step 5 to determine which store(s) stock "Cauliflower Florets".

  4. Modify the SQL query in step 5 to determine which stores stock some type of bakery goods (these have id numbers between 48 and 52, inclusive).
    (Note: The largest ID number in the current grocery database is 52, so bakery items involve any id >= 48.)

  5. Modify the SQL query in step 5 to determine which store(s) stock some type of dairy products (these have id numbers between 34 and 47, inclusive).




created 22 February 2019
revised 23 February 2019
Valid HTML 4.01! Valid CSS!