Sql Union Syntax
Learn how SQL UNION works and how I use it to combine results from multiple SELECT statements into a single result set. In this video, I cover the core UNION syntax, when to use UNION vs UNION ALL, and the rules you need to follow so your queries run correctly and return the data you expect. SQL UNION is useful when I need to merge rows from similar queries, especially when data is split across tables with the same structure or when I want to standardize output from different sources. I focus on practical query patterns that help you write cleaner SQL and avoid common mistakes. What I cover: - Basic SQL UNION syntax - How UNION combines results from multiple SELECT queries - The difference between UNION and UNION ALL - Matching column counts and compatible data types - How duplicate removal works - Ordering the final combined result set - Common errors and how I avoid them Example SQL UNION syntax: ```sql SELECT first_name, last_name FROM customers_us UNION SELECT first_name, last_name FROM customers_eu; ``` If I want to keep duplicates, I use UNION ALL: ```sql SELECT product_id, sale_date FROM online_sales UNION ALL SELECT product_id, sale_date FROM store_sales; ``` A common rule with UNION is that each SELECT must return the same number of columns in the same order, and the data types should be compatible: ```sql SELECT employee_id, department_name FROM current_employees UNION SELECT employee_id, department_name FROM former_employees; ``` I can also rename columns in the first query so the final output is easier to read: ```sql SELECT customer_id AS id, email AS contact FROM newsletter_subscribers UNION SELECT customer_id, email FROM premium_members; ``` To sort the combined result, I apply ORDER BY at the end: ```sql SELECT city FROM suppliers UNION SELECT city FROM warehouses ORDER BY city; ``` Specific technical use case: A practical use case is building a unified reporting query for sales data stored in separate regional tables. For example, if an application stores North America sales in one table and Europe sales in another, I can use UNION or UNION ALL to create one result set for a dashboard, export, or analytics workflow. ```sql SELECT order_id, customer_id, total_amount, 'north_america' AS region FROM sales_na UNION ALL SELECT order_id, customer_id, total_amount, 'europe' AS region FROM sales_eu; ``` This approach is especially helpful in reporting pipelines where I need one consistent dataset without restructuring the underlying schema first. By adding a region label directly in the query, I can preserve source context while still combining the records into a single output that works well for BI tools, internal admin panels, and ad hoc analysis. I also show how UNION helps when consolidating user records, combining archived and active datasets, or merging lookup values from multiple tables into one clean list. Understanding when to remove duplicates with UNION and when to preserve every row with UNION ALL can make a big difference in both correctness and performance. If you want to improve your SQL query writing and better understand how to combine result sets safely and efficiently, this video walks through the syntax and the practical logic behind it in a straightforward way. #sql #sqlunion #sqltutorial #database #sqlexamples #sqlsyntax #learnsql
Download
0 formatsNo download links available.