Combine Tables Archives

There are many ways you can combine tables together.

This page is a collection of articles going over set operators, joins, sub queries, and common table expressions.  All are great techniques you can learn today:

Use set operators when you wish to combine rows from separate tables together.

Joins come in handy when you want to combine columns from two or more tables.  Use joins to combine columns from two, three, or more tables.  Also, learn the different way joins can match data and influence which row are included in your result.

I love using subqueries to avoid hard coding values in my where clause.  Once you get good with subqueries, you’ll see there are a great way to avoid “hard-coding” specific values into your queries.

Finally, when you combine tables together, your statements can get real funky and unmanageable.  Use Common Table Expressions to make the more readable!

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the sample databases AdventureWorks and WideWorldImporters.  You can get started using these free tools using my guide Getting Started Using SQL Server.


Is it possible to use Joins, Subqueries, and Unions in one Query?

A reader recently asked me to help them solve a problem combining data from multiple tables into a single result.  What was interesting was that the solution used the three ways to combine data that we have recently covered, namely:  JOINS, set operators, and subqueries. Given the comprehensive nature of this problem I thought it […]

Continue reading

How do I combine results from several SQL tables (hint: there are three ways)

Combine data in SQL

Many times in a relational database the information you want to show in your query is in more than one table.  This begs the question “How do you combine results from more than one table?” All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can […]

Continue reading

SQL Inner Join to Two or More Tables

Use an SQL INNER JOIN when you need to match rows from two tables.  Rows that match remain in the result, those that don’t are rejected. The match condition is commonly called the join condition.  When the match conditions involve equality, that is matching exactly the contents of one column to another, the sql inner […]

Continue reading