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
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.
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 […]
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 […]
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, […]
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 […]
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 […]
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 […]
In this tutorial, I create a project in ASP.net CRUD Web Application using Entity Framework. This Project user can Create, Read(Display), Update, Delete data from Database using […]
In this blog, I will show step by step how to create Database and Table to SQL Server. explain how its work and why we […]
In this tutorial, I create a project in ASP.net Web Application using SQL Server. This Project have an user login and registration. I will use […]