w3resource_MySQL练习题:Joins

1. Write a query to find the addresses (location_id, street_address, city, state_province, country_name) of all the departments
Hint : Use NATURAL JOIN.
Sample table: locations
Sample table: countries
-- 1. 自然连接,MySQL自己判断多表的连接条件,分为内/外自然连接
select location_id, street_address, city, state_province, country_name
from locations
NATURAL JOIN countries
-- 2. 多表连接,在from里连接多张表,通过where进行匹配
select l.location_id, l.street_address, l.city, l.state_province, c.country_name
from locations as l, countries as c
where l.country_id=c.country_id
2. Write a query to find the name (first_name, last name), department ID and name of all the employees
Sample table: employees
Sample table: departments
-- 1. 多表连接
select e.first_name, e.last_name, e.department_id, d.department_name
from employees as e, departments as d
where e.department_id=d.department_id
-- 2. 左连接(显然employees表数量远大于departments表)
select e.first_name, e.last_name, e.department_id, d.department_name
from employees as e
left join departments as d
on e.department_id=d.department_id

 

3. Write a query to find the name (first_name, last_name), job, department ID and name of the employees who works in London
Sample table: departments
Sample table: locations
-- 1. 多表连接
select e.first_name, e.last_name, e.job_id, e.department_id, d.department_name
from employees as e, departments as d, locations as l
where e.department_id=d.department_id
and d.location_id=l.location_id
and l.city='London'
-- 2. 左连接
select e.first_name, e.last_name, e.job_id, e.department_id, d.department_name
from employees as e
left join departments as d
on e.department_id=d.department_id
left join locations as l
on d.location_id=l.location_id
where l.city='London'
4. Write a query to find the employee id, name (last_name) along with their manager_id and name (last_name)
Sample table: employees
-- 1. 使用多表连接
select e1.employee_id, e1.last_name, e2.employee_id, e2.last_name
from employees as e1, employees as e2
where e1.manager_id=e2.employee_id
-- 2. 使用内连接
SELECT e.employee_id 'Emp_Id', e.last_name 'Employee',
m.employee_id 'Mgr_Id', m.last_name 'Manager'
FROM employees e
join employees m
ON (e.manager_id = m.employee_id);

  

5. Write a query to find the name (first_name, last_name) and hire date of the employees who was hired after 'Jones'
Sample table: employees
-- 1. 通过where进行筛选
select first_name, last_name, hire_date
from employees
where hire_date>(
    select hire_date from employees where last_name='Jones'
)
-- 2. 内连接
SELECT e.first_name, e.last_name, e.hire_date
FROM employees e
JOIN employees davies
ON (davies.last_name = 'Jones')
WHERE davies.hire_date < e.hire_date;
6. Write a query to get the department name and number of employees in the department
Sample table: employees
Sample table: departments
-- 要点:多表连接后,在新表做group by
select d.department_name, count(*)
from employees as e, departments as d
where e.department_id=d.department_id
group by e.department_id

  

7. Write a query to find the employee ID, job title, number of days between ending date and starting date for all jobs in department 90
Sample table: employees
-- 要点:datediff()
SELECT employee_id, j.job_title, DATEDIFF(end_date, start_date)
FROM job_history
NATURAL JOIN jobs AS j
WHERE department_id = 90;

  

8. Write a query to display the department ID and name and first name of manager
Sample table: employees
Sample table: departments
-- 要点:以departments表为左表
select d.department_id, d.department_name, e.first_name
from departments as d
left join employees as e
won d.manager_id=e.employee_id

  

9. Write a query to display the department name, manager name, and city
Sample table: employees
Sample table: departments
Sample table: locations
-- 要点:以departments表为主表
select d.department_name, e.first_name, l.city
from departments as d
left join employees as e
on d.manager_id=e.employee_id
left join locations as l
on d.location_id=l.location_id
10. Write a query to display the job title and average salary of employees
Sample table: employees
-- 要点:group by分组计算组内均值
select job_id, avg(salary)
from employees
group by job_id
11. Write a query to display job title, employee name, and the difference between salary of the employee and minimum salary for the job
Sample table: employees
-- 要点:多表连接
SELECT job_title, first_name, salary-min_salary 'Salary - Min_Salary'
FROM employees
NATURAL JOIN jobs;
12. Write a query to display the job history that were done by any employee who is currently drawing more than 10000 of salary
Sample table: employees
Sample table: Job_history
-- 要点:多表连接
select j.*
from job_history as j, employees as e
where j.employee_id=e.employee_id
and e.salary>10000

  

