Click here to Skip to main content
15,890,123 members
Articles / Database Development / SQL Server

How Do I Combine Results from Several SQL Tables (Hint: There Are Three Ways)

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
14 Aug 2019MIT5 min read 8.4K   2   1
How to combine results from more than one table

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 get started using these free tools using my Guide Getting Started Using SQL Server.

What Are the Ways I Can Combine Results From More Than One Query?

SQL wouldn’t be a very useful language if it didn’t provide an easy means for you to combine results from more than one query. Fortunately, there are three main ways you can combine data from multiple tables. We’ll go over these briefly here and provide links to more in-depth articles.

Three Main Ways to Combine Data

Data in relational database tables are organized into rows and columns. As we investigate ways to combine data, keep in mind that the end result will be to either add more columns to a result, perhaps from another related table, or rows, by taking a set of rows from two or more tables.

When most people learn to combine data, they learn about:

  1. JOIN – You can use joins to combine columns from one or more queries into one result.
  2. UNION – Use Unions and other set operators to combine rows from one or more queries into one result.
  3. Sub Queries – Sometimes called nested queries, these can be used to perform a separate search in the database showed results can be used in another query.

Joins

I like to think of joins as the glue that put the database back together. Relational databases are usually normalized to make the data easier to maintain and to improve performance, but the end result is information is separated into many tables. You can use Joins to recombine that information back together into a more human readable format. The data is recombined by matching columns from each table.

In all cases, joins require two main ingredients: Two tables and a join condition. The tables are what we will use to pull the rows and columns and the join condition is how we intend on matching the columns between tables.

Example JOIN

SQL
SELECT  Person.FirstName,
        Person.LastName,
        PersonPhone.PhoneNumber
 FROM   Person.Person
        INNER JOIN Person.PersonPhone
        ON Person.BusinessEntityID = 
           PersonPhone.BusinessEntityID

There are two main types of joins, Inner Joins and Outer Joins.

Inner Joins only return a resulting row if the join condition matches in both tables. Inner joins are mainly used to match the primary key of one table with a foreign key in another.

The second type of join is an outer join. Outer joins always return at least one row for the main table, referred to as the Left or Right table, and null values in the corresponding columns of the non-matching column. Outer joins are useful for finding non-matching data.

It is important to note that joins can return more rows than those that exist in either table combined. The joins return combinations of matches. If you join two tables, on containing 5 row, and the other 10, the result may contain anywhere from 0 to 50 rows depending on the join condition.

Unions

A UNION is used to combine the rows of two or more queries into one result. Union is called a set operator.

There are some special conditions that must occur in order for a union to work. First, each query must have the same number of columns. Second, the data types of these columns must be compatible. Generally speaking, each query must return the same number and type of columns.

A practical example of union is when two tables contain part numbers and you want to create a combine list for a catalogue. You can either elect to have the end result be a unique listing for the combine query or if you use UNION ALL return all rows from each table.

Example UNION

SQL
SELECT C.Name
FROM   Production.ProductCategory AS C
UNION
SELECT S.Name
FROM   Production.ProductSubcategory AS S

In addition to Union, there are a couple of other handy set operators:

  • INTERSECT – You can use this to only return rows that are common between two tables.
  • EXCEPT – You can use this to return rows that exist on one table, but aren’t found in another.

As you go on to learn more SQL, you find that you can use joins to write equivalent statements for Intersect, and Except, but there are no equivalents for Union.

Sub Queries

Sub queries are sometimes called nested queries. They are queries defined inside of other queries. Sub queries can be confusing. I think a lot of this stems for the fact they can be used in many places in a SQL select statement, and for several purposes!

For example, here are some areas where you may see a sub query:

  • SELECT clause – Used to return a value. For instance, if you’re querying a sales table, you could include the total sales by returning a sum of all sales from within a sub query.
  • WHERE clause – Sub queries can be used in the where clause in comparisons. You could set up a comparison to compare sales to the overall average. The overall average would be returned from a sub query. You can also use sub queries in membership operators such as IN. Rather than hard-coding the in clause, you can use a sub query to make it more dynamic.
  • HAVING clause – A single value from a sub query is included in the HAVING clause comparisons.

Example Sub query

SQL
SELECT SalesOrderID,
       LineTotal,
       (SELECT AVG(LineTotal)
        FROM Sales.SalesOrderDetail) AS AverageLineTotal
FROM   Sales.SalesOrderDetail

When used in select clauses and comparison operators such as equals, greater than, and less than, a sub query can only return one row. If used in conjunction with a membership operator, such as IN, it is OK for the query to return one or more rows.

The post How do I combine results from several SQL tables (hint: there are three ways) appeared first on Essential SQL.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
QuestionSQL combine the results of two tables Pin
Corina Catalina26-Oct-21 1:00
Corina Catalina26-Oct-21 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.