Sie sind auf Seite 1von 4

Course Code Type Course Code Here

Database Management System


Description
1
College / Department:
LabExer No. 007
Online Education
Laboratory Exercise Page 1 of 1
Direction:
 Use the Employees table (copy and paste the code in SQL command line)
 Based on the given table: Employees table as shown below, write the PL/SQL that applies single row
function in order to get the printed output per number. Write your answer in a short bond paper.
1. Create a query that display the MINIMUM salary of employees per job_id. Note job_id shoulb
be in lowercase.
Possible Output:
LOWEST SALARY JOB_ID
------------- ----------
4200 it_prog
12000 ac_mgr
.
SQL> select job_id, min(salary) from employees group by job_id;

JOB_ID MIN(SALARY)
---------- -----------
sa_rep 7000
mk_rep 6000
ad_pres 24000
ad_vp 17000
it_prog 4200
st_man 5800
ac_account 8300
sa_man 10500
ad_asst 4400
ac_mgr 12000
st_clerk 2500
mk_man 13000
.
12 rows selected
2. Create a query that counts the number of ST_CLERK job_id. Rename the column as “Total no.
of ST_CLERK”.
Possible Output:
Total no. of ST_CLERK
---------------------
4
 SQL> select count(job_id) as "Total no. of ST CLERK" from
employees where job_id='st_clerk';

 Total no. of ST CLERK


---------------------
4
3. Create a report that display distinct job_id and the the total number per distinct (unique) job_id.
Possible Output:
JOB_ID Total no. of Job_id
---------- -------------------
IT_PROG 3
AC_MGR 1

 . SQL> SELECT JOB_ID, COUNT(*) AS "Total no, of Job_id" FROM


EMPLOYEES group by job_id;

JOB_ID Total no, of Job_id


---------- -------------------
sa_rep 3
mk_rep 1
ad_pres 1
ad_vp 2
it_prog 3
st_man 1
ac_account 1
sa_man 1
ad_asst 1
ac_mgr 1
st_clerk 4

JOB_ID Total no, of Job_id


---------- -------------------
mk_man 1
.
12 rows selected
4. Create a query that display the Job_id and add the ff: function:
 Count the total number of job_id per distinct values; Compute for the summary of salary
per job_id; and Compute for the average salary per job_id
Possible Output:
JOB_ID No. of Job_id Total Salary Average Salary
---------- ------------- ------------ --------------
IT_PROG 3 10200 5100
AC_MGR 1 12000 12000
AC_ACCOUNT 1 8300 8300

 SQL> SELECT JOB_ID, COUNT(job_id) AS "Total no, of Job_id",


sum(salary), avg(salary) FROM EMPLOYEES where job_id
in('it_prog','ac_mgr','ac_account')group by job_id;

 JOB_ID Total no, of Job_id SUM(SALARY) AVG(SALARY)


---------- ------------------- ----------- -----------
it_prog 3 10200
5100
ac_account 1 8300
8300
ac_mgr 1 12000
12000
5. Create a query that displays the lowest salary rename as “LOWEST SALARY”, maximum
salary rename as “HIGHEST SALARY” and department_id concatenated to job_id.
Possible Output:
LOWEST SALARY HIGHEST SALARY Record
------------- -------------- ----------------------------
4200 6000 60 With Job_Id Of It_Prog
5800 5800 50 With Job_Id Of St_Man
.
.
14 rows selected
6. Create a report that display the smallest (minimum) Surname.. and apply the ff. functions:
 Get the average salary; Group the data per job_id; Get only job_id with a keyword
“REP”; and Apply having clause, which the max salary per job_id is greater than 5000.
 SQL> select lower(min(lastname)), avg(salary) from employees where job_id like '%rep' group
by job_id having max(salary) >5000;

 LOWER(MIN( AVG(SALARY)
---------- -----------
abel 8866.66667
fay 6000

7. Create a query that display the Minimum firstname concatenated to lastname. Look for the
maximum salary per department_id. Note that pad function is applied for the function
maximum.
Possible Output:
Complete Name Maximum Salary DEPARTMENT_ID
--------------------- -------------------------------- -------------
KIMBERLEY GRANT $$$$7000
PAT FAY $$$$6000 20
 . SQL> select department_id, max(salary) from employees group by
department_id;

 DEPARTMENT_ID MAX(SALARY)
------------- -----------
7000
90 24000
20 13000
110 12000
50 11000
80 10500
60 6000
10 4400
.
7 rows selected
8. Create a query that display the maximum lastname concatenated to firstname and rename the
column as “Employees Name”, Job_id and apply the ff. functions:

 Count for the distinct Job_id; Apply where condition whose lastname ends with letter ‘N’;
Group the job_id; and Apply having clause of employees having average salary that is
greater that 10000.
Possible Output
Employees Name JOB_ID COUNT(JOB_ID) JOB_ID
--------------------- ---------- ------------- ----------
De Haan,Lex AD_VP 1 AD_VP
Hartstein,Michael MK_MAN 1 MK_MAN

 SQL> select lastname||','||firstname as "employees name", job_id,


count(job_id), from employees where lastname like '%n' group by
job_id having avg(salary) > 1000;

9. What is the main goal of using GROUP BY clause?


The main goal of GROUP BY clause will gather all of the rows together that contain data in the
specified column(s) and will allow aggregate functions to be performed on the one or more
columns.

10. What is the difference between where condition and HAVING clause?

The difference between WHERE clause is used is filter records from a result. The filter occurs
before any groupings are made while HAVING clause is used to filter values from a group.

Das könnte Ihnen auch gefallen