This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
kurs:correlated_subquery [2009/09/24 16:12] mh |
kurs:correlated_subquery [2022/03/15 10:39] (current) admin |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <code> | ||
+ | with cte_multiple_last_names as ( | ||
+ | select last_name, | ||
+ | count(*) c | ||
+ | from hr.employees | ||
+ | group by last_name | ||
+ | having count(*) > 1 | ||
+ | order by c desc | ||
+ | ) | ||
+ | select employee_id, | ||
+ | first_name, | ||
+ | last_name | ||
+ | from hr.employees where last_name | ||
+ | in (select last_name from cte_multiple_last_names) | ||
+ | |||
+ | select | ||
+ | emp.first_name, | ||
+ | emp.last_name | ||
+ | from | ||
+ | hr.employees emp | ||
+ | where | ||
+ | (select count(last_name) from hr.employees emp_sub where emp_sub.last_name = emp.last_name) > 1 | ||
+ | |||
+ | <code> | ||
+ | select first_name, last_name, e.employee_id, c from employees e, | ||
+ | (select employee_id, count(employee_id) c from job_history | ||
+ | group by employee_id | ||
+ | having count(employee_id) >= 2) g | ||
+ | where e.employee_id = g.employee_id | ||
+ | |||
+ | select first_name, last_name, employee_id from employees e | ||
+ | where (select count(*) from job_history where employee_id = e.employee_id) >= 2 | ||
+ | |||
+ | |||
+ | select first_name, last_name from employees e where | ||
+ | -- employee_id in (select manager_id from employees where e.employee_id = manager_id) | ||
+ | exists (select 1 from employees where e.employee_id = manager_id) | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | select first_name, last_name, e.employee_id, c from employees e, | ||
+ | (select employee_id, count(employee_id) c from job_history | ||
+ | group by employee_id | ||
+ | having count(employee_id) >= 2) g | ||
+ | where e.employee_id = g.employee_id | ||
+ | |||
+ | select first_name, last_name, employee_id from employees e | ||
+ | where (select count(*) from job_history where employee_id = e.employee_id) >= 2 | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | select * from ( | ||
+ | SELECT tst_fahrzeuge.fhz_name, count(tst_extras.ext_name) c | ||
+ | FROM tst_extras, tst_fahrzeuge, tst_fhz_ext_int | ||
+ | WHERE ( (tst_extras.ext_id = tst_fhz_ext_int.fei_ext_id) | ||
+ | AND (tst_fahrzeuge.fhz_id = tst_fhz_ext_int.fei_fhz_id) | ||
+ | ) | ||
+ | group by fhz_name | ||
+ | ) where c > 1 | ||
+ | |||
+ | |||
+ | select fhz_name from tst_fahrzeuge outer | ||
+ | where ( | ||
+ | SELECT count(ext_name) c | ||
+ | FROM tst_extras, tst_fahrzeuge inner, tst_fhz_ext_int | ||
+ | WHERE ( (ext_id = fei_ext_id) | ||
+ | AND (inner.fhz_id = fei_fhz_id) | ||
+ | and outer.fhz_name = inner.fhz_name | ||
+ | )) > 1 | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | with emp_per_country as ( | ||
+ | SELECT dep.department_id, | ||
+ | department_name, | ||
+ | cou.country_id, | ||
+ | country_name, | ||
+ | COUNT(employee_id) c | ||
+ | FROM hr.employees emp | ||
+ | JOIN hr.departments dep | ||
+ | ON emp.department_id = dep.department_id | ||
+ | JOIN hr.locations loc | ||
+ | ON loc.location_id = dep.location_id | ||
+ | JOIN hr.countries cou | ||
+ | ON cou.country_id = loc.country_id | ||
+ | GROUP BY dep.department_id, department_name, cou.country_id, country_name | ||
+ | ORDER BY COUNT(employee_id) DESC | ||
+ | ) | ||
+ | select country_name, department_name, c from emp_per_country o | ||
+ | where c = (select max(c) from emp_per_country where country_id = o.country_id) | ||
+ | </code> |