w3resource_MySQL练习:Joins

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

猜你喜欢

转载自www.cnblogs.com/xingyucn/p/10540386.html