Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ORACLE Fundamental - SQL 1 آاروأ أ: Moh&Ali

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

2010

ORACLE Fundamental_ SQL 1‫  أ


 أوراآ‬

Moh&Ali
‫ إهاء إ‬WWW.ARAB
/OUG/HARDWARE/TEAM2000
3/21/2010
LES 03 Using Single-Row Functions to
Customize Output
Prac!ce 3 Part 1

1.Write a query to display the current date. Label the column Date.

‫ ا" ! ن   م دا  ا‬#‫ و‬%‫ ا
 د‬:1 ‫ اال‬
! &'  () * + , ‫ ون‬-.
‫ * ا‬/' 0 " !‫ و‬1‫ دا و‬2!
...  8‫ ا‬%6‫ ه‬3-) ‫ن‬4 ! %‫آ دوال ا
 د‬
select to_char(last_day('01-dec-2003'),'dd-mm-yy')

from dual;

2. The HR department needs a report to display the employee number, last name, salary, and salary
increased by 15.5% (expressed as a whole number) for each employee. Label the column New Salary.
Place your SQL statement in a text file named lab_03_02.sql.
‫د‬.‫ ا‬,)‫ و‬%15.5 - 1 < ‫ !=اد‬34 ‫ وا‬1-4 !‫ ا>  و‬1)‫ ا@? وا‬A '  *4 :‫ <وز ه‬: 2 ‫ اال‬
%‫ ا=د‬.' 34 ‫ وا‬C*‫ ا‬34 ‫ ا‬1 ‫ و<وز <د <د‬C(<'  @‫ '  وا =ودوا ا‬:‫ ه ه‬D‫ ا‬34 '
... ,:. ‫ى‬A ?‫ ه‬M!  N)‫ أ‬3 ‫ ف‬1 < .‫ ه‬1K   ‫م واال‬E.)F‫ ا‬G ‫ أ‬CI  *‫ا‬
select employee_id,last_name,salary,(salary*.155)+salary "NEW SALARY"

from employees;

3.Run your query in the file lab_03_02.sql.


‫ ت‬, ‫ ال ا‬%:P  , ‫م ا‬E.)F‫ ا‬Q ‫ '*ل‬: 3 ‫ اال‬
20‫را‬... ‫ * 
 ا>ول‬/‫ وا‬Run Query .

4. Modify your query lab_03_02.sql to add a column that subtracts the old salary from the new salary.
Label the column Increase. Save the contents of the file as lab_03_04.sql. Run the revised query.

select employee_id,last_name,salary,(salary*.155)+salary
"NEW SALARY",( (salary*.155)+salary)-salary “Increase”

from employees;

(salary*.155) “Increase” ‫ ح‬/‫ ا‬3 + ‫ '& ه‬. ! ‫ @   آن‬M! "‫ * ا‬+ ً .-+
5. Write a query that displays the last name (with the first le?er uppercase and all other letters
lowercase) and the length of the last name for all employees whose name starts with the letters J, A,
or M. Give each column an appropriate label. Sort the results by the employees’ last names.

(M&J&A )1) ‫ ط أن ن أول  ف‬8' 1)‫ و<د  وف أ‬-‫ ا@? آ‬C)‫ ا" ف ا>ول ! ا‬:‫ <وز ه‬: 5 ‫ اال‬

select initcap(last_name),length(last_name)

from employees

where last_name like 'M%' or last_name like 'A%' or last_name like 'J%'

order by last_name;

Rewrite the query so that the user is prompted to enter a letter that starts the last name. For example, if
the user enters H when prompted for a letter, then the output should show all employees whose last name
starts with the letter H.

4‫ أ‬2! ‫ ط آ ه و‬8‫ ا‬.0 2! X'‫م ا‬E.)F‫'*ل ا< آ' ا‬: 5 ‫ اال‬2'4
\‫ ات‬Q‫ * ا‬/' 1 .‫ا‬
select initcap(last_name),length(last_name)
Sub_val&
from employees

