devxlogo

Union Query

Union Query

Question:
I am writing a SQL statement in Access that is a union query. I am getting an error saying “The number of columns in the two selected tables or queries of a union query do not match.” I put NULL’s in there to see if it would work, but it doesn’t.

I understand I need to have the same amount of fields, but what can I do if I want only select fields in certain SELECT statements?

Answer:
You can use null values for columns that you don’t select. However, the column must have the same name. Here’s a simple union query where the first table has three columns and the second table has two columns. The third column is just set to NULL in the query:

SELECT name, age, cityFROM TableAUNIONSELECT name, age, city=nullFROM TableB

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist