Home > Database > SQL > Keyword representing union in sql

Keyword representing union in sql

下次还敢
Release: 2024-04-29 15:33:15
Original
524 people have browsed it

The keyword representing union in SQL is UNION. It combines the results of two or more SELECT statements into a single result set that contains all unique rows, which means it combines all the rows from the two input result sets without duplicates contained in both Rows in the result set.

Keyword representing union in sql

Keyword representing union in SQL: UNION

Union operation

The UNION keyword is used to combine the results of two or more SELECT statements into a single result set. It returns all unique rows from the two input result sets, which means it combines all the rows from the two result sets but does not duplicate the rows contained in both result sets.

Syntax

<code class="sql">SELECT_statement UNION [ALL] SELECT_statement</code>
Copy after login

Where:

  • SELECT_statement is the SQL query to be merged.
  • ALL is an optional keyword that returns all rows, including duplicate rows.

Usage

The UNION keyword can be used in the following scenarios:

  • Merge two result sets:Merge data from two or more tables into one result set.
  • Delete duplicate rows: Use the UNION ALL keyword to merge two result sets while retaining all rows, including duplicate rows.
  • Calculate totals: Add or join values ​​from multiple tables.
  • Create a list of unique values: Combine values ​​from two or more columns into a unique list.

Example

The following example merges the names from the employees table and the customers table into a single result set :

<code class="sql">SELECT name FROM employees
UNION
SELECT name FROM customers;</code>
Copy after login

The result set will contain all unique names from the employees table and the customers table.

Note:

  • The UNION keyword can only merge result sets with the same number of columns and data types.
  • The UNION ALL keyword will return all rows, including duplicate rows, while the UNION keyword returns only unique rows.

The above is the detailed content of Keyword representing union in sql. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template