SQL Tutorial


      Basics

    1. Introduction
    2. Database Structure

      Commands

    3. Create
    4. Select
    5. Insert
    6. Update
    7. Delete
    8. Drop

      Operations

    9. Joining Tables

      Appendices

    10. Terms
    11. Credits

Joining Tables

Databases can have redundant data eliminated by having multiple tables established to eliminate repetition of data within a single table. For example every city has a unique address for each house or building. In our example of a table with a cityname, state, zipcode, and population if we add an address, the name of the state, city, zipcode and population is repeated for every address in the city making the database many times larger than necessary. Therefore it is best to create the following two tables:

  • cityname state zipcode population
  • zipcode address

The zipcode is used to join the tables.

Example

SELECT citytable.cityname, citytable.state, addresstable.address
FROM citytable, addresstable
WHERE citytable.zipcode = addresstable.zipcode;
SELECT citytable.cityname, citytable.state, addresstable.address
FROM citytable INNER JOIN addresstable
WHERE citytable.zipcode = addresstable.zipcode;