Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1) Find the names, street address, and cities of residence for all employees who work for 'First

Bank
Corporation' and earn more than $10,000.

Select employee-name, street, city

From Employee, Works

Where company-name = ‘First Bank Corporation’

AND salary >10,000

2) Find the names of all employees in the database who live in the same cities as the companies for
which they work

Select e.employee-name

From Employee e, Company c, Works w

Where e.employee-name = w.Employee-name,

e.city = c.city , w.company-name = c.company-name.

//////3) Assume that the companies may be located in several cities. Find all companies located in every
city in which 'Small Bank Corporation' is located.

Select company-name

From Employee e, Company c

Where (SELECT city

FROM Company c

WHERE company-name = “Small Bank Corporation”

/////Q4: Find the names of all employees who earn more than the average salary of all employees of
their company. Assume that all people work for at most one company 39: Find the name of the
company that has the smallest payroll

Select employee-name, AVG(salary) AS “AVG_SAL”

From Employee e, Works w

Where salary > AVG_SAL


////Q5: Find the names of all employees in the database who live in the same cities and on the same
streets as do their managers.

Select e.employee-name

From Employee e, Manages m

Where e.employee-name = m.manager-names

AND e.city = m.city

AND e.street = m.street

EMPLOYEE (NO, NAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

DEPARTMENT (DeptNO, DeptName, LOC)

SALGRADE (GRADE, LOSAL, HISAL)

30) Give name, salary and LOC of all non-commissioned employees.

Select e.ename, e.sal, d.loc

From employee e, department d

Where e.deptno = d.deptno

AND e.comm IS NULL

29) Give names of all those employees who work in the city “London”, take no commission under
manager 3323 and hire before 2002.

Select e.ename

From employee e, department d

Where e.deptno = d.deptno

AND d.loc = ‘London’

AND e.comm IS NULL

AND e.mgr = 3323

AND hiredate < 2002


28) Give names and department names of those employees whose one third salary is greater than 6000.

Select e.enames, d,deptname

From employees e, department d

Where (1/3 * sal) > 6000

27)Give all data of employees whose salary is less than average salary and department id is 30.

Select e.NO, e.NAME, e.JOB, e.MGR, e.HIREDATE, e.SAL, e.COMM, e.DEPTNO

From employees e, department d

Where e.deptno = d.deptno

AND sal < AVG(sal)

AND d.deptno = 30

26) Give employee number, name, hire date, commission, department id, department name, location,
salary grade of all employees those names are Hassan, salary is less than 50000, commission is 2% of the
half of their salary

Select e.no, e.name, e.hiredate, e.comm, d.deptno, d.deptname, d.loc, s.grade

From employee e, department d, salgrade s

Where e.deptno = d.deptno

AND e.ename = ‘Hassan’

AND sal < 50000

AND comm = 0.02* (1/2)*sal


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

DEPT (DEPTNO, DNAME, LOC)

SALGRADE (GRADE, LOSAL, HISAL)

25) Show all the employee names as EMP_name of length 3 or 4.

Select ename AS “EMP_name”

From emp

Where LENGTH(ename) = 3

OR LENGTH(ename) = 4

24) Give the employee numbers in ascending order as “Ascending” and in descending order as
“Descending”.

Select e.empno AS “Ascending” , t.empno AS “Descending”

From emp e, emp t

GROUP BY e.empno, t.empno DESC

23) Give employee name, hire date, department name and commission of all employees in the format,
e.g “Hanan was hired on 1-12-2001 in department Accounts with commission 2.5%” as Emp Detail, of
those commission is less than 30% of the one third of their salary.

Select ename || ‘was hired on ’ hiredate || ‘in department ’ dname || ‘with’ comm) AS “Emp Detail”

From emp

Where comm < (0.30*(1/3)*sal)

22) Show all employee names, starting with ‘A’ and second last letter ‘M’

Select ename

FROM emp

Where ename LIKE ‘A%’

AND ename LIKE ‘%M_’


21) Give name, manager and salary grade of employees those salary is 1/4 times of their commission.

Select e.ename, e.mgr, s.grade

FROM emp e, salgrade s

Where e.sal = ((1/4)*comm)

20) Give the employee number and hiredate and maxsal of employees those salgrade is greater than 19
and all employees that manger is Adeel.

Select e.empno, e.hiredate, e.MAX(sal)

From emp e, salgrade s

Where s.grade>19

AND e.job = ‘manager’

AND e.ename = ‘Adeel’

19) Give salary grade of only those employees whose Manager is 7902 or department id is 30 and
location is Boston or department name is “Operations”.

Select s.grade, s.losal, s.hisal

From salgrade s,emp e, dept d

Where e.deptno = d.deptno

AND (e.mgr = 7902

OR d.deptno =30)

