Вы находитесь на странице: 1из 5

2010

ORACLE Fundamental_ SQL 1 ‫ أ أوراآ‬

Mustafa
‫ إهاء إ‬WWW.ARAB
/OUG/HARDWARE/TEAM2000
3/21/2010
LES 04 Reporting Aggregated Data
Using the Group Functions
Practice 4 :

Determine the validity of the following three statements. Circle either True or False.

-:  ‫ا ال
أو‬

1. Group func*ons work across many rows to produce one result per group.
True/False

.(00
)"./‫)ا‬.   ‫"ن   وا‬# ‫ف‬%
 &' (#) *‫ ه اوال ا‬-1
2. Group func*ons include nulls in calcula*ons.
True/False

9":‫ت إ‬/" &9 /‫ إ‬2‫"ر‬%‫ ا‬45‫ن اوال ا* "ه ا‬8 . ( 67" )".8‫)ا‬. 2‫"ر‬%‫ ا‬45‫ف ا‬#) *‫ ه ا‬-2
. (count (*)) ‫ام اا‬DE‫& " أ‬9 ‫او‬،(?
ً @ ‫"م‬B‫ارغ ا& ار‬%‫= ا‬0 NVL) ‫اا‬

3.The WHERE clause restricts rows prior to inclusion in a group calculation.


True/False

'. "I ‫م‬D= *‫ن اوال ا‬8 J‫") ( وذ‬./‫ )ا‬. *‫ اوال ا‬G "*' 'H Where '‫ ه ا‬-3
.*‫ "رج اا ا‬8‫& ا‬B") G Where ‫ام‬DE‫ أ‬L= L‫ و‬Where '. ‫ام‬DE‫" ا‬I* H= /‫ و‬Having

The HR department needs the following reports:

4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole
number. Place your SQL statement in a text file named lab_04_04.sql.

‫ ا‬S=O5 ‫ و‬4I  =) 4E‫ ا‬G:‫ و‬G L%T‫"ت ا‬P O UE‫ وع و‬O(
‫ وأ‬OP‫ أآ‬L ‫ "وز آ‬: 4 ‫ ا ال‬
. 0
‫ب د‬OB/

Select round(Max(Salary),0) "Maximum",Round(Min(Salary),0)


"Minimum",Round(Avg(Salary),0) "Average",Round(Sum(Salary),0) "Sum"

From Employees;
5. Modify the query in lab_04_04.sql to display the minimum, maximum, sum, and average salary
for each job type. Resave lab_04_04.sql as lab_04_05.sql. Run the statement in lab_04_05.sql.

G U59 4 ‫ ال‬E &9 ‫"ت‬9 &'‫*?م ا‬EX‫( ا‬#= ‫ ه" "وز‬:5 ‫ ا ال‬
....%T‫ ا‬4E‫ام ا‬DEY) ‫"س ا‬E‫ '& أ‬S O‫ا‬

select job_id, round(max(salary),0) "Maximum",round(min(salary),0)


"Minimum",round(avg(salary),0) "average",round(sum(salary),0) "sum"

from employees

group by (job_id);

6.Write a query to display the number of people with the same job.
.count ‫ف دا‬#‫دارة ه" ه‬X‫ دا ا‬%T‫(' آ و‬#) &'‫ ا‬L%T‫ "وز ع د ا‬: 6 ‫ ا ال‬

select job_id, count(*)

from employees

group by job_id;

7.Determine the number of managers without lis*ng them. Label the column Number of Managers.
Hint: Use the MANAGER_ID column to determine the number of managers.
.Count ‫م دا‬D "I9 ‫دارة‬X") L=O=‫ ه" "وز د ا‬: 7 ‫ ا ال‬

select count(distinct(manager_id))

from employees;

8.Find the difference between the highest and lowest salaries. Label the column DIFFERENCE.
.. S O B‫ وأ‬S O &'‫ أ‬L) ‫ق‬O%‫ ه" "وز ا‬:8 ‫ ا ال‬

SELECT max(salary)-min(salary) "difference"

from employees;
If you have time, complete the following exercises:

9.Create a report to display the manager number and the salary of the lowest-paid
employee for that manager. Exclude anyone whose manager is not known. Exclude any groups
where the minimum salary is $6,000 or less. Sort the output in descending order of salary.

‫ط‬O#) ‫ وا‬O= 4BO *)"   S O B‫ ه" "وز ا‬:9 ‫ ا ال‬
select min(salary),manager_id
‫ أو ="وى‬L B‫ أ‬S O B‫ =ن أ‬/‫ " و‬B ‫ =ن‬/ O=‫ ا‬4B‫أن ر‬
from employees
.S O B‫"س ا‬E‫ '& أ‬S O‫ ا‬G 6000
where manager_id is not null

