Viewers: in countries Watching now:
In this course, author Adam Wilbert illustrates how to create and leverage real-world queries and turn raw data into usable information. The course covers setting up queries, performing calculations, using the built-in Access functions to further refine query results, and identifying top performers or areas for improvement based on a range of criteria.
The Union query is a special type of query within Access and it's used to combine two different data tables with the same structure. Let's open up our Chapter 8 custom group and we'll open up our Employees table. This table houses all the information about our in-company employees. We can see that we have 200 employees represented in this table. The FieldReps table has information about our contracted field representatives. If I open up that, you'll see that it has an identical structure to the Employees table.
We have a total of 100 field representatives. We can use a Union query to take the 100 field reps and the 200 employees and merge them together in a single query record set. Let's go ahead and close both of these tables and we'll see how that's done. I'll go into the Create tab and I'll create a new query in Design view. I'll go ahead and select my Employees table and say Close. Now I want to select all of my employees from this table and I can use this shortcut by double-clicking on the asterisk to choose all of these fields.
Now the Union query is only created in SQL View. You can't make the Union query using the Design View in Access. So what we can do is switch to SQL view and finish our statements there. This is the SELECT statement that Access uses to select all of the fields from the Employees table. We can use this as a guide to help us type out how to select all of the records from the FieldReps table, and we'll join both of those using a Union statement. So at the end of this-- I'll jump down two lines so it's easy to read-- I'll write the word Union.
I'll move down another two lines and I'll type in this SQL statement to select everything from the FieldReps table. We can look to the Employees table above to help us with the syntax. So let's write out the statement. First we'll start with SELECT, and then we want to tell Access what it is that we want to select. We want to select the table FieldReps. tbl_FieldReps. And we want to select all of the fields in the FieldReps table, so we'll out in a period and then an asterisk. Finally, we have to tell Access from what table we want to select from and it does seem a little bit redundant, but this is the syntax that we're going to be using.
FROM tbl_FieldReps. Finally, we'll add a semicolon to finish our statement. So now we've got a statement that takes all of the employees information and adds that to all of the field reps information. When I run this query, you'll see we have a total of 300 records and I'll scroll to the right. We'll see we have our employees listed on the top. And if we scroll down, we'll see that those are joined or unioned with the field sales reps.
Now we have a single query result that has all of our associated employees, whether they're internal within the company or whether they're contracted employees. Now that we have both tables merged together and functioning as one, we can save this query out and use it as the input to other queries down the road. There's no reason why we would need to physically merge the data from both tables at all. We can use a Union query to join both tables in a record set without having to duplicate that information in a new table within our database.
There are currently no FAQs about Access 2010: Queries in Depth.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.