Multiple-table queries

Previous Index

Queries from more than one table can be performed. Usually these tables share a common field.

Such queries are also carried out by SELECT statements, and the syntax is the same as that for single-table queries. However, field names appear in the expressions in such queries should contain the table name (as <table_name>.<field_name>) to avoid confusion.

The following examples show how a multiple-table query can be performed (click here to see the contents of the tables s6a and books).

Retrieve the students' names, books' Call IDs and due dates from the tables s6a and books where the Student ID in s6a is the same as that in books and the due date is before November 15, 1999. List these items in ascending order of the due dates.
SELECT s6a.name, books.call_id, books.due_date FROM s6a, books
  WHERE s6a.stud_id = books.stud_id AND books.due_date < {11/15/99}
  ORDER BY books.due_date ASC
Output:
name call_id due_date
Sung Hing Wah, Patrick 845678 10/20/1999
Chan Lai Yin 811141 10/24/1999
Yeung Chun 904568 10/29/1999
Lai Fung Chun 899554 10/30/1999
Fung Ching Man, Mandy 844221 10/31/1999
Yeung Chun 821344 11/01/1999
Chow Chi Ling 812112 11/01/1999
Poon Kwok Fai 853222 11/01/1999
Tang Wing Chi 887656 11/04/1999
Wong Ka Tak, Kent 844355 11/09/1999
Chan Wai Man, Raymond 804356 11/09/1999
Tang Wing Chi 891111 11/11/1999
Leung King 899943 11/12/1999
Lee Lai May 811332 11/13/1999
Hung Wai Ming 804890 11/14/1999
Yeung Chun 845223 11/14/1999
 
Retrieve the students' names, books' Call IDs, email addresses and due dates from the tables s6a and books where the Student ID in s6a is the same as that in books and the due date is before November 1, 1999. List these items in desscending order of the due dates.
SELECT s6a.name, s6a.email, books.call_id, books.due_date FROM s6a, books
  WHERE s6a.stud_id = books.stud_id AND books.due_date < {11/01/99}
  ORDER BY books.due_date DESC
Output:
name

email

call_id due_date
Fung Ching Man, Mandy mandyfung@example.com 844221 10/31/1999
Lai Fung Chun s96374@sample.edu.hk 899554 10/30/1999
Yeung Chun cyeung@testing.com.hk 904568 10/29/1999
Chan Lai Yin cly@testing.com.hk 811141 10/24/1999
Sung Hing Wah, Patrick patricksung@example.com 845678 10/20/1999

Previous Index