Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I combine results from two SELECT queries with UNION?

Quick answer

Write the two SELECT queries with the same number of selected columns, then connect them with UNION to remove duplicate complete result rows or UNION ALL to retain them. For example, select the same item ID and name from current and archived tables. Duplicate checking applies to the complete selected rows, not just one column. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a current_items table and an archived_items table. How do I get one list containing the items from both?

Geez

Put the two SELECT statements in one compound query and place UNION between them. For this example, assume both tables have columns named item_id and item_name; those are illustrative names, so replace them with your actual table and column names.

SELECT item_id, item_name
FROM current_items
UNION
SELECT item_id, item_name
FROM archived_items;

Each SELECT must return the same number of columns. Here, each returns two: the item ID and the item name. [S1] Selecting corresponding fields in the same order makes the combined output easier to interpret. This query returns rows from both sides while removing duplicate result rows. [S1]

Follow-up question

What exactly counts as a duplicate? If the same item ID appears in both tables, will one always disappear?

Geez

With UNION, duplicate checking applies to the complete result row formed from the selected columns. [S1] So if both queries select item_id and item_name, (102, 'Pen') in each result is a duplicate and only one copy remains. But (102, 'Pen') and (102, 'Blue Pen') are different result rows: the selected names differ, so UNION does not treat those complete rows as duplicates. [S1]

That distinction matters if you mean “one row per item ID.” Selecting the ID and name with UNION does not ensure one row per ID when the names differ. Decide which values should define the rows you want to deduplicate before choosing the selected columns.

Follow-up question

Could you show both operators with a small example, including what their results would be?

Geez

Assume, just for illustration, that the current table contains (101, 'Notebook') and (102, 'Pen'), while the archived table contains (102, 'Pen') and (103, 'Folder').

Using the same SELECTs with UNION:

SELECT item_id, item_name FROM current_items
UNION
SELECT item_id, item_name FROM archived_items;

The result contains these three distinct rows: (101, 'Notebook'), (102, 'Pen'), and (103, 'Folder'). The repeated (102, 'Pen') appears once.

Change only the operator to UNION ALL:

SELECT item_id, item_name FROM current_items
UNION ALL
SELECT item_id, item_name FROM archived_items;

That result contains all four input rows, including two copies of (102, 'Pen'). UNION ALL retains duplicates; UNION removes duplicate result rows. [S1]

Follow-up question

How should I choose between them, and what should I check if my result looks wrong?

Geez

Choose UNION when repeated complete rows should appear once; choose UNION ALL when every row from each query should be kept. [S1] If you expected a repeated item to disappear, compare every selected value on both sides. A difference in a selected value means the complete result rows are not duplicates. [S1]

Also check that both SELECTs return the same number of columns, as required for a compound SELECT. [S1] Confirm that each query selects the fields you need and that the corresponding fields are in the intended order. The key distinction is that duplicate IDs alone are not enough to make rows duplicates if other selected values differ. Try a small sample whose rows you can inspect.

A practical checklist

  • Write down the exact columns the combined result should contain.

  • Make each SELECT return the same number of columns.

  • Check that corresponding selected columns are in the intended order.

  • Use UNION for distinct complete result rows or UNION ALL to retain repeats.

  • Compare every selected value when checking why a row did or did not count as a duplicate.

A few more useful answers

Will UNION remove rows that share an item ID but have different names?

Not if both ID and name are selected: different complete result rows are not duplicates just because their IDs match. [S1] Choose selected columns that match the result you actually need.

Can the two SELECT statements return different numbers of columns?

No. Each constituent SELECT in a compound SELECT must return the same number of result columns. [S1]

Which operator keeps every row from both queries?

UNION ALL returns rows from both sides and retains duplicates. [S1]

Sources

References used for this guide.

  1. [S1] SQLite SELECT: compound SELECT and UNION operators
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work