Joining Tables in SQL Queries
- Oct 25, 2002
See all Sams Teach Yourself on InformIT Database Tutorials.
To this point, all database queries you have executed have extracted data from a single table. During this hour, you learn how to join tables in a query so that data can be retrieved from multiple tables.
The highlights of this hour include
An introduction to the table join
The different types of joins
How and when joins are used
Numerous practical examples of table joins
The effects of improperly joined tables
Renaming tables in a query using an alias
Selecting Data from Multiple Tables
Having the capability to select data from multiple tables is one of SQL's most powerful features. Without this capability, the entire relational database concept would not be feasible. Single-table queries are sometimes quite informative, but in the real world, the most practical queries are those whose data is acquired from multiple tables within the database.
As you witnessed in the hour on normalization, a relational database is broken up into smaller, more manageable tables for simplicity and the sake of overall management ease. As tables are divided into smaller tables, the related tables are created with common columnsprimary keys. These keys are used to join related tables to one another.
A join combines two or more tables to retrieve data from multiple tables.
You might ask why you should normalize tables if, in the end, you are only going to rejoin the tables to retrieve the data you want. You rarely select all data from all tables, so it is better to pick and choose according to the needs of each individual query. Although performance may suffer slightly due to a normalized database, overall coding and maintenance are much simpler.