Except operations for attribute subtraction sets in SQL for two different tables

Source: Internet
Author: User

SQL uses the EXCEPT keyword for the two set subtraction operation, and the syntax format is as follows:

SELECT  from table_name1 EXCEPT SELECT  from Table_name2

The output is a tuple that exists in the previous collection and does not exist in the second collection.

If you choose two tuples, what happens if you have a different tuple name?

Using the example you used earlier, build the code as follows:

Create TableEmployee (EmpNameChar(6), Numchildrenint)Create TableDependent (DepnameChar(6), ageint)Insert  intoEmployeeValues('Haohao',2);Insert  intoEmployeeValues('Haohao',4);Insert  intoEmployeeValues('FGSD',5);Insert  intoEmployeeValues('HDS',7);Insert  intoEmployeeValues('Hauuo',9);Insert  intoEmployeeValues('Hsao',4);Insert  intoEmployeeValues('Hhao',5);Insert  intoDependentValues('Haohao',2);Insert  intoDependentValues('HDGSO',2);Insert  intoDependentValues('Hreo',2);Insert  intoDependentValues('Hjh',2);Insert  intoDependentValues('Haaao',2);

For two worksheets with different attributes, use the except action:

(Select  empname,numchildrenfrom  employee)except(  Select  depname,age from dependent)

The output results are as follows:

"HDS"; 7
"Hhao"; 5
"Hsao"; 4
"Hauuo"; 9
"Haohao"; 4
"FGSD"; 5

The names of the columns are "EmpName" and "Numchildren" and are the attributes of the employee.

After testing, the except operation will also filter out the results according to the definition.

  

  Now, play some tricks.

If so, what would happen if the number of attributes for two tables were different?

Build a new data table with the following code:

Create Table Newone (newname     Char(6), newnum    int, tel          int)

Insert data:

Insert  intoNewoneValues('Haohao',2,6);Insert  intoNewoneValues('Haohao',5,6);Insert  intoNewoneValues('Haoyo',2,6);Insert  intoNewoneValues('H6ao',2,6);Insert  intoNewoneValues('Hrhao',2,6);

A statement error appears with the result:

Error: Each EXCEPT query must have the same number of fields
Line 4: (Select Depname,age
^

Error **********

Error: Each EXCEPT query must have the same number of fields
SQL state:42601

character:57

Except operations for attribute subtraction sets in SQL for two different tables

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.