'Understanding why UNION is used in this SQL injection example

I'm trying to understand more about SQL injection, so I found this lesson from Red Tiger Labs.

According to the solution, the cat=1 part of the URL is vulnerable to SQL injection.

I can understand that you can append ORDER BY X# and keep incrementing X to establish the number of columns, which is 4.

However according to the solution, the next step is to do:

cat=1 union select 1,2,3,4 from level1_users #

The table name is provided, so that's ok. But I'm really having trouble understanding the purpose of the UNION. My guess is the underlying code does something like:

SELECT * FROM level1_users where cat=1

Presumably it would expect only 0 or 1 results. Then it prints out some number of columns onto the screen. According to the example, it prints out:

This hackit is cool :)
My cats are sweet.
Miau
3
4

The first three lines were printed out without the extra SQL injection. So what's going on, and what's the significance?

I would not expect the union to do anything, I assume the numbers refer to columns?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source