You are on page 1of 8

DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


Database Management Systems
Lab Experiments

Consider following tables to write SQL queries.


EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO )
DEPT (DEPTNO, DNAME, LOC)

Lab 6: To use Single row functions in SQL queries.


1. Display the employee's name in upper case letters and their salary rounded to two
decimal places.

Query:

select UPPER(ename), round(sal)


from emp;

Output:

2. Display the employee name and their annual salary including commission amount. If
the commission is null replace it with 0.
Query:

SELECT
ename,
(sal + NVL(comm, 0))*12 AS annual_salary
FROM
emp;

Output:

3. Display Employee’s name with the first letter capitalized and all other letters lowercase
and the length of their name, for all employees whose name starts with J, A, or M. Give
each column appropriate names.

Query:

SELECT
INITCAP(ename) AS formatted_name,
LENGTH(ename) AS length_of_name
FROM
emp
WHERE
ename LIKE 'J%' OR ename LIKE 'M%' OR ename LIKE 'A%';
Output:

4. Display the name, hiredate, and day of the week on which the employee started. Label
the column First Day.
Query:
SELECT
ename,
hiredate,
TO_CHAR(hiredate, 'Day') AS "First Day"
FROM
emp;

Output:
5. For each employee, display the employee name and calculate the number of months
between today and the date the employee was hired. Label the column
MONTHS_WORKED. Order the result by the number of months employed. Round the
number of months up to the closest whole number.

Query:

SELECT
ename, round((sysdate-hiredate)/30,0) as Months_Worked

FROM
emp order by Months_Worked

Output:

6. Display the Employee number, name, salary, and salary increase by 15% expressed as
a whole number. Label the column New Salary.
Query:

select empno, ename , sal, trunc(sal+0.15*sal) as NewSalary from emp;

Output:
7. Display the employee name, hiredate, 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 “Sunday, the seventh of September, 1981”.
Query:

SELECT
ename,
TO_CHAR(hiredate, 'FMDay, DDth "of" FMMonth, YYYY') AS "Hire Date",
TO_CHAR(NEXT_DAY(ADD_MONTHS(HIREDATE,6),'MONDAY'),'FMDay, DDth
"of" FMMonth, YYYY') as review
FROM
emp;
Output:
8. Create a query that will display the employee name and commission amount. If the
employee does not earn a commission, put “No Commission.” Label the column
COMM.
Query:
SELECT
ename,
NVL(TO_CHAR(comm), 'No Commission') AS "COMM"
FROM
emp;

Output:

9. Create a query that displays the employees’ names and indicates the amounts of their
salaries through asterisks. Each asterisk signifies a hundred dollars. Sort the data in
descending order of salary. Label the column
EMPLOYEE_AND_THEIR_SALARIES.

Query:

SELECT
ename,
RPAD('*', sal / 100, '*') AS "EMPLOYEE_AND_THEIR_SALARIES"
FROM
emp
ORDER BY
sal DESC;
Output:

10. Display the name, hiredate, and the 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.

Query:

SELECT

ename,

hiredate,

TO_CHAR(hiredate, 'Day') AS "DAY"

FROM

emp

ORDER BY

CASE

WHEN TO_CHAR(hiredate, 'D') = '1' THEN 8

ELSE TO_NUMBER(TO_CHAR(hiredate, 'D'))

END;
Output:

You might also like