Explain Codes LogoExplain Codes Logo

Select INTO USING UNION QUERY

sql
best-practices
data-migration
data-warehousing
Alex KataevbyAlex Kataev·Jan 14, 2025
TLDR

Quickly merge rows from multiple SELECT statements into a new table, use SELECT ... INTO along with UNION or UNION ALL:

SELECT col1, col2 INTO new_table FROM table1 WHERE condition1 UNION SELECT col1, col2 FROM table2 WHERE condition2;

Key points:

  • Ensure columns match in count and data type.
  • Choose between UNION for distinct results, or UNION ALL to include all rows.
  • The query generates a new new_table packed with the aggregated data from table1 and table2.

Common errors & best practices

Syntax errors: The lurking monsters

One pesky critter often encountered is the "Incorrect syntax near ')'" error. It's like finding a missing semicolon in JavaScript - it's always the last place you look!

Explicit column naming: Your variables, your rules

Clear and explicit column naming is the iron throne where your code's readability sits. It smashes uncertainty and brings clarity and compatibility to your queries.

Schema consistency: Avoid types confusion

A mismatched pair of socks may be trendy, but mismatched column counts and data types are a recipe for disaster. Always ensure schema consistency before you hit the 'Run Query' button.

Testing your code: Because everyone makes mistakes

As the old adage goes, measure twice, cut once. Always test your SELECT INTO queries in a non-production environment to avoid creating variant Spider-Men where you didn't intend to.

Duplicates: To be or not to be

UNION and UNION ALL, though similar, will create different results:

  • UNION ALL certainly doesn't discriminate. It includes all rows, duplicates included, like a wicked stepmother.
  • UNION, on the other hand, is the fairytale alternative. It filters out duplicate rows, delivering only the unique rows to your new table.

Key insights and deeper details

There are some additional key points that enhance the functionality of the SELECT INTO USING UNION QUERY:

Speed for creation: More power to you

When it's SELECT INTO vs. CREATE TABLE AS, the former usually wins the day due to performance advantages like lesser logging in some database systems.

Errors investigation: Play detective with your code

Syntax errors, especially our pesky friend "Incorrect syntax near ')'", must be weeded out with a careful check on parentheses placement and aliases.

Database specification: Right place, right time

Sometimes, specifying the database with USE <yourdb-name> might be required to avoid the equivalent of accidentally sending a text to the wrong person.

Column definition: Columns are your pillars

When creating tables using SELECT INTO, ensure new table columns are defined accurately before using them in your table.

Applies to these scenarios

SELECT INTO USING UNION QUERY is a practical approach in different scenarios:

  • Data warehousing: Merge multiple transactional tables into a single, summary table for optimized querying.
  • Table backups: Create a quick backup copy before fiddling around with sensitive operations that modify contents.
  • Data migration: Merge different datasets into a consistent format for easier downstream processing.