SQL advanced query operator UNION EXCEPT right and outer joins

Source: Internet
Author: User
Tags joins

SQL advanced query Operator union except right and outer joins
A:union operator
The Union operator derives a result table by combining the other two result tables (such as Table1 and table2) and eliminating any duplicate rows in the table. When all is used with union (that is, union ALL), duplicate rows are not eliminated. In both cases, each row of the derived table is either from table1 or from table2.

SQL Union syntax
Select COLUMN_NAME (s) from table_name1
Union
Select COLUMN_NAME (s) from table_name2

Look at a simple example

Select E_name from Employees_china
UNION ALL
Select E_name from Employees_usa


B:except operator
The except operator derives a result table by including all rows that are in Table1 but not in table2, and all duplicate rows are eliminated. When all is used with except (except all), duplicate rows are not eliminated.

SELECT * FROM TESTX except select * from testy
In the TESTX data but not duplicated in the testy

Note:
Except can not be used in SQL Server 2000 (only use union to manipulate the keyword), seemingly 2005 can

C:intersect operator
The syntax for Intersect is as follows:

[SQL statement 1]
Intersect
[SQL Statement 2]

The Intersect operator derives a result table by including only the rows in Table1 and table2 and eliminates all duplicate rows. When all is used with intersect (intersect all), duplicate rows are not eliminated.
Note: Several query result rows that use an operator must be consistent.

Select Date from Store_information
Intersect
Select Date from Internet_sales



12, Description: Use of external connections
A, left (outer) join:
Left OUTER join (left connection): The result set includes a matching row for the join table and all rows of the left-attached table.
Sql:select a.a, A.B, A.C, B.C, B.D, B.f from a left-out join B on a.a = B.C
B:right (outer) Join:
Right outer join (right connection): The result set includes both matching connection rows for the join table and all rows of the right join table.
C:full/cross (outer) Join:
All outer joins include not only matching rows for symbolic join tables, but also all records in two connected tables.
12, grouping: GROUP by:
A table, once the group is completed, the query can only get the group-related information.
Group-related information: (statistics) COUNT,SUM,MAX,MIN,AVG grouping criteria)
When grouping in SQL Server: You cannot group by a field of type Text,ntext,image
The fields in the SELECTE statistic function cannot be put together with the normal fields;
13, the database tutorial to operate:
Separate database: sp_detach_db; Additional databases: sp_attach_db indicates that the attach requires a full path name
14. How to modify the name of the database:
Sp_renamedb ' Old_name ', ' new_name '

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.