group by manager_id

having min(salary)>6000

order by salary;

If you want an extra challenge, complete the following exercises:

10.Create a query to display the total number of employees and, of that total, the number of
employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings.

1998،1997،1996،1995 &9  L=`'‫ 'ب د ا*"ل ا‬: 10 ‫ ا ال‬

‫ى ه‬B "D (# L=‫اااااااااااا ه" "وز‬. . ‫ ال‬E. 4I") ‫واع‬
"I‫ط أ‬Oa * L=‫ "وز‬S7 (Sum) ‫ وه& اا‬G) &'‫" ا‬I ‫ف دوال‬O* "") H%' P") &5P= E 9 * ‫"وز د ا*"ل ا'& ا‬
a ‫ *ل‬L."0 S7 SUM ‫" دا‬9O S=O2 "I' S7 4I") ‫ اع‬G' bB‫ ا‬c%9‫ و‬4I* ‫ @? و‬1995 ‫ف ا'& ا *ا ف‬#
S7 TO_CHAR(HIRE,’YYYY’) ‫م‬D‫ و‬e"@‫ ا‬H%' G.O &5P= c) ‫ ا‬L=‫ "وز‬/// E OIa ‫ =م‬4"9 G') ‫ آ"ن ه‬d=‫ا"ر‬
G' 1995 b"‫  آ‬L*‫ ا‬E &' b * ‫ دا‬L."0 f:O) &5P= ‫ ا دى‬G' ‫"ن‬# f=‫ " * ا‬4I‫") ا‬.X‫ ار)"ع ا‬b' ‫آ‬
‫ا‬9 O% S7 E ‫" '& آ‬I5P‫ و‬1995 4I* E &'‫ ا‬L%T‫ ه"وا د ا‬5P= ‫ ا"= دول‬G ‫م‬5 SUM ‫ا‬9 (1) ?@ 4B‫ر‬
b'59 4‫ ا‬4a ‫ دول‬L‫ ا‬L9‫"ن أ "ر‬# f c) S=O2 0‫ا‬..."5% ‫ ا‬Decode ‫ دا‬G'‫ د ه‬b‫ )* ا‬b"‫ آ‬3‫ ال‬H%‫ ا‬O9
COUNT> ‫ )ا‬L%T‫ق  " ا‬9 ‫ه‬....5-6-7-8 ‫ ا‬4B‫& ر‬9 O()‫و‬SUM G)‫ر‬8‫ ا‬d) &*= ‫ر‬O)‫ة وا و‬O9 0‫ ا‬...0‫أن ا‬
‫ ا‬4B‫ ر‬G'7 TO_CHAR '  " ‫& " أن‬9 "I) S= ‫"ن‬# DECODE "‫ وا‬SUM "I9‫و& "ر‬/‫ دوال ا‬3 ‫م‬D ‫ راح‬L=*)‫و‬
SUM ‫ دا‬1 b*'7 '9 0 G' ‫ ه‬h ‫ و‬1 ‫ ا‬G' "‫ ه‬, ‫ف  )"وى ا'& )* ال‬# ‫ و‬DECODE ‫( اا‬# "‫ ه‬1995 ?@
. ‫ وه`اااااااااااااا‬1995 E &9 * ‫ ا'& أ‬L%T‫ آ ا‬G' ‫ ا"= و‬G

SELECT count(Employee_id),

sum(decode(to_char(hire_date,'yyyy'),1995,1,0)) "1995”,

sum(decode(to_char(hire_date,'yyyy'),1996,1,0)) "1996",

sum(decode(to_char(hire_date,'yyyy'),1997,1,0)) "1997",

sum(decode(to_char(hire_date,'yyyy'),1998,1,0)) "1998"

from employees ;
11. Create a matrix query to display the job, the salary for that job based on department number,
and the total salary for that job, for departments 20, 50, 80, and 90, giving each column an
appropriate heading.

......10 c ‫*ا‬.‫" أر‬IO#‫ ه‬h‫ و‬10‫ س‬b") "" b "9 &' P# ‫ة‬O9 :11 ‫ ا ال‬

select job_id,

sum(decode(department_id,20,salary,0)) "20”,

sum(decode(department_id,50,salary,0)) "50",

sum(decode(department_id,80,salary,0)) "80",

sum(decode(department_id,90,salary,0)) "90",

sum(salary) "TOTAL"

from employees

group by job_id,department_id;

Вам также может понравиться