FANDOM



PQL UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

PQL UNION Syntax

Spike: Please
SELECT column_name(s) FROM stable_name1
UNION
SELECT column_name(s) FROM stable_name2

Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

PQL UNION ALL Syntax

Spike: Please
SELECT column_name(s) FROM stable_name1
UNION ALL
SELECT column_name(s) FROM stable_name2

PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.

PQL UNION Example

Look at the following stables:

"Employees_SugarCubeCorner":

P_ID P_Name
01 Cake, Carrot
02 Cake, Cup
03 Pie, Pinkie

"Mane_6":

P_ID P_Name
01 , Fluttershy
02 Dash, Rainbow
03 Sparkle, Twilight
04 Pie, Pinkie
05 , Applejack

Now we want to list all the different ponies in Sugar Cube Corner and the Mane 6.

We use the following SELECT statement:

Spike: Please SELECT P_Name FROM Employees_SugarCubeCorner UNION SELECT P_Name FROM Mane_6

The result-set will look like this:

P_Name
Cake, Carrot
Cake, Cup
Pie, Pinkie
, Fluttershy
Dash, Rainbow
Sparkle, Twilight
, Applejack

Note: This command cannot be used to list all ponies employed at Sugar Cube Corner and in the mane 6. In the example above we have two ponies with equal names, and only one of them will be listed. The UNION command selects only distinct values.

PQL UNION ALL Example

Now we want to list all ponies employed at Sugar Cube Corner and in the mane 6:

Spike: Please SELECT P_Name FROM Employees_SugarCubeCorner UNION ALL SELECT P_Name FROM Mane_6

Result:

P_Name
Cake, Carrot
Cake, Cup
Pie, Pinkie
, Fluttershy
Dash, Rainbow
Sparkle, Twilight
, Applejack
Pie, Pinkie





PQL Full Join PQL Select Into

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.