SQL 基础之多表查询(十)

发布时间:2020-06-20 19:28:21 作者:yuri_cto
来源:网络 阅读:491

JOINS 类型和它的语法

Natural joins(自然连接):

– NATURAL JOIN 子句

– USING 子句

– ON 子句


自连接

  非等值连接

  

Outer joins(外连接):

– LEFT OUTER JOIN(左外连接)

– RIGHT OUTER JOIN(右外连接)

– FULL OUTER JOIN(全外连接)


笛卡尔积

– Cross join(交叉连接)


语法:

select table1.column, table2.column

from table1

[natural join table2] |

[join table2 using (column_name)] |

[join table2

on (table1.column_name = table2.column_name)]|

[left|right|full outer join table2

on (table1.column_name = table2.column_name)]|

[cross join table2];


限制重复的列名

    – SQL代码量更少,使用较少的内存


创建自然连接


1、查询department_id 和department_name 在哪些城市

select department_id,department_name, location_id,city from departments natural join locations;

SQL 基础之多表查询(十)


使用 USING 子句创建连接


1、查询employee_id,last_name,location_id 从员工表,并且使用department_id为指定键值

select employee_id, last_name,location_id, department_id from employees join departments using (department_id);

SQL 基础之多表查询(十)


ON 子句创建连接


1、查找employees和 的departments 两张表员工信息,并按照department_id为条件

select e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id from employees e join departments d on (e.department_id = d.department_id);

SQL 基础之多表查询(十)


使用AND子句或WHERE子句适用附加条件: 查询manager_id为149的

select e.employee_id, e.last_name, e.department_id,

d.department_id, d.location_id

from employees e join departments d

on (e.department_id = d.department_id)

and e.manager_id = 149;

SQL 基础之多表查询(十)

或者

select e.employee_id, e.last_name, e.department_id,

d.department_id, d.location_id

from employees e join departments d

on (e.department_id = d.department_id)

where e.manager_id = 149 ;

SQL 基础之多表查询(十)


使用 ON 子句自连接

使用条件 WORKER 表 表 MANAGER_ID 等于MANAGER 的EMPLOYEE_ID

select worker.last_name emp, manager.last_name mgr

from employees worker join employees manager

on (worker.manager_id = manager.employee_id);


非等值连接

select e.last_name, e.salary, j.grade_level

from employees e join job_grades j

on e.salary

between j.lowest_sal and j.highest_sal;


使用外连接返回没有直接匹配的记录


左外连接

select e.last_name, e.department_id, d.department_name

from employees e left outer join departments d

on (e.department_id = d.department_id) order by department_id desc;

SQL 基础之多表查询(十)


右外连接

select e.last_name, e.department_id, d.department_name

from employees e right outer join departments d

on (e.department_id = d.department_id) ;

SQL 基础之多表查询(十)


全外连接

select e.last_name, d.department_id, d.department_name

from employees e full outer join departments d

on (e.department_id = d.department_id) ;

SQL 基础之多表查询(十)



笛卡尔积


– 连接条件被遗漏

– 连接条件不正确

– 所有表中的所有行互相连接


创建交叉连接


SQL 基础之多表查询(十)

select last_name, department_name from employees cross join departments;



练习题:

1. 为HR部门写一条查询语句,要求结果生成所有部分的地址。请使用 LOCATIONS 和 COUNTRIES 表,

要求输出 location_id,street_address,city,state_province,以及 country。使用自然连接获得要求的结果

select location_id,street_address,city,state_province,country_id,country_name from locations  natural join countries;

SQL 基础之多表查询(十)



2. HR部门需要一个可以查出所有员工的 last_name,department_id,department_name 的查询语句

select last_name,department_id,department_name from employees join departments using(department_id);

SQL 基础之多表查询(十)


3.  请查出所有在 Toronto工作的员工的 last_name,job_id,department_id,department_name.

select e.last_name,e.job_id,e.department_id,d.department_name from employees e join departments d

on (e.department_id = d.department_id)

join locations l

on (d.location_id = l.location_id)

where l.city ='Toronto';

SQL 基础之多表查询(十)

如果Toronto 为小写的toronto可以用下面语句

select e.last_name,e.job_id,e.department_id,d.department_name from employees e join departments d

on (e.department_id = d.department_id)

join locations l

on (d.location_id = l.location_id)

where lower(l.city) ='toronto';


4.  请查询出每个员工的 last_name,employee_id,经理的 last_name,manager_id。请依次为这

些 列 取 合 适 的 别 名 ”Emeployee”,”EMP#”,”Manager”,”Mgr#”。


select w.last_name "Employee",w.employee_id "EMP#",

m.last_name "Manager",m.employee_id "Mgr#"

from employees w join employees m

on (w.manager_id = m.employee_id);

SQL 基础之多表查询(十)


5、显示所有员工(包括没有经理的 King),请根据员工编号进行排序

select w.last_name "employee", w.employee_id "emp#",

m.last_name "manager",m.employee_id "mgr#"

from employees w

left outer join employees m

on (w.manager_id = m.employee_id)

order by 2;

SQL 基础之多表查询(十)


6.  请查询出每个员工的 department_id,员工的 last_name,以及在同一部门一起工作的同事

select e.department_id department,e.last_name employee,

c.last_name colleague

from employees e join employees c

on (e.department_id = c.department_id)

where e.employee_id <> c.employee_id

order by e.department_id,e.last_name,c.last_name;

SQL 基础之多表查询(十)


7、查 询 所 有 员 工 的last_name,job_id,department_name,salary,grade_level。

select e.last_name,e.job_id,d.department_name,

e.salary,j.grade_level

from employees e join departments d

on (e.department_id = d.department_id)

join job_grades j

on (e.salary between j.lowest_sal and j.highest_sal);


8、 HR 部门的同事想知道所有在 Davies 之后被雇佣的员工。请帮忙写出一条 SQL 查出来这些员工的last_name 以及 hire_date

select e.last_name,e.hire_date

from employees e join employees davies

on (davies.last_name='Davies')

where davies.hire_date < e.hire_date;

SQL 基础之多表查询(十)


9.  HR 部门的同事想要找出那些在他们的经理之前被雇佣的员工的 last_name,hire_date,以及他们

经理的 last_name,和 hire_date

select w.last_name,w.hire_date,m.last_name,m.hire_date

from employees w join employees m

on (w.manager_id=m.employee_id)

where w.hire_date < m.hire_date;

SQL 基础之多表查询(十)


推荐阅读:
  1. MySQL之SQL基础
  2. SQL基础之使用集合运算符进行多表查询(十二)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql 表查询 基础

上一篇:flume案例支持

下一篇:PHP 并发过程中的数据乐观锁

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》