Home > Articles > Data > SQL

Joining Tables in SQL Queries

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book
This lesson teaches the reader how to join multiple tables in a single query in order to optimize the use of the data stored in the database. The ability to join tables in SQL allows greater flexibility in database design and offers more avenues of approach and usages for data.

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 columns—primary 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.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus