Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Tech

What is Mysql Union? SQL UNION Operator

by Luke
October 3, 2020
in Tech
Reading Time: 5 mins read
mysql union

The MYSQL UNION operator is made use of to integrate the result-set of 2 or more SELECT statements. Each SELECT statement within MYSQL UNION must have the very same number of columns. The columns must additionally have comparable data types The columns in each SELECT declaration need to also remain in the very same order

Table of Contents
What is Mysql Union? SQL UNION Operator
MYSQL UNION Syntax
MYSQL UNION ALL Syntax
Demo Database
SQL MYSQL UNION Example.
SQL MYSQL ALL Example
SQL MYSQL UNION With WHERE
SQL UNION ALL With WHERE
MYSQL UNION ALL
Another MYSQL UNION Example.
MYSQL UNION

What is Mysql Union? SQL UNION Operator

In this article, you can know about mysql union here are the details below;

MYSQL UNION Syntax

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

MYSQL UNION ALL Syntax

The MYSQL UNION driver picks only distinctive worths by default. To enable duplicate values, make use of MYSQL UNION ALL:

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Keep in mind: The column names in the result-set are typically equal to the column names in the very first SELECT statement.

Demo Database

In this tutorial, we will utilize the popular Northwind example data source.

Below is a choice from the “Customers” table:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

As well as a selection from the “Suppliers” table:

SupplierID SupplierName ContactName Address City PostalCode Country
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. London EC1 4SD UK
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA
3 Grandma Kelly’s Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA

SQL MYSQL UNION Example.

The adhering to SQL statement returns the cities (just distinctive worths) from both the “Customers” and also the “Suppliers” table:

Example.

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;

Note: If some consumers or vendors have the very same city, each city will only be detailed as soon as, since MYSQL picks just distinctive worths. Usage MYSQL UNION ALL to additionally choose to replicate worths!

SQL MYSQL ALL Example

The complying with SQL declaration returns the cities (duplicate worths additionally) from both the “Customers” and also the “Suppliers” table:

Example.

SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

SQL MYSQL UNION With WHERE

The adhering to SQL statement returns the German cities (just unique worths) from both the “Customers” as well as the “Suppliers” table:

Example.

SELECT City, Country FROM Customers
WHERE Country=‘Germany’
UNION
SELECT City, Country FROM Suppliers
WHERE Country=‘Germany’
ORDER BY City;

SQL UNION ALL With WHERE

SELECT City, Country FROM Customers
WHERE Country=‘Germany’
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country=‘Germany’
ORDER BY City;

MYSQL UNION ALL

SELECT City, Country FROM Customers
WHERE Country=‘Germany’
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country=‘Germany’
ORDER BY City;

Another MYSQL UNION Example.

The adhering to SQL statement details all consumers and distributors:

Example.

SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

MYSQL UNION

SELECT ‘Customer’ AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT ‘Supplier’, ContactName, City, Country
FROM Suppliers;

Notice the “AS Type” over – it is a pen name. SQL Aliases are utilized to give a table or a column a short-term name. Pen names only exist throughout the inquiry. So, below we have created a temporary column named “Type” that lists whether they get in touch with a person is a “Customer” or a “Supplier.”

Check out over other articles like:

  • 100 % Fixed | Solutions to DISM Error 87 Windows 10 / 8/7
  • How to Solve | Error code 50382 WM1 in Monster Hunter World
  • (According to 150 Experts) How to Learn SEO in 2020
  • {Solved} Secure Connection Failed PR_END_OF_FILE_ERROR on Firefox
  • OpenGL Error 1281 Invalid Value | How to Fix it

Related Posts

help desk ticketing system

The 11 Best Help Desk Ticketing Systems for 2026

January 21, 2026
Sites Like Numlooker

Best 16 Sites Like Numlooker In 2025

November 10, 2025

Top 15 Benefits of Using a Managed IT Service Provider

November 4, 2025

9+ Best Ipa Apps Download Sites In 2025

November 1, 2025

Top 10 Geographic Information System Software In 2025

September 15, 2025

The 10 Best Instapage Alternatives for 2025

September 3, 2025

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Articles

  • How to Transfer Windows 10 from HDD to SSD Without Losing Data (2026 Guide)
  • When reflective materials change how traffic cameras react
  • Best 15 SOCKS5 Proxy Service Providers in 2026
  • 15 Best VoIP Phone Services in 2026
  • 10 IPTV Smarters Pro Alternatives In 2026
  • The 11 Best Help Desk Ticketing Systems for 2026
  • Top 15 Best Task Management Software For Small Business

Related Posts

None found

  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

Reuters, the news and media division of Thomson Reuters, is the world’s largest multimedia news provider, reaching billions of people worldwide every day, Sign up for our free daily newsletter: thomson@reutersmarkets.com

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.