Monday, May 14, 2012

SQL UNION

Hello Friends, Welcome, in this post I will explain you about the UNION query.

The UNION Query, doesn't helps to find the column count while performing a Manual Injection.

Once I asked a l33t claimer, why do you use UNION, and the answer was "Simple, to find number of columns" and i was like LOL.

Anyways, lets get into it, UNION query is used to concatenate two tables based on a column.

So lets check out our Tables,

Table: CK_Members

CID     Name         Designation
222     Whiskey       Moderator
444     Onty             Member
111     Gaurav          Admin
333     Kumaar        Admin
555     Rishabh        Admin
666     Rose            Admin
888     White           Member
999     Mikey          Member

Table: ACTIVE

Name                       Status
Whiskey                   Active
Gaurav                      Inactive
Onty                         Inactive
Kumaar                    Active
White                       Active
Mikey                      Active
AkShay                   Active

So we have two tables named, CK_Members, and ACTIVE

Now, a normal UNION will return the distinct records, any duplicate records are eliminated.

Syntax:

SELECT column FROM table_1
UNION
SELECT column FROM table_2


So For our Query,

SELECT Name FROM CK_Members
UNION
SELECT Name FROM ACTIVE


Our result-set will have,

Name
Whiskey
Onty
Gaurav
Kumaar
Rishabh
Rose
White
Mikey
AkShay

So basically, this will concatenate and our result-set will have the matching records appearing once.

Now, another form of UNION query is the UNION ALL, and this gives us a result set, including the duplicate records from both the Table

Syntax:

SELECT column FROM table_1
UNION
SELECT column FROM table_2


Query:

SELECT Name FROM CK_Members
UNION ALL
SELECT Name FROM ACTIVE


Result-set:

Name
Whiskey
Onty
Gaurav
Kumaar
Rishabh
Rose
White
Mikey
Whiskey
Gaurav
Onty   
Kumaar
White
Mikey
AkShay

So I hope This should be clear to my readers the actual function of UNION Query. Thats All For this Post.

Don't Forget to check my next Post. a Like and Share would be appreciated.

Thank You!

0 comments:

Post a Comment