<?php
$c=OCILogon("hr", "hr", "orcl9");
if(!$c)
			{
			echo "Невозможно подключится к базе: " . var_dump( OCIError() );
			die();
			}
		$sql="select  E.Last_name as e_last, E.Salary as e_sal, C.Last_name as c_last, C.Salary as c_sal, E.Department_id, G.Avsal as avgsal";
		$sql=$sql." from HR.EMPLOYEES E, HR.EMPLOYEES C, (select Department_id, round(avg(Salary)) Avsal";
		$sql=$sql." from HR.EMPLOYEES group by Department_id ) G";
		$sql=$sql." where E.Department_id = C.Department_id";
		$sql=$sql." and C.Department_id = G.Department_id";
		$sql=$sql." and E.Salary > G.Avsal ";
		$sql=$sql." and E.Salary < C.Salary";
		$sql=$sql." and C.Employee_id <> E.Employee_id";
		$sql=$sql." and E.Department_id in (60,80)";
		$sql=$sql." order by";
		$sql=$sql." E.Department_id, E.Salary, E.Last_name, C.Salary , C.Last_name";
		$s = OCIParse($c, $sql);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
			$fam[$i]=ociresult($s,"E_LAST");
			$salary[$i]=ociresult($s,"E_SAL");
			$c_fam[$i]=ociresult($s,"C_LAST");
			$c_salary[$i]=ociresult($s,"C_SAL");
			$dep[$i]=ociresult($s,"DEPARTMENT_ID");
			$avsal[$i]=ociresult($s,"AVGSAL");
			$i++;
			}
?>
<table border=1><tr><th>Фамилия</th><th>Оклад</th><th>Фамилия</th><th>Оклад</th><th>Отдел</th><th>Ср.оклад</th></tr>
<?php	
for($i=0;$i<count($fam);$i++)
	{
	echo "<tr><td>$fam[$i]</td>";
	echo "<td>$salary[$i]</td>";
	echo "<td>$c_fam[$i]</td>";
	echo "<td>$c_salary[$i]</td>";
	echo "<td>$dep[$i]</td>";
	echo "<td>$avsal[$i]</td></tr>";
	}
?>
	</table>		
		</table>
	</body>
</html>