13. Write a query to display department name, name (first_name, last_name), hire date, salary of the manager for all managers whose experience is more than 15 years
Sample table: employees
Sample table: departments
-- 要点:datediff
select e.first_name, e.last_name, e.hire_date, e.salary, datediff(now(), (e.hire_date)) as exp
from departments as d, employees as e
where d.manager_id=e.employee_id
and datediff(now(), (e.hire_date))>15

w3resource_MySQL练习:Joins的更多相关文章

  1. SQL Tuning 基础概述07 - SQL Joins

    N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...

  2. Part 7 Joins in sql server

    Joins in sql server Advanced or intelligent joins in sql server Self join in sql server Different wa ...

  3. [HIve - LanguageManual] Joins

    Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown ...

  4. 转载: C#: Left outer joins with LINQ

    I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my app ...

  5. The dplyr package has been updated with new data manipulation commands for filters, joins and set operations.(转)

    dplyr 0.4.0 January 9, 2015 in Uncategorized I’m very pleased to announce that dplyr 0.4.0 is now av ...

  6. ### 七种SQL JOINS

    七种SQL JOINS 1.SELECT FROM TABLEA A LEFT JOIN TABLEB B ON A.Key=B.Key 2.SELECT FROM TABLEA A RIGHT JO ...

  7. MySQL Block Nested Loop and Batched Key Access Joins(块嵌套循环和批量Key访问连接)

    Block Nested-Loop and Batched Key Access Joins Batched Key Access (BKA) Join算法通过index和join buffer访问j ...

  8. MySQL表与表之间的SQL Joins图介绍

    下图很好的解释了各表之间SQL Joins之间的关系

  9. Lerning Entity Framework 6 ------ Joins and Left outer Joins

    Joins allow developers to combine data from multiple tables into a sigle query. Let's have a look at ...

随机推荐

  1. Xshell连接不上虚拟机&连接提示SSH服务器拒绝了密码,请再试一次

    问题1:Xshell连接不上虚拟机 #启动ssh服务 /etc/init.d/ssh start #查看SSH服务22端口是否开启 netstat -antulp | grep ssh 问题2:XSh ...

  2. GUI的最终选择 Tkinter(八):Message组件、Spinbox组件、PanedWindow组件、Toplevel组件

    Message组件 Message(消息)组件是Label组件的变体,用于显示多行文本消息,Message组件能够自动执行,并调整文本的尺寸使其适应给定的尺寸. from tkinter import ...

  3. 中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter

    中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter 中间件的注册除了可以借助Startup对象(DelegateStartup或者ConventionBasedStar ...

  4. Windows7&IIS7.5部署Discuz全攻略

    组长说在内网部署一个论坛,这可难不倒我,装个Discuz嘛.部署环境就一台普通的PC,四核i3,Windows7.这就开搞了. 准备工作 系统是Windows 7 专业版,自带IIS7.5(家庭版不带 ...

  5. 基于CDH5.7.x Kylin部署

    配置目标文件为 /etc/profile #Kylin exportKYLIN_HOME=/opt/apache-kylin-1.5.4-cdh5.7-bin #Hadoop export HBASE ...

  6. kindeditor 修改上传图片的路径的方法

    默认情况下kindeditor上传的图片在编辑器的根目录/attached/目录下.以日期建一个目录,然后保存文件.有些时候大概我们并不想这样.考虑到更新编辑器,或更换编辑器不太方便.比如我现在想把上 ...

  7. java 多线程死锁

    死锁案例: package com.test; public class DealThread implements Runnable { public String username; public ...

  8. Coroutine(协程)模式与线程

    概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...

  9. autofac 注入生命周期

    创建实例方法 1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component ...

  10. 什么是JavaScript

    来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=30&cid= JavaScript是一种松散类型的客户端脚本语言,在用户浏览器中执 ...