AND (d.loc = ‘Boston’

OR d.dname = ‘Operations’)

18) Give salary of all those employees whose name is John and their location is Sydney and also of those
whose name is Smith and their location is not California.

SELECT e.sal

FROM emp e, dept d

WHERE e.deptno = d.deptno

AND e.ename =’John’


AND d.loc = ‘Sydney’

AND ( SELECT p.sal

FROM emp p, dept t

WHERE p.deptno = t.deptno

AND p.ename =’Smith’

AND t.loc = ‘California’)

17) Determine the number of managers without listing them. Label the column Number of Managers.

Select COUNT (DISTINCT mgr) AS “Number of Managers”

FROM emp

16) Write a query that displays the employee numbers and last names of all employees who work in a
department with any employee whose last name contains a k, and sorted the record on the basis of
employee name

Select e.empno, e.ename

FROM emp e, dept d

Where e.deptno = d.deptno

AND ename LIKE “%k”

ORDER BY e.ename

15) Display the employee numbers, last names, and salaries of all employees who earn more than the
average salary and who work in a department with any employee with a u in their name.

Select empno, ename, sal

From emp

Where sal> AVG(sal)

AND deptno IN (SELECT deptno


FROM employees
WHERE ename LIKE '%u%');
14) Display the manager number and the salary of the lowest paid employee for that manager. Exclude
anyone whose manager is not known. Exclude any groups where maximum salary is less than $15,000.
Sort the output in descending order of minimum salary.

Select mgr, MIN(sal)

FROM emp

Where mgr IS NOT NULL

NOT MAX(sal) < 15,000

GROUP BY mgr

ORDER BY MIN(sal) DESC

13) Write a query to display each department’s name, location, number of employees, and the average
salary for all employees in that department. Label the columns Name, Location, Number of People, and
Salary, respectively. Round the average salary to two decimal places.

Select d.dname AS “Name”, d.loc AS “Location”, COUNT(d.dname) AS “Number of People”,


ROUND(AVG(e.sal) , 2) AS “Salary”

FROM emp e , dept d

Where e.deptno = d.deptno

12) Create a query to display the name and hire date of any employee hired after employee SCOTT.

Select emp.ename, emp.hiredate

FROM emp e, emp scott

Where scott.ename = ‘SCOTT’

AND emp.hiredate> scott.hiredate

11) Write a query to display the employee name, hire date, number of months employed, first Friday
after hiredate and last day of the month when hired, for all employees employed for fewer than 36
months.

Select ename, hiredate, DATEDIFF(MONTH, ’sysdate’ , ’hiredate’) , NEXT_DAY (hiredate ,


'FRIDAY') ,LAST_DAY(hiredate)

FROM emp

Where DATEDIFF(MONTH, ’sysdate’ , ’hiredate’) <36


10) Write a query that displays the employee names, job and hire date, also display a column with the
name ‘New Hire date’ in which increase 6 months in the hire date of the Salesman,2 months in the hire
date of Clerk ,1 month in the hired date of President. (Format should be 31/06/ 2000.)

SELECT ename, job, hiredate

FROM emp

ADD_MONTH( hiredate,6 )AS “NEW HIRE DATE”,  Saleman

ADD_MONTH( hiredate,2 )AS “NEW HIRE DATE”,  clerk

ADD_MONTH( hiredate,1 )AS “NEW HIRE DATE”,  President

Format (fnDD MONTH YYYY)

9) Write a query that produces the following for each employee: earns monthly but wants. Label the
column Dream Salaries whose salary ranges in between 1100 to 2900.

SELECT sal AS “Dream Salaries”

FROM emp

WHERE sal BETWEEN 1100 AND 2900

8) For each employee, display the employee number, ename, salary, and salary increased by 15% as
New Salary add a column as Increment that subtracts the old salary from the new salary and expressed
as a whole number.

SELECT empno, ename, sal, (sal *0.15) AS “NEW SALARY”, TRUNC(NEW SALARY - sal) AS “INCREMENT”

FROM emp

7) Write a query that displays the employee’s names with the first letter capitalized and all other letters
lowercase and the length of the name for all employees whose name starts with M, T, or K. Sort the
results by the employees’ names.

SELECT INITCAP(ename) AS “NAME” , LENGTH(ename) AS “LENGTH”


FROM EMP
WHERE ename LIKE ‘%M’
OR ename LIKE ‘%T’
OR ename LIKE ‘%K’
ORDER BY ename
6) Display the ename and department number of all employees in departments 20 and 30 in
alphabetical order by ename, and length of name greater than 4.

Select e.ename, d.deptno

FROM EMP e, DEPT d

Where (d.deptno = 20

AND d.deptno = 30)

AND LENGTH(e.ename > 4)

ORDER BY e.ename

You might also like