Select columns from multiple SQL tables
How to select columns from multiple SQL tables with joins or UNION, what each approach changes in the result, and how to keep exported results usable.
Pulling columns from more than one table in SQL is usually done with a join: you name the columns you want, say which table each comes from, and state how rows in one table relate to rows in another. This review covers when that approach fits, what it changes in the result, and what to watch for when the output is copied or exported as delimited text.
Is a multi-table SELECT the right choice?
If the tables share a key, such as a customer ID that appears in both a customers table and an orders table, a join with qualified column names is the standard approach. The main benefit is a single result set in which related values sit on the same row. The decisive tradeoff is that the join type and join condition decide which rows appear and how many times. A wrong choice can silently drop rows or multiply them.
A plain delimited-text approach is often the better fit when the data has already been exported to CSV or TSV, you only need to keep or drop columns from one file, and no rows need to be matched across sources. A column-selection tool can cut fields from a file, but it does not relate rows between two files. Matching rows still requires a join, whether in SQL or in a tool that explicitly supports joining.
The basic pattern
SELECT c.customer_id,
c.name,
o.order_id,
o.order_date
FROM customers c
INNER JOIN orders o
ON o.customer_id = c.customer_id;
Each table gets a short alias (c and o), and every column is prefixed with its alias. A prefix is required when the same column name exists in more than one table, such as customer_id above, and it makes the query easier to read even when it is not required. Writing alias.* selects every column from that one table. Many engines accept the keyword AS before a table alias, but Oracle does not, so leaving it out is more portable.
Selecting two columns from different tables
The smallest case works the same way: one column from each table, plus a condition that links them.
SELECT e.full_name,
d.department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id;
A bare JOIN is treated as INNER JOIN. Additional tables are added by chaining further JOIN ... ON clauses, each with its own condition.
SELECT c.name,
o.order_id,
p.product_name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items i ON i.order_id = o.order_id
JOIN products p ON p.product_id = i.product_id;
What the query actually changes
A SELECT does not modify stored data. It builds a new result set. Two separate things happen. Choosing columns, called projection, decides which fields appear. Joining decides which rows from each table are paired. Searches for selecting multiple columns from multiple tables often mean the first, but the second is what determines whether the result is correct.
- INNER JOIN: keeps only rows with a match in both tables.
- LEFT JOIN: keeps every row from the left table and fills the right table's columns with NULL where no match exists.
- RIGHT JOIN: the same as LEFT JOIN with the sides reversed.
- FULL OUTER JOIN: keeps unmatched rows from both sides. MySQL does not support it; it is commonly emulated with a UNION of a LEFT and a RIGHT join.
- CROSS JOIN: pairs every row of one table with every row of the other.
The older comma syntax (FROM a, b) behaves like a cross join unless a WHERE clause links the tables. Leaving out that condition is a common way to get an unexpectedly huge result.
Row multiplication is the other frequent surprise. In a one-to-many relationship, the values from the "one" side repeat on every matching row from the "many" side. That is expected, but sums and counts over the joined result will overcount the repeated side. Aggregate before joining, or count distinct keys, when that matters.
When the tables are unrelated: UNION instead of a join
If the goal is to list similar data from two tables in one column set, such as contacts from two sources, UNION stacks rows rather than placing them side by side. Each SELECT must return the same number of columns with compatible types. The output column names come from the first query. UNION removes duplicate rows, while UNION ALL keeps them and avoids the cost of deduplication.
SELECT name, email, 'customer' AS source
FROM customers
UNION ALL
SELECT name, email, 'supplier' AS source
FROM suppliers;
If two tables have no relationship and you still want their columns side by side, there is no meaningful way to pair the rows. Workarounds that number the rows in each table and join on that number produce an arbitrary pairing unless the numbering follows an order that actually means something.
Keeping the query and its output readable
- Qualify every column with its table alias, not just the ambiguous ones.
- Rename output columns that would otherwise collide, for example c.id AS customer_id and o.id AS order_id.
- Avoid SELECT * across joins. It produces duplicate column names, and the output changes whenever a table gains or loses a column.
- Add ORDER BY when row order matters. Without it, the order of returned rows is not guaranteed.
Will the result stay usable after copying or exporting?
Once a result grid is copied or exported as CSV or TSV, the selected columns carry over, but several details depend on the client and the destination. These are the points most worth checking:
- Duplicate headers: two columns both named id may be renamed, overwritten or rejected by the next tool. Aliasing them in the query avoids this.
- NULL versus empty: some exports write NULL as an empty field, which loses the distinction from an empty string, while others write the literal text NULL.
- Delimiters inside values: commas, tabs, quotes and line breaks in data must be quoted. The common CSV convention, described in RFC 4180, wraps such fields in double quotes and doubles any embedded quotes. A tool that splits naively on the delimiter will shift columns.
- Spreadsheet reinterpretation: spreadsheet applications may strip leading zeros from codes or reformat dates when opening a file.
- Row order: an export without ORDER BY may not match the order you saw on screen.
For sharing results with people, structure matters for accessibility. A real table with marked header cells, such as an HTML table or a spreadsheet, keeps column relationships available to assistive technology. A space-aligned text grid pasted into a document or message usually becomes a run of text with no column information. When the destination cannot hold a real table, a short plain-text equivalent is clearer, such as a list with one record per line and labelled fields. This review did not test any specific screen reader or destination, so confirm the behavior where the result will be used.
Gaps and limits
No tool-specific evidence was available for this review. It does not cover any particular query client, export utility or delimited-text tool, and it includes no measurements or test results. The SQL shown follows widely supported syntax, but engines differ in join support, alias rules and export defaults. Check the documentation for your database and client before relying on a specific behavior.