Hello Everyone,
Good evening everyone, how are you, i hope you all guys will happy and good. By the grace of God, i am also good. Today i am here with new post in which we discuss the SQL Union operators. There are two types of operator. One is simple UNION and second is UNION ALL. we will also explore its syntax by using simple example.
In the field of database, SQL play an crucial role in manipulating data. The UNION operator is used for combining the results of multiple SELECT statements into a single result. by using UNION operators, data will increased by rows wise.
It removes duplicate rows automatically and provide the data set ideal. Some important rules are needed to follow for joins. The columns in each SELECT statement must have the same data types and same numbers of columns in corresponding positions.
UNION Syntax
SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2
The SQL UNION operator is vital for combining and aggregating data from various sources in a database. Knowing its syntax, features, and best practices empowers professionals to streamline data tasks, improving database efficiency.
User1
NO | sector | name_1 |
---|---|---|
1 | 1000 | ALI HAMZA |
2 | 1000 | IMRAN |
3 | 1000 | SIKANDAR |
User2
NO | sector | name_1 |
---|---|---|
4 | 15 | Mustafa |
5 | 106 | Farhan |
6 | 90 | abdullah |
The above are two table on which we will apply the SQL UNION then see the result.
Select * from User1
Union
Select * from User2
See the below Result
NO | sector | name_1 |
---|---|---|
1 | 1000 | ALI HAMZA |
2 | 1000 | IMRAN |
3 | 1000 | SIKANDAR |
4 | 15 | Mustafa |
5 | 106 | Farhan |
6 | 90 | abdullah |
If we face the data requirement in which we need to make data set will allowing the duplication in data then we will use the UNION ALL operator.
We can also use the union and union all operators by using with clause. We can apply the filter on each select queries to get the data according to requirement.
For example i want to get those data that just have sector 1000 and sector 15 then we will apply the where clause on both tables then use the union operator to show and get the combine results. Below is the query that will meet our requirements.
Select * from User1
where sector in (1000 , 15)
Union
Select * from User2
where sector in (1000 , 15)
Now see the result.
NO | sector | name_1 |
---|---|---|
1 | 1000 | ALI HAMZA |
2 | 1000 | IMRAN |
3 | 1000 | SIKANDAR |
4 | 15 | Mustafa |
It would be my pleasure if you will share your opinion and reviews on this topic
Please cast witness vote to @bangla.Witness or set proxy to @rme.
_
Vote @bangla.witness as witness
Set Proxy @rme as proxy
Special Thanks.
Cc: @rme
Cc: @hungry-griffin
Cc: @pennsif
Thanks for giving your precious time to my post.
Follow | Upvote | Share | Comments
Follow me on Instagram.
Instagram
🆃🅷🅰️🅽🅺 🆈🅾️🆄
Congratulations, your post has been upvoted by @dsc-r2cornell, which is the curating account for @R2cornell's Discord Community.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit