You are on page 1of 4

2010

ORACLE Fundamental_ SQL 1‫  أ


 أوراآ‬

Moh&Ali
‫ إهاء إ‬WWW.ARAB
/OUG/HARDWARE/TEAM2000
3/21/2010
LES 06 Using Subqueries to Solve Queries
Practice 6

1.The HR department needs a query that prompts the user for an employee last name. The query then
displays the last name and hire date of any employee in the same department as the employee whose
name they supply (excluding that employee). For example, if the user enters Zlotkey, find all
employees who work with Zlotkey (excluding Zlotkey).

 
 ‫ 
ل ان إدار   إ م‬: 1 ‫ ا"! ال‬#$
%&' (
) ‫ إدارة‬+,) -.‫ د‬/"‫ ا‬-
,01" -

"‫ ا‬2.‫ور‬
../; ) ‫د&  ا م‬45 ‫ ا م‬/ . ‫  ة‬#‫ آ‬7"8‫د‬9 ‫
ن‬

select last_name,hire_date,department_id

from employees

where department_id = ( select department_id

from employees

where last_name = '&&name')

and last_name <> '&name';

2. Create a report that displays the employee number and last name of all employees who earn
more than the average salary. Sort the results in order of ascending salary.
#=‫@ون  ? أ‬8A. -.@" (B1‫ وا‬-
,01"‫ ;وز ر= ا‬: 2 ‫ ا"! ال‬#$
. ? 1"‫!? ا‬$ /; .4;D E5
"‫ ت ور? ا‬1"‫ ا‬G -
select employee_id,last_name

from employees

where salary < (select avg(salary)

from employees)

order by salary;
3. Write a query that displays the employee number and last name of all employees who work in a
department with any employee whose last name contains a u. Place your SQL statement in a text file
named lab_06_03.sql. Run your query.

/) ‫ن‬1 . -.@"‫ ا‬-


,01"‫ " ض ر= وا ا‬.  : 3 ‫ ا"! ال‬#$
. U ‫ ف‬$ /; ‫ى‬L. 71‫ ا‬M0 ( ‫أى إدارة‬
select employee_id,last_name

from employees

Where department_id= any( select department_id

from employees

where last_name like '%u%’);

4.The HR department needs a report that displays the last name, department number, and job ID of all
employees whose department loca6on ID is 1700.

-
,01"‫ ا‬E,
0‫ ض ا ور= إدارة وو‬. .  :4 ‫ ا"! ال‬#$
‫   ه  وف ان‬1700  = =‫ إدارة ر‬/) ‫ن‬1 . -.@"‫ا‬
#1 P ‫دارات ) زم‬O‫ ار=م ا= ا‬7 +
" -
,01"‫ول ا‬4
. ‫دارات‬O‫ول ا‬4 /; /; ,"‫ م ا‬O‫ا‬
select last_name,department_id,job_id
from employees
where department_id in (Select department_id
from departments
where location_id=1700)/
where location_id=&id);

5.Create a report for HR that displays the last name and salary of every employee who reports to King.

KING ‫دارة‬9) 1


 /"‫ ا‬-
,01" 7 ‫ و‬M01"‫  ا‬.  :5 ‫ ا"! ال‬#$

select last_name,salary

from employees

where manager_id= any( select employee_id

from employees

where last_name like 'King');


6.Create a report for HR that displays the department number, last name, and job ID for every employee in the
Executive department.
. E.@
,"‫دراة ا‬R" E,
0"‫دارة وا وا‬O‫  = ا‬.  ‫;وز‬ : 6 ‫ ا"! ال‬#$
select last_name,job_id,department_id

from employees

/ where manager_id is null/

where department_id=( select department_id


from employees
where manager_id is null)

If you have time, complete the following exercise:

7.Modify the query in lab_06_03.sql to display the employee number, last name, and salary of all
employees who earn more than the average salary and who work in a department with any employee
whose last name contains a u. Resave lab_06_03.sql as lab_06_07.sql. Run the statement in lab_06_07.sql.

( ‫( و‬1‫( وا‬1=‫ ر‬-


,01"‫;وز ا‬:7 ‫ ا"! ال‬#$

71‫ ا‬4$ (
) (‫ وادار‬G1"‫ ا‬- /;‫  ( ا‬/"‫ا‬
. U ‫ ف‬$ /)
select employee_id,last_name,salary,department_id

from employees

Where salary>( select avg(salary)

from employees)

and department_id= any(select department_id

from employees

where last_name like '%u%';

You might also like