In a standard SQL query, data is retrieved from one table. The `UNION` operator allows multiple `SELECT` statements to be combined. If an application is vulnerable to SQL injection, an attacker can inject a crafted SQL query that appends a `UNION` statement to the original query. Let's assume a vulnerable web application retrieves product details based on a product ID from a database:
SELECT product_name, product_price FROM products WHERE product_id = 'input_id';An attacker could modify the `input_id` to include the data from another table like `users`.
1' UNION SELECT username, password FROM users --After submitting our payload, the query become the following SQL:
SELECT product_name, product_price FROM products WHERE product_id = '1' UNION SELECT username, password FROM users --';:warning: The 2 SELECT clauses must have the same number of columns.
Source: SQL Injection/README.md in PayloadsAllTheThings. From PayloadsAllTheThings by Swissky and contributors. MIT License, Copyright (c) 2019 Swissky.