Assignment 2 (SQL)
Assignment 2 (SQL)
Assignment-2
SQL
Solution
1. Select * from SALGRADE;
2. Select * from EMP;
3. Select * from emp where sal between 1000 and 2000;
4. Select deptno,dname from dept order by dname;
5. Select distinct job from emp;
6. Select * from emp where deptno in(10,20) order by ename;
7. Select ename,job from emp where lower(job)=’clerk’ and
deptno=20;
8. Select ename from emp where ename like (‘%TH%’) or ename
like (‘%LL%’);
9. Select * from emp where lower(job)=’manager’;
10. Select ename,comm from emp;
11. Select * from emp where hiredate = ’12-JAN-83’;
12. Select ename,sal*12 as annsal ,comm from emp where
lower(job)=’salesman’ and sal>comm order by sal desc;
13. Select * from emp order by mgr;
Problem# 2 (Using Functions)
This exercise covers functions not just in the SELECT but in WHERE and ORDER BY clauses.
Note the column alias which have been used.
2.1 - List the employee name and salary increased by 15% and expressed as a whole number
of dollars.
2.2 - Display each employee’s name and hiredate from dept. 20.
2.3 - Display each employee name with hiredate, and salary review date. Assume review date
is one year after hiredate. Order the output in ascending review date order.
2.4 - Print a list of employees displaying just the salary amount if more than 1500. If exactly
1500 display ‘On Target’, if less than 1500 display ‘Below 1500’.
2.5 - Write a query which will return the DAY of the week (i.e. MONDAY,) for any date entered
in the format: DD.MM.YY.
2.6 - Employees hired on or before the 15th of any month are paid on the last Friday of that
month. Those hired after the 15th are paid on the last Friday of the following month. Print a
list of employees, their hiredate and first pay date. Sort on hiredate.
2.7 - Write a query to calculate the length of time any employee has been with the company.
Solution
1. Select ename,sal*0.15 as “whole number of dollors” from
emp;
2. Select ename,hiredate from emp where deptno=20;
3. Select ename,hiredate,hiredate+365 as “salary review date”
from emp order by “salary review date”;
4. Select ename,sal,CASE WHEN(sal<1500) THEN ‘Below’
WHEN(sal=1500) THEN ‘On Target’ ELSE ‘’ END RESULT
from emp;
5. Select to_char(hiredate,’DAY,DD-MON-YYYY’) from emp;
6.
7. Select ename, trunc(months_between(sysdate,hiredate)/12)
as “Employee Job Years” from emp;
Solution
Problem# 4 (Joins)
4.1 - Display all employee names and their department name, in department name order.
4.2 - Display all employee names, department number and name.
4.3 - Display the name, location and department of employees whose salary is more than 1500
a month.
4.4 - Produce a list showing employees’ salary grades.
4.5 - Show only employees on grade 3.
4.6 - Show all employees in Dallas.
4.7 - List the employee name, job salary, grade and department name for everyone in the
company except clerks. Sort on salary, displaying the highest salary first.
4.8 - Display the department that has no employees.
4.9 - List all employees by name and number along with their manager’s name and number.
4.10 - Modify solution to question 10 to display KING who has no manager.
4.11 - Find the job that was filled in the first half of 1983, and the same job that was filled
during the same period in 1984.
4.12 - Find all employees who joined the company before their manager.
Solution
1. Select ename,dname from emp,dept where
emp.deptno=dept.deptno;
2. Select ename,emp.deptno,dname from emp,dept where
emp.deptno=dept.deptno;
3. Select ename,loc,dname from emp,dept where
emp.deptno=dept.deptno and sal>1500;
4. Select ename,sal,grade from emp,salgrade where sal
between losal and hisal;
5. Select ename,sal,grade from emp,salgrade where grade=3
and sal between losal and hisal;
6. Select ename,loc from emp,dept where
emp.deptno=dept.deptno and loc=’DALLAS’;
7. Select ename,job,sal,grade,dname from emp,dept,salgrade
where emp.deptno=dept.deptno and sal between losal and
hisal order by sal desc;
8. Select dname,ename from emp,dept where
emp.deptno(+)=dept.deptno;
9. Select worker.ename,worker.empno,manager.ename as
ManagerName,manager.empno as ManagerNo from emp
worker,emp manager where worker.mgr=manager.empno;
10. Select worker.ename,worker.empno,manager.ename
as ManagerName,manager.empno as ManagerNo from emp
worker,emp manager where
worker.mgr=manager.empno(+);
Solution