How is the union operation used in databases?
In databases, UNION is an operator used to merge the result sets of two or more SELECT statements, returning unique values. It is used to combine multiple query results into one result set, automatically removing duplicate rows.
For example, suppose there are two tables A and B. Table A contains the fields name and age, and table B contains the fields name and gender. To query all unique values in the name fields from both tables, you can use the UNION operator.
SELECT name FROM A
UNION
SELECT name FROM B;
The above query will return a collection of unique name field values from both tables. If you want to include duplicate values, you can use the UNION ALL operator.