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

The Revolution in Telecommunications: How technology is changing connections

The Revolution in Telecommunications: How technology is changing connections

June 8, 2025
How to Use Linear Actuator Mounting Brackets: DIY Tips and Best Practices

How to Use Linear Actuator Mounting Brackets: DIY Tips and Best Practices

April 14, 2025

11 Best Home Inventory Apps for Android and iOS

May 5, 2025

How .NET Development Services Drive Digital Transformation

March 17, 2025

Iperius Backup Review Competitors – A Comprehensive Guide

March 5, 2025

Disaster Recovery Planning: Why Your Business Needs a Managed IT Services Provider

February 26, 2025

Leave a Reply Cancel reply

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

Recent Articles

  • Biometric Banking: Why Your Face, Fingerprint, and Voice Are the New PIN
  • The Revolution in Telecommunications: How technology is changing connections
  • Understanding the Differences: BACS vs CHAPS Payment Systems Explained
  • Why CSPM Is the Unsung Hero of Enterprise Cloud Security
  • The New Face of Antivirus: Smarter, Faster, and Built for 2025 Threats
  • What are The Responsibilities of your tax preparer?
  • Best 10 Cryptocurrency Exchange Development Companies 2025

Related Posts

None found

  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

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

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.