English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I was asked this question in one of the interview on php

2007-01-23 15:43:29 · 1 answers · asked by Brajendra 3 in Computers & Internet Programming & Design

1 answers

I think you mean "subquery" and a "correlated subquery".

Subquery:
select employee_name
from employees
where emp_no in (select distinct emp_no from late_attendance)

Correlated Subquery:
select e1.emp_no, e1.dept_cd, u.active_cd
from user_profile u, employee e1
where u.emp_no=e1.emp_no
and e1.effective_date = (select max(effective_date) from employee e2 where e1.emp_no=e2.emp_no)

Notice in the first query, the inner query (the sub query) runs independant of the outer query. Conversely, in the correlated subquery, the inner query requires information from the outer query in order to run (e1.emp_no = e2.emp_no).

To help understand why this would be done, assume that the employee table inserts a new row with a new effective date each time a change is made. Thus, obtaining the latest effective dated record.

2007-01-23 17:30:15 · answer #1 · answered by BigRez 6 · 0 0

fedest.com, questions and answers