① union(去重,排序)取并集并且去重,按照第一个查询语句的第一个字段升序排序
②union all(不去重,不排序)并集,相当于两个查询语句结果堆集,无序,不去重
③intersect : 取交集
④minus:第二个查询结果的补集,这个补集是第一个查询结果的子集
除了union all 不排序,其他都排序去重
1.union
SQL> select employee_id,job_id 2 from employees 3 union 4 select employee_id,job_id 5 from job_history; EMPLOYEE_ID JOB_ID ----------- ---------- 100 AD_PRES 101 AC_ACCOUNT 101 AC_MGR 101 AD_VP 102 AD_VP 102 IT_PROG 103 IT_PROG 104 IT_PROG 105 IT_PROG 106 IT_PROG 107 IT_PROG EMPLOYEE_ID JOB_ID ----------- ---------- 108 FI_MGR 109 FI_ACCOUNT 110 FI_ACCOUNT 111 FI_ACCOUNT 112 FI_ACCOUNT 113 FI_ACCOUNT 114 PU_MAN 114 ST_CLERK 115 PU_CLERK 116 PU_CLERK 117 PU_CLERK EMPLOYEE_ID JOB_ID ----------- ---------- 118 PU_CLERK 119 PU_CLERK 120 ST_MAN 121 ST_MAN 122 ST_CLERK 122 ST_MAN 123 ST_MAN 124 ST_MAN 125 ST_CLERK 126 ST_CLERK 127 ST_CLERK EMPLOYEE_ID JOB_ID ----------- ---------- 128 ST_CLERK 129 ST_CLERK 130 ST_CLERK 131 ST_CLERK 132 ST_CLERK 133 ST_CLERK 134 ST_CLERK 135 ST_CLERK 136 ST_CLERK 137 ST_CLERK 138 ST_CLERK EMPLOYEE_ID JOB_ID ----------- ---------- 139 ST_CLERK 140 ST_CLERK 141 ST_CLERK 142 ST_CLERK 143 ST_CLERK 144 ST_CLERK 145 SA_MAN 146 SA_MAN 147 SA_MAN 148 SA_MAN 149 SA_MAN EMPLOYEE_ID JOB_ID ----------- ---------- 150 SA_REP 151 SA_REP 152 SA_REP 153 SA_REP 154 SA_REP 155 SA_REP 156 SA_REP 157 SA_REP 158 SA_REP 159 SA_REP 160 SA_REP EMPLOYEE_ID JOB_ID ----------- ---------- 161 SA_REP 162 SA_REP 163 SA_REP 164 SA_REP 165 SA_REP 166 SA_REP 167 SA_REP 168 SA_REP 169 SA_REP 170 SA_REP 171 SA_REP EMPLOYEE_ID JOB_ID ----------- ---------- 172 SA_REP 173 SA_REP 174 SA_REP 175 SA_REP 176 SA_MAN 176 SA_REP 177 SA_REP 178 SA_REP 179 SA_REP 180 SH_CLERK 181 SH_CLERK EMPLOYEE_ID JOB_ID ----------- ---------- 182 SH_CLERK 183 SH_CLERK 184 SH_CLERK 185 SH_CLERK 186 SH_CLERK 187 SH_CLERK 188 SH_CLERK 189 SH_CLERK 190 SH_CLERK 191 SH_CLERK 192 SH_CLERK EMPLOYEE_ID JOB_ID ----------- ---------- 193 SH_CLERK 194 SH_CLERK 195 SH_CLERK 196 SH_CLERK 197 SH_CLERK 198 SH_CLERK 199 SH_CLERK 200 AC_ACCOUNT 200 AD_ASST 201 MK_MAN 201 MK_REP EMPLOYEE_ID JOB_ID ----------- ---------- 202 MK_REP 203 HR_REP 204 PR_REP 205 AC_MGR 206 AC_ACCOUNT 115 rows selected.
②union all
SQL> select employee_id,job_id,department_id 2 from employees 3 union all 4 select employee_id,job_id,department_id 5 from job_history 6 order by employee_id; EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 100 AD_PRES 90 101 AD_VP 90 101 AC_ACCOUNT 110 101 AC_MGR 110 102 AD_VP 90 102 IT_PROG 60 103 IT_PROG 60 104 IT_PROG 60 105 IT_PROG 60 106 IT_PROG 60 107 IT_PROG 60 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 108 FI_MGR 100 109 FI_ACCOUNT 100 110 FI_ACCOUNT 100 111 FI_ACCOUNT 100 112 FI_ACCOUNT 100 113 FI_ACCOUNT 100 114 PU_MAN 30 114 ST_CLERK 50 115 PU_CLERK 30 116 PU_CLERK 30 117 PU_CLERK 30 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 118 PU_CLERK 30 119 PU_CLERK 30 120 ST_MAN 50 121 ST_MAN 50 122 ST_MAN 50 122 ST_CLERK 50 123 ST_MAN 50 124 ST_MAN 50 125 ST_CLERK 50 126 ST_CLERK 50 127 ST_CLERK 50 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 128 ST_CLERK 50 129 ST_CLERK 50 130 ST_CLERK 50 131 ST_CLERK 50 132 ST_CLERK 50 133 ST_CLERK 50 134 ST_CLERK 50 135 ST_CLERK 50 136 ST_CLERK 50 137 ST_CLERK 50 138 ST_CLERK 50 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 139 ST_CLERK 50 140 ST_CLERK 50 141 ST_CLERK 50 142 ST_CLERK 50 143 ST_CLERK 50 144 ST_CLERK 50 145 SA_MAN 80 146 SA_MAN 80 147 SA_MAN 80 148 SA_MAN 80 149 SA_MAN 80 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 150 SA_REP 80 151 SA_REP 80 152 SA_REP 80 153 SA_REP 80 154 SA_REP 80 155 SA_REP 80 156 SA_REP 80 157 SA_REP 80 158 SA_REP 80 159 SA_REP 80 160 SA_REP 80 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 161 SA_REP 80 162 SA_REP 80 163 SA_REP 80 164 SA_REP 80 165 SA_REP 80 166 SA_REP 80 167 SA_REP 80 168 SA_REP 80 169 SA_REP 80 170 SA_REP 80 171 SA_REP 80 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 172 SA_REP 80 173 SA_REP 80 174 SA_REP 80 175 SA_REP 80 176 SA_REP 80 176 SA_REP 80 176 SA_MAN 80 177 SA_REP 80 178 SA_REP 179 SA_REP 80 180 SH_CLERK 50 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 181 SH_CLERK 50 182 SH_CLERK 50 183 SH_CLERK 50 184 SH_CLERK 50 185 SH_CLERK 50 186 SH_CLERK 50 187 SH_CLERK 50 188 SH_CLERK 50 189 SH_CLERK 50 190 SH_CLERK 50 191 SH_CLERK 50 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 192 SH_CLERK 50 193 SH_CLERK 50 194 SH_CLERK 50 195 SH_CLERK 50 196 SH_CLERK 50 197 SH_CLERK 50 198 SH_CLERK 50 199 SH_CLERK 50 200 AD_ASST 10 200 AC_ACCOUNT 90 200 AD_ASST 90 EMPLOYEE_ID JOB_ID DEPARTMENT_ID ----------- ---------- ------------- 201 MK_MAN 20 201 MK_REP 20 202 MK_REP 20 203 HR_REP 40 204 PR_REP 70 205 AC_MGR 110 206 AC_ACCOUNT 110 117 rows selected.
③intersect
SQL> select employee_id,job_id 2 from employees 3 intersect 4 select employee_id,job_id 5 from job_history; EMPLOYEE_ID JOB_ID ----------- ---------- 176 SA_REP 200 AD_ASST
④minus
SQL> select employee_id 2 from employees 3 minus 4 select employee_id 5 from job_history; EMPLOYEE_ID ----------- 100 103 104 105 106 107 108 109 110 111 112 EMPLOYEE_ID ----------- 113 115 116 117 118 119 120 121 123 124 125 EMPLOYEE_ID ----------- 126 127 128 129 130 131 132 133 134 135 136 EMPLOYEE_ID ----------- 137 138 139 140 141 142 143 144 145 146 147 EMPLOYEE_ID ----------- 148 149 150 151 152 153 154 155 156 157 158 EMPLOYEE_ID ----------- 159 160 161 162 163 164 165 166 167 168 169 EMPLOYEE_ID ----------- 170 171 172 173 174 175 177 178 179 180 181 EMPLOYEE_ID ----------- 182 183 184 185 186 187 188 189 190 191 192 EMPLOYEE_ID ----------- 193 194 195 196 197 198 199 202 203 204 205 EMPLOYEE_ID ----------- 206 100 rows selected.
时间: 2024-10-18 08:26:26