In this blog, I will show how to use and where to use UNION in SQL. Explain How its work and why we need to use it.
I. SQL means
SQL is a domain-specific language used in programming and designed for managing data held in a relational database management system, or for stream processing in a relational data stream management system
for more blog and topic about SQL , Click to visit HERE.
II. UNION Operator
The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
I have sample simple query here, this is only show how is work, I have table user and user_gender with this column lists .
and first we need to select all Male and second is Female.
- Male Query
- Female Query
NOTE: this is it. this is for example only and you need to jump your thinking out of the box to understand and expound this idea. I am doing this for a reason why I am using simple sample, because I want you to develop your logical and imagination aspect.
I know that the result of this is can be get as simple join query but I use it for other topic. every blog of this site will using simple sample and that your time to expand your logical aspect.
Here we go.
to Union the to query you only need to put Union Operation between to of them.
Select * From users A inner Join user_gender B On A.gender = B.id Where B.gender = 'Male' UNION Select * From users A inner Join user_gender B On A.gender = B.id Where B.gender = 'Female'
Thank you for reading and visiting my site. Please do subscribe to be updated every new posted article here.
Import SQL Database in SSMS from Local Machine
In this article, We learn how to Import SQL Database in SSMS in Local Machine. I will show how to do it, and just follow […]
Export(Backup) SQL Database in SSMS in Local Machine
In this article, We learn how to export(Backup) SQL Database in SSMS in Local Machine. I will show how to do it, and just follow […]
Angular CRUD with .NET Core API
In this article, We create a full tutorial of Angular CRUD with .NET Core API. I will show how to do it, and just follow […]
Create Table in MSSQL using T-SQL Command/Statement
In this blog, I will show how to create Table in MS SQL using T-SQL command. explain how its works and why we need to […]
Use UNION Operator in SQL
In this blog, I will show how to use and where to use UNION in SQL. Explain How its work and why we need to use […]
Calling a Web Services or Rest API from T-SQL using MSXML
In this blog, I will show step by step how to call a Web Service or Rest API in your T-SQL. explain how its works […]
Common Table Expression (CTE) in SQL Query
A Common Table Expression also called CTE in short form, is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, […]
use Sub-Query in SQL Query
In this blog, I will show step by step how to use SQL Sub-Query in SQL Query. explain how its works and why we need […]
Create SQL Views
A Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a real table in the […]
Create Stored Procedure in SQL
A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name, which are stored in a relational database management system […]
One Reply to “Use UNION Operator in SQL”
I wasn’t sure I could manage writing such a long article in the beginning. Well, your style definitely caught my interest. You always deliver amazing content. Great Article Neil. While I did read it a few weeks ago, I didn’t leave any comments. However , I felt that the article was good enough to deserve a thankyou.