User Tools

Site Tools


kurs:correlated_subquery

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
kurs:correlated_subquery [2009/09/24 16:12]
mh
kurs:correlated_subquery [2020/06/02 09:26]
admin
Line 1: Line 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>​
kurs/correlated_subquery.txt ยท Last modified: 2022/03/15 10:39 by admin