ORACLE Fundamental - SQL 1 آاروأ أ: Moh&Ali
ORACLE Fundamental - SQL 1 آاروأ أ: Moh&Ali
ORACLE Fundamental - SQL 1 آاروأ أ: Moh&Ali
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;
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
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-44 و.(ر ا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.
from employees ;
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 اال
from employees
!'ش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;