SQL union syntax and Examples

Source: Internet
Author: User

SQL union syntax and Examples

SQL union allows you to query the results of two or more individuals. The consortium is different because queries are subqueries that are independent from each other. The Consortium combines these individually selected outputs and lists them as part of a single output table.
SELECT column1,... column_n
FROM derived_table
UNION [ALL]
SELECT column1,... column_n
FROM derived_table

The SQL statement for each union query must have the same number of results as similar data types. In addition, when using a consortium, you can only select different values (similar to different values ).

Instance 1

SELECT Item
FROM Antiques
UNION
SELECT ItemDesired
FROM Orders
In this example, if a project contains two antique and Orders tables, it will appear in your result set once.
Instance 2
SELECT FirstName, LastName
FROM EmployeeAddressTable
WHERE City IN ('losantiville ', 'Paris ')
Union select OwnerFirstName, OwnerLastName
FROM AntiqueOwners
WHERE City IN ('losantiville ', 'Paris', 'san Diego ')

======================================

SQL join statement multi-Table query


So far, we have been searching for queries that retrieve data from a single table. Single-table queries are useful, but they do not use all the functions of the SQL language. SQL is a relational database query language. Therefore, one of its most important features is that it can retrieve information about related tables from several different ways. In relational databases, this process is called a connection. The table will be named as the name of each table that is separated by commas. Join in the relationship between tables defined by the WHERE clause predicate. A predicate can refer to any relation in the form of a joined table column.

For example

SELECT EmployeeAddressTable. EmployeeIDNo,
EmployeeAddressTable. FirstName,
EmployeeAddressTable. LastName,
EmployeeStatisticsTable. Salary
FROM EmployeeAddressTable, EmployeeStatisticsTable
WHERE EmployeeAddressTable. EmployeeIDNo = EmployeeStatisticsTable. EmployeeIDNo

In the preceding example, you can search for numbers, names, surnames, and salaries of employees. From the salary domain connection table. This SQL statement returns all rows from the EmployeeAddressTable and EmployeeStatisticsTable tables. If there is a matched EmployeeIDNo, whether it is the value of the EmployeeAddressTable and EmployeeStatisticsTable tables.

Add in
This is, in fact, adding the most common type. The inner join returns that the conditions for adding all rows in multiple tables are met. There must be a common field that matches values in two tables. An internal join cannot be nested in one left or right join.

SELECT EmployeeAddressTable. EmployeeIDNo,
EmployeeAddressTable. FirstName,
EmployeeAddressTable. LastName,
EmployeeStatisticsTable. Salary
FROM EmployeeAddressTable inner join EmployeeStatisticsTable
ON EmployeeAddressTable. EmployeeIDNo = EmployeeStatisticsTable. EmployeeIDNo

With the internal join operation, any relational comparison operator can be used in the ON clause: =, "=," =, or "<". In the preceding example, the value of the field in the 'employees' table of 'employee id' matches the value of the 'employeestatisticstable' field in all cases. If there is no EmployeeStatisticsTable match in the EmployeeAddressTable row, will these rows be listed.

Left join
The left join operator is used to create a left Outer Join, including two tables from the first (all records on the left), even if there is no matching second record value.


SELECT EmployeeAddressTable. EmployeeIDNo,
EmployeeAddressTable. FirstName,
EmployeeAddressTable. LastName,
EmployeeStatisticsTable. Salary
FROM EmployeeStatisticsTable left join EmployeeAddressTable
ON EmployeeAddressTable. EmployeeIDNo = EmployeeStatisticsTable. EmployeeIDNo

This SQL statement returns all rows from the first table (EmployeeStatisticsTable), even if the second table (EmployeeAddressTable) does not match. If the row "EmployeeStatisticsTable" does not match "EmployeeAddressTable", these rows will also be listed. In the result set, they are displayed.

Right join
The right to join the operator is used to create a good external join, including all records from the second (two tables on the right), even if there is no matching value in the first record.

SELECT EmployeeAddressTable. EmployeeIDNo,
EmployeeAddressTable. FirstName,
EmployeeAddressTable. LastName,
EmployeeStatisticsTable. Salary
FROM EmployeeStatisticsTable right join EmployeeAddressTable
ON EmployeeAddressTable. EmployeeIDNo = EmployeeStatisticsTable. EmployeeIDNo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.