[Switch] The difference between set operation UNION, UNION ALL, INTERSECT, MINUS

There are two tables, the job_history table has 10 pieces of data; the employees table has 107 pieces of data, and the two tables have 2 pieces of data that are duplicates

SQL> ed

written to file afiedt.buf

  1  SELECT employee_id id, job_id

  2  FROM   employees

  3  minus

  4  SELECT employee_id id, job_id

  5* FROM   job_history

SQL> /

Indicates that the records that exist in employees but not in job_history are returned, 105 pieces of data are returned, and 2 duplicate pieces of data are removed

SQL> ed

written to file afiedt.buf

  1  SELECT employee_id id, job_id

  2  FROM   job_history

  3  minus

  4  SELECT employee_id id, job_id

  5* FROM   employees

SQL> /

Indicates that the records that exist in job_history but not in employees are returned, 8 pieces of data are returned, and 2 pieces of duplicate data are removed

 

 

 

ORDER BY clause in set operations

 

Appears only at the end of the entire collection. Can be sorted by column name, alias or position number in the first SELECT statement.

SQL> ed

written to file afiedt.buf

  1  SELECT employee_id id, job_id

  2  FROM   employees

  3  UNION all

  4  SELECT employee_id, job_id

  5  FROM   job_history

  6* order by employee_id

SQL> /

 

The following statement is correct

SQL> ed

written to file afiedt.buf

  1  SELECT employee_id, job_id

  2  FROM   employees

  3  UNION all

  4  SELECT employee_id id, job_id

  5  FROM   job_history

  6* order by employee_id

SQL> /

 

 

 

summary

 

The expressions in the SELECT list must have the same number and type.

Display the column names from the first Select statement in the results.

Except for the UNION ALL operation, other set operations automatically remove duplicate values.

 

So other operations will be implicitly sorted (there is no order by in the sql statement), using CPU resources.

Except for UNION ALL operations, the output results of other set operations are arranged in ascending order by default.

 

So other operations will be implicitly sorted (there is no order by in the sql statement), using CPU resources.

 

(From: http://www.cnblogs.com/simplefrog/archive/2012/07/15/2592380.html )

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326799690&siteId=291194637