Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
SQL UNION ALL Operator 
 

The SQL UNION ALL Operator is used to list all records from two or more select statements.

 

SQL UNION ALL Operator

                         

The SQL UNION ALL Operator is used to list all records from  two or more select statements. The SQL Union ALL  Operator is used to combine two table using select statement when both the  table have the same number of columns. Each columns in a table must have the same data types.The UNION ALL select all the records from both table must be in the same order.

 

Understand with Example

The Tutorial illustrates an example from SQL UNION  ALL Operator. In this Tutorial, we create a tables name 'Stu_Class_10'  and 'Stu_Class_12' using create statement. The insert into  is used to  add the records or rows in the respective tables. The select statement is used to retrieve the records or rows from the respective tables.

Create Table Stu_Class_10

create table Stu_Class_10(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class  varchar(10))

Create Table Stu_Class_12

create table Stu_Class_12(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class  varchar(10))

Insert data into Stu_Class_10

insert into Stu_Class_10 values(1,'Komal',10)
insert into Stu_Class_10 values(2,'Ajay',10)
insert into Stu_Class_10 values(3,'Rakesh',10)
insert into Stu_Class_10 values(4,'Bhanu',10)
insert into Stu_Class_10 values(5,'Santosh',10)
insert into Stu_Class_10 values(1,'Komal',10)

Insert data into Stu_Class_12

insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(2,'Ajay',12)
insert into Stu_Class_12 values(3,'Rakesh',12)
insert into Stu_Class_12 values(4,'Bhanu',12)
insert into Stu_Class_12 values(5,'Santosh',12)

Stu_Class_10

Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10

Stu_Class_12

Stu_Id Stu_Name Stu_Class
1 Komal 12
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12

SQL UNION Syntax

The SQL UNION Syntax used for  union columns from two tables is given below:

SELECT column_name(s) FROM table_name1
UNION All
SELECT column_name(s) FROM table_name2

Use UNION in SQL Query

In this example, we union columns from two different tables. The UNION ALL combine two table using select statement when both the table have the same name field and its data type. The select return you all duplicate records from both tables. The UNION ALL command select all records from a tables.

.SELECT * FROM Stu_Class_10
UNION ALL
SELECT * FROM Stu_Class_12

Result

Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
1 Komal 12
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12

 

                         

» View all related tutorials
Related Tags: sql mysql string io sed return this id position for with to dst pos e it synonym ids use in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.