where last_name like '&lastname%'

6. The HR department wants to find the length of employment for each employee. For each employee, display the
last name and calculate the number of months between today and the date on which the employee was hired. Label
the column MONTHS_WORKED. Order your results by the number of months employed. Round the number of
months up to the closest whole number.

2! (8' C( ]@‫ و‬, ‫^ه ا@ ن‬A ,‫*  'ة ا‬4 HR ‫\ إدارة‬:! 3 /4 :6 ‫ اال‬
3 *4‫ أ)س اة و‬, < 1-4 4‫ و‬.‫(ر ا‬8 `4:‫ ا‬C)‫ ا‬Q4‫ اة و‬2! ?@! ‫ آ‬C)‫< ض ا‬
(:‫اااًا <وز اة 'اة <ر‬0 ?‫ '& ه‬b ‫ ه‬c"d CA‫ ب ر‬A> ‫` اة‬4
... ‫ اة‬, < 1-4 ‫ ب ( و‬A> `4:‫ * ب ا‬.'‫( و‬MONTHS_BETWEEN)

select last_name,round(months_between(sysdate,hire_date),0)
"MONTHS_WORK"

from employees

order by months_work ;
Part 2:

7. Create a report that produces the following for each employee: <employee last name> earns
<salary> monthly but wants <3 !mes salary>. Label the column Dream Salaries.

‫ ! اال أن 'ع اارد‬cg‫ وا‬.-+ : 7 ‫ اال‬


C: Q8! C(‫ أ‬:'‫ أوا ر‬M .! &' g ‫  س‬8-‫ا‬
‫ن 'ع‬8< ‫!ت‬E.)‫ <ا إ‬- ‫وراآ‬K‫ش ا‬:! ‫ن‬8<
‫ 'م‬i' ‫ اال‬3+...‫وش =ه*ا‬.*! '  ‫ا‬
... `!‫ ا‬d
Select last_name||'EARNS'||to_char(salary,'$99,999.00')||'MONTHLY BUT
WANTS'||TO_CHAR(salary*3,'$99,999.00') as "DREAM SALARIES"

from employees ;

If you have time, complete the following exercises:

8. Create a query to display the last name and salary for all employees. Format the salary to be 15
characters long, left-padded with the $ symbol. Label the column SALARY.

‫ ا>وراآ ه <وز‬j ‫ !رش‬kA‫ و‬:< 1*:‫ ه‬1g ' .-+ : 8 ‫ اال‬
‫ زى‬$ !E. g‫ل إ‬8‫ ( ! ا‬. ‫م‬A‫ ا>ر‬,A'‫ و‬CA‫ر‬15 ! ‫ت‬-4 ‫<د ا‬
. ‫ت‬-4 ' ‫د‬.‫ ا‬,‫!   و‬
Select last_name,lpad(salary,15,’$’)

From employees;

9. Display each employee’s last name, hire date, and salary review date, which is the first Monday
after six months of service. Label the column REVIEW. Format the dates to appear in the format
similar to “Monday, the Thirty-First of July, 2000.”

! ?@‫ ا‬.' :I‫ ا*  آ= <وز أول م أ‬:‫ ه‬3+ : 9 ‫ اال‬
C)‫( وا‬8  -:' ‫ ام‬C)‫ ام وا‬C)‫ * دى ا‬/' ‫ ض‬.‫(ر و‬6
....‫م‬A‫ار‬4 :‫( وا‬8‫ا‬

select last_name,hire_date,
to_char(next_day(add_months(hire_date,6),’Monday’)’day,ddspth,month,yyyy') "REVIEW"

from employees;
-10-Display the last name, hire date, and day of the week on which the employee started. Label the
column DAY. Order the results by the day of the week, starting with Monday.

.‫ع‬-)o -:' .‫ وم ا‬.‫ ا‬n‫ر‬4‫ و‬C)K‫ ا‬:‫ <وز ه‬:10 ‫ اال‬

Select last_name,hire_date,to_char(hire_date,'day') 'DAY'

from employees

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


11.Create a query that displays the employees’ last names and commission amounts. If an employee
does not earn commission, show “No Commission.” Label the column COMM.

‫ !'ش‬1‫( !( ا‬P < ‫ !'ش‬1‫  ا‬,‫ و‬1<' .‫ ا@? وا‬C)‫ <وز ا‬:11 ‫ اال‬
D:‫" ا‬4 , " ‫م‬A‫ ا>ر‬2! ‫م‬A‫ر‬o ‫ء ا و‬, (! (P4 j‫ ر‬CA ‫ت‬0‫( إذا و‬NVL)  0 ‫دا‬.. <
.. ‫" وف‬
select last_name,
nvl (to_char(commission_pct),'NO COMMISSION')"COMM"
from employees

12.Create a query that displays the first eight characters of the employees’ last names and indicates the
amounts of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
‫ ا@? !^ف‬C)‫ وف ! ا‬8 ‫ <وز اول‬D' ‫!  ى‬: 12 ‫ اال‬
"‫ن ا‬8< ‫ف‬KK‫ <د ا‬3D:! ‫ آ !@? و'ل‬34 ! , ‫ف‬K>‫ <د ا‬C(‫ا‬
D j ! 2 /‫ه‬990  ' ‫ن‬- j k‫  ا‬,:. D ?‫ !ن آ ا‬i"
! ,:. 1
A ‫\ أزاى‬-. ! ‫ ً   اد‬.-+.., ‫زم‬E! 3D‫ ه‬M! ,:.

' ‫ ح‬8 ‫م‬D:‫ع ا‬g! ‫ن‬8< Rpad N(‫م دا ا‬:' C ((‫ا‬
‫ 'وى <د ا" وف‬:‫ ه‬n Rpad(col,n,'*') ‫ ا‬,*-' , ‫م ا‬D:‫<د ا‬
‫م هن <د‬D:‫ ال <د ا‬3+ '*' r! !E< 2! (- ‫ <وز‬, ‫ا‬
‫ف‬K>‫ <د ا‬3D ‫ن‬8. 3 *‫آ ش ا‬6! :‫ ا@? وه‬34  , ‫ف ا‬K‫أ‬
.:‫ ا@? ه‬C)‫  وف ! ا‬8 ‫ أول‬3D ‫ن‬8<‫ ار و‬3:D ‫ن‬8< Trunc ‫وم دا‬1000 , < C*:‫ه‬
.Substr ‫اا‬

select rpad(substr(last_name,1,8),trunc(salary/1000,0),'*')
EMPLOYEES_AND_THEIR_SALARIES
FROM employees‫؛‬
13. Using the DECODE function, write a query that displays the
grade of all employees based on the value of the column JOB_ID,
using the following data: Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0

14. Rewrite the statement in the preceding exercise using the CASE syntax.

‫ وه < اا وه <وز‬%.! :‫ ً أ‬.-+‫ و‬decode<case ‫ <وز م اا‬:‫ ه‬:13،14 ‫ اال‬
. ‫ا‬6‫ وه‬,) N ‫ ا‬2-4 ,*- `! -! Q8'  Er ?@‫ ا‬-4‫ ر‬C(' 3D

13-Select job_id,
decode (job_id,'AD_PRES','A','ST_MAN','B','IT_PROG','D','ST_CLERK','E',0) “GRA”
From employees;

14-Select job_id,
CASE job_id
when 'AD_PRES' then 'A' -: PE!
when 'ST_MAN' then 'B' , 1!‫ آ‬0 K-1
‫م‬P &  , < % -<‫ا
ا‬-2
when 'IT_PROG' then 'D'
1.g‫' ! و‬E ,  ,:. ‫ت‬N ‫ا‬
when 'ST_CLERK'then'E' ‘0’ '
else ‘0’ END “GRA”
From employees;

You might also like