本文深入探讨了SQL查询中的并集、交集、差集操作,以及如何利用ROW_NUMBER函数进行排序与分页。通过实际代码示例,展示了在HR员工表与销售客户表中查询国家、地区和城市信息的方法,包括并集查询、交集查询、差集查询以及排序与分页技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

use tsql2012;

 

select country, region, city from hr.employees

union all

select country, region, city from sales.customers;

 

select country, region, city from hr.employees

union

select country, region, city from sales.customers;

 

select country, region, city from hr.employees

intersect

select country, region, city from sales.customers;

 

select row_number() over(partition by country, region, city order by (select 0)) as rownum,

country, region, city

from hr.employees

intersect

select row_number() over(partition by country, region, city order by (select 0)) as rownum,

country, region, city

from sales.customers;

 

with intersect_all

as

(select row_number() over(partition by country, region, city order by (select 0)) as rownum,

country, region, city

from sales.customers

intersect

select row_number() over(partition by country, region, city order by (select 0)) as rownum,

country, region, city

from hr.employees)

select country, region, city from intersect_all;

 

select country, region, city from hr.employees

except

select country, region, city from sales.customers;

 

select country, region, city from sales.customers

except

select country, region, city from hr.employees;

 

with except_all

as

(select row_number() over(partition by country, region, city order by (select 0)) as rownum,

country, region, city

from hr.employees

except

select row_number() over(partition by country, region, city order by(select 0)) as rownum,

country, region, city

from sales.customers)

select country, region, city from except_all;

 

select country, count(*) as numlocations

from (select country, region, city from hr.employees

union

select country, region, city from sales.customers) as U

group by u.country;

 

select empid, orderid, orderdate

from (select top(2) empid, orderid, orderdate

from sales.orders

where empid = 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值