How to use MS Access SQL to combine 2 queries or tables
Microsoft Structured Query Language allows the direct manipulation of tables, queries and data contained in a Microsoft Access database as well as other file formats. SQL statements can be used to modify the contents of tables or queries, or to delete information that meets a certain criteria. Combine two separate sources of data into a new table or query or display data in a different format or sequence by setting up a SQL Select statement that includes criteria for each source.
Instructions
-
-
1
Launch Microsoft Access 2010 and open a database that contains at least two tables or two queries, or create two sample tables with which to work.
-
2
Click the "Create" tab and click "Query Design" under the "Queries" group.
-
-
3
Click on a table and click "Add" in the "Show Table" dialog box to add the table to the query or click on either the "Queries" or "Both" tab, click a query and click "Add" to add it to the new query. Repeat for a second table or query. Click "Close" on the "Show Table" window.
-
4
Click the small arrow at the bottom of the "View" button in the "Results" group of the "Query Tools: Design" tab. Click "SQL View" from the context menu.
-
5
Point the mouse cursor to the right of the first line where it says "Select" and click to place the cursor at the end of the line.
-
6
Leave a space after the word "Select" and type an open bracket symbol ("[") followed by the name of the first table or query to be included.
-
7
Type a close bracket symbol ("]") followed by a period (".") without any spaces and then type the field name for the first field from the first table or query followed by a comma. Press the space bar to create a space after the comma and repeat for additional fields from the same or other tables or queries.
-
8
Hold down the "CTRL" key and press the letter "S" to bring up the "Save As" dialog box for the new query. Type a useful name for the query and click "OK" to save the query.
-
9
Click the "Home" tab and click "Datasheet View" under the "View" dialog in the "Views" group to run the query and display the results.
-
1
Tips & Warnings
Create a copy of the live database or create a sample database to experiment with while learning new techniques.
The finished SQL statement should look similar to this example with "Table1" and "Table2" replaced with the actual table or query names and "Field1" and "Field2" replaced with the actual field names:
SELECT [Table1].Field1, [Table2].Field2
FROM [Table1], [Table2]
Always perform a full backup of any database when experimenting with new techniques, especially routines that perform bulk operations on tables or automatically modify data.
References
Resources
- Photo Credit Justin Sullivan/Getty Images News/Getty Images