一文然你搞懂sql92的等值連接,自連接,非等值連接的用法

sql92連接的介紹及使用

一:等值連接

等值連接特點:

一:多表等值連接的結果爲多表的交集部分

二:n表連接,至少需要n-1個連接條件

三:多表的順序沒有要求

四:一般需要爲表起別名

五:可以搭配前面介紹的所有子句使用,比如排序,分組,篩選

案例一:查詢女神名和對應的男生名

select name,boyName from boys,beauty
where beauty.boyfriend_id=boys.id;

案例二:查詢員工名對應的部門名

select last_name,department_name
from employees,departments
where
employees.department_id=departments.department_id;

表表起別名

提高語句的簡介度
區分多個重名的字段

注意:

如果爲表起了別名,則查詢的字段就不能使用原來的表名去限定

案例:查詢員工名,工種名,工種號

select last_name,e.job_id,job_title
from employees as e,jobs as j
where e.job_id=j.job_id;

可以添加篩選條件等值連接

案例:查詢有獎金的員工名,部門名

select last_name,department_name,commission_pct
from employees,departments
where employees.department_id=departments.department_id
and employees.commission_pct is not null;

案例:查詢城市名中第二個字符爲o的部門名和城市名

select department_name,city
from departments,locations
where departments.location_id=locations.location_id
and city like '_o%';

可以加分組等值連接

案例:查詢每個城市的部門個數

select count(*),city
from locations,departments
where
locations.location_id=departments.location_id
group by city;

案例:查詢有獎金的每個部門的部門名和部門的領導編號和該部門的最低工資

select d.department_name,
e.manager_id,
min(salary)
from employees e,
departments d
where
e.department_id=d.department_id
and commission_pct is not null
group by department_name,
manager_id;

可以加排序

案例:查詢每個工種的工種名和員工的個數,並且按員工個數降序

select job_title,
count(*)
from employees e,
jobs j
where j.job_id=e.job_id
group by j.job_id
order by count(*) desc;

可以實現三表連接

案例:查詢員工名,部門名和所在的城市

select
last_name,
department_name,
city
from employees e,
departments d,
locations l
where
e.department_id=d.department_id
and
d.location_id=l.location_id;

非等值連接

案例:查詢員工的工資和工資級別

select 
salary,
grade_level
from employees e,
job_grades g
where
salary between 
g.lowest_sal
and g.highest_sal;

自連接

案例:查詢員工名和上一級的領導名

select
e.employee_id,
e.last_name,
m.employee_id,
m.last_name
from 
employees e,
employees m
where 
e.manager_id = m.employee_id;

練習題:

顯示員工表的最大工資,工資平均值

select
	max(salary),
	avg(salary)
	from employees;

查詢員工表的employee_id,job_id,last_name,按department_id降序,salary升序

select
employee_id,
job_id,
last_name
from 
employees
group by department_id desc,
salary asc;

查詢員工表的job_id中包含a和e的,並且a在e的前面

select 
job_id
from 
employees
where 
job_id
like '%a%e%';

已知表student,裏面有id(學號),name,gradeld(年級編號) 已知表grade,裏面有id(年級編號),name(年紀名) 已知表result,裏面有id,score,studentNo(學號) 要求查詢姓名,年紀名,成績

select s.name,g.name,result
from student s,grade g,result r
where 
s.gradeId=g.id
and
s.id=r.studentNo;

顯示當前日期,以及去前後元素,截取字符串的函數

select now();
select trim();
select trim(字符 from '');
select substr(str,startindex);
select substr(str,startindex,length);

sql92總結:

字符函數:

在這裏插入圖片描述

數學函數:

在這裏插入圖片描述

其他函數:

在這裏插入圖片描述

日期函數:

在這裏插入圖片描述

流程控制函數:

在這裏插入圖片描述
在這裏插入圖片描述

分組函數

在這裏插入圖片描述

特點:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

分組查詢

在這裏插入圖片描述

連接查詢:

在這裏插入圖片描述

分類:

按年代分類

sql92

在這裏插入圖片描述

sql99

在這裏插入圖片描述

sql92語法:

一:等值連接:

在這裏插入圖片描述

等值連接特點:

在這裏插入圖片描述

非等值連接:

在這裏插入圖片描述

自連接:

在這裏插入圖片描述

練習題:

顯示所有員工的姓名,部門號,部門名稱

select last_name,department_name,departments.department_id
from employees,departments
where 
employees.department_id=departments.department_id;

顯示90號部門員工的Job_id和90號部門的location_id

select job_id,location_id
from employees e,
departments d
where e.department_id = d.department_id
and e.department_id=90;

選擇所有有獎金的員工的last_name,department_name,location_id,city

select last_name,department_name,locations.location_id,
city
from employees,
departments,
locations
where
employees.department_id=departments.department_id
and
departments.location_id=locations.location_id
and
employees.commission_pct is not null;

選擇city在Toronto工作的員工的last_name,job_id,department_id,department_name

select e.last_name,
job_id,
d.department_id,
department_name,
l.city
from
employees e,
departments d,
locations l
where
l.city='Toronto';

查詢每個工種,每個部門的部門名,工種名和最低工資

select d.department_name,
j.job_title,
min(salary)
from 
employees e,
departments d,
jobs j
where
e.department_id=d.department_id
and
e.job_id=j.job_id
group by
j.job_title,
d.department_name;

查詢每個國家下的部門個數大於2的國家編號

select country_id,
count(*)
from 
locations
group by
country_id
having
count(*)>2;

選擇指定員工的姓名,員工名,以及他的管理者的姓名和員工名,結果類似於下面的格式

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章