1. 程式人生 > >分組函數 多表連接 子查詢

分組函數 多表連接 子查詢

cit mis sql語法 city 語法 別名 employees rac 多表連接

count計數行數
sum 求和 avg 求平均值 min最小值 max最大值
例:elect avg(salary), avg(nvl(commission_pct, 0)) from employees;
//求所有人的提成平均值 包括沒有提成的 nvl:定義空值
只有count(*) 會把空值統計進去 count 不會統計空值
group by 根據……分組 空值不會被排除
只允許出現分組的列和分組的函數
例:select department_id, avg(salary) from employees group by department_id;
//查詢每個部門的平均工資 以部門為分組
多列分組
用逗號分隔
例:select department_id, job_id, max(salary) from employees group by department_id, job_id order by department_id;
//先按部門分組 部門相同的按工作分組 並且按照部門從小到大排序
having 相當於group by 之後的where語句
例:select department_id,avg(salary) from employees where job_id not like ‘_man%‘ and department_id is not null group by department_id having avg(salary)>=‘5000‘ order by avg(salary) desc;
//按部門求出所有有部門的普通員工的平均工資,部門平均工資少於5000的不顯示,最終結果按平均工資的降序排列。
多表連接
select e.last_name,d.department_name
from employees e,departments d
where e.department_id=d.department_id(+)
//from 給倆表定義別名 where後面(+)包含哪一個表的全部 from左邊的表包含進去就寫在where的右邊 同理右邊的表寫左邊
左外連接:107(106+1)
select e.last_name, d.department_name
from employees e, departments d
where e.department_id=d.department_id(+)

select e.last_name, d.department_name
from employees e left outer join departments d
on e.department_id=d.department_id;
右外連接:122(106+16)
select e.last_name, d.department_name
from employees e, departments d
where e.department_id(+)=d.department_id;

select e.last_name, d.department_name
from employees e right outer join departments d
on e.department_id=d.department_id;
完全外連接:123(106+1+16)
select e.last_name, d.department_name
from employees e full outer join departments d
on e.department_id=d.department_id;
n張表連接:
select e.last_name, d.department_name, l.city
from employees e, departments d, locations l
where e.department_id=d.department_id
and d.location_id=l.location_id;
SQL語法
select e.last_name, d.department_name, l.city
from employees e join departments d on e.department_id=d.department_id
join locations l on d.location_id=l.location_id;
左外鏈接
select e.last_name, d.department_name, l.city
from employees e, departments d, locations l
where e.department_id=d.department_id(+)
and d.location_id=l.location_id(+);
SQL語法
select e.last_name, d.department_name, l.city
from employees e left outer join departments d on e.department_id=d.department_id
left outer join locations l on d.location_id=l.location_id;
查詢所有員工姓名,部門名稱,部門所屬城市(city),國家(country)和區域(region)名稱,對於空值用“無”代替。(N/A)
(使用oracle和sql99的語法)
select e.last_name,d.department_name,l.city,c.country_name,r.region_name
from employees e,departments d,locations l,countries c,regions r
where e.department_id=d.department_id(+)
and d.location_id=l.location_id(+)
and l.country_id=c.country_id(+)
and c.region_id=r.region_id(+)
SQL語法
select e.last_name,d.department_name,l.city,c.country_name,r.region_name
from employees e left outer join departments d
on e.department_id=d.department_id
left outer join locations l
on d.location_id=l.location_id
left outer join countries c
on l.country_id=c.country_id
left outer join regions r
on c.region_id=r.region_id

分組函數 多表連接 子查詢