一文然你搞懂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;

选择指定员工的姓名,员工名,以及他的管理者的姓名和员工名,结果类似于下面的格式

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值