R
R
random2015-09-08 08:57:08
MySQL
random, 2015-09-08 08:57:08

How to write complex queries in sql?

There is such a scheme, according to it you need to make the following queries:
1)
Select employees of the American and European regions (AMERICA and EUROPE), who have at least 2 transfers by position. The selection should contain the full name of employees, the name of their current position and the number of transfers .

SELECT first_name, last_name, job_title, count()
FROM regions, employees, jobs
Where region_name = ‘AMERICA’ and ‘EUROPE’ and count() > 2

Not really sure how to check positions
2)
For each department with at least 3 employees, select the number of employees, the minimum and maximum salary, and a column that sums up the salaries of employees who have worked more than 3 years. The sample must also contain the name of the region, country, and division.
SELECT department_id
FROM employees
group by department_id
having count(*) >=3

wrote only the output of departments where there are 3 or more employees
c2a626674e764792afd1e4be490b4d2a.jpg
ZY: there is no table itself

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nozzy, 2015-09-08
@demon123

1)
select bla.* from
(
select t1.first_name "f", t1.last_name "n", count(*) "c"
from employees t1
inner join job_history t2 on t2.employee_id = t1.employee_id
inner join departments t3 on t3.departments_id = t2.departments_id
inner join locations t4 on t4.location_id = t3.location_id
inner join countries t5 on t5.country_id = t4.country_id
inner join regions t6 on t6.region_id = t5.region_id
where t6.region_name in (' AMERICA', 'EUROPE')
group by t1.first_name, t1.last_name
) bla
where bla."c" > 2

R
Rsa97, 2015-09-08
@Rsa97

"the number of transfers by position is at least 2" - it means that JOBS_HISTORY should contain at least two or three records for this employee, depending on how this table is filled. So JOIN with SELECT.
For the second task - see the functions MIN(), MAX(), SUM() and the same JOIN with SELECT from EMPLOYEES.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question