Professional Documents
Culture Documents
2010
Using a correlated sub-query determine the names of employees who earn
more than the average salary for their department. Show the SQL code. (sample
tables)
Select e.ename, e.sal, d.dname from emp e, dept d where e.sal > (select
avg(sal) from emp where deptno=e.deptno) and e.deptno = d.deptno;
Write the SQL code to display the first two letters of all employee names.
(sample tables)
Select substr(ename,1,2) from emp;
Write the SQL code to show all employee names and the name of the
department they work in. (sample tables)
select e.ename, d.dname from emp e, dept d where e.deptno = d.deptno;
Describe the purpose of an index in a database. In what situation would you not
use an index?
See Question 6 Spring 2008
Write the SQL code to ask for a letter and display all employee names that start
with that letter. The prompt should say “letter please”. (sample tables)
Select ename from emp where ename like UPPER(‘&letterplease%’);
Write the SQL code to list the employees who are the highest paid for their job
type. (sample tables)
Select e.ename, e.sal, d.dname, e.job, from emp e, dept d where sal =
(select max(sal) from emp where deptno = d.deptno);
Write the SQL code to list all employees whose name starts with an „S‟. Show
two ways of doing this. (sample tables)
1 Select ename from emp where ename like ‘S%’;
Write the SQL to list the average salary for each job type. Do not display the
average if it is less than 2000 and do not include Managers in your calculation.
(sample tables)
Select job, avg(sal) from emp where job != ‘MANAGER’ group by job
having avg(sal) > 2000;
2008 – AUTUMN
1 Write the SQL to display the length of all employee names
(sample tables).
SELECT ename, LENGTH(ename) FROM emp;
2 Write the SQL script that will ask the user for an employee
name. The script will then display the name of all employees
who work in the same department as the employee name
supplied (sample tables).
SELECT ename FROM emp e
WHERE e.deptno = (SELECT deptno FROM emp
WHERE UPPER(’&ename’) = ename);
4 Write the SQL code to ask for an employee name and display
the third letter of the name. (sample tables)
SELECT ename, substr(ename,3,1) FROM emp
WHERE ename = ‘&name’;
9 Write the SQL to display the number of employees for all job
types (sample tables).
SELECT job, count(ename)FROM emp
GROUP BY job;
10 Write the SQL script to determine how many days there are
between today and the end of the month.
SELECT DAYS_BETWEEN(sysdate,LAST_DAY(sysdate))
FROM sys.dual;
SELECT
ROUND(date(MONTHS_BETWEEN(sysdate,LAST_DAY(s
ysdate))),’DAY’) FROM sys.dual;
2008 - SPRING
1 Write the SQL script to determine how many years an employee
has worked in the company (sample tables)
SELECT ename , hiredate ,
round(months_between(sysdate,hiredate)/12)"YEARS IN
COMPANY"
FROM EMP;
2 Write the SQL script that will ask the user for an employee
name. The script will then display the salary of all employees
whose name sounds similar to the supplied name (sample
tables).
SELECT ename , sal
FROM emp
WHERE soundex(ename)= soundex('&ename');
4 Write the SQL code to ask for an employee name and display
the first two letters of the name. (sample tables)
SELECT substr(ename ,1 ,2)
FROM emp
WHERE ename = UPPER('&NAME');
6 Describe the situations where you would and would not use an
index?
You would use an index if the data is unique, e.g you would
use an index if you were looking up PPS numbers
You would not use an index if the data is not unique e.g
Select ename , where email like %yahoo.com , as many users
would have yahoo addresses and it would look up every row
regardless of index
7 Write the SQL script to ask the user for a job type and a salary.
The script will output all employees with that job type who earn
more then the supplied salary (sample tables).
SELECT ename , job , sal
FROM emp
WHERE job = UPPER('&JOB') AND SAL > &SALARY;
9 Write the SQL to list all employees whose name begins with
an ‘S’. Show two ways of doing this (sample tables).
SELECT ename
FROM emp
WHERE ename LIKE 'S%';
1 Write the SQL code to list all managers who do not earn a
commission. (sample tables)
SELECT ename , job
FROM emp
WHERE job = 'MANAGER' AND NVL(COMM,0) =0;
2 Describe the difference between an equi-join and a non-equi
join.
A join is linking two tables together using fields common to
both. An equi-join uses a comparison operator (i.e. =). There
can be inner, outer, or self equi joins. e,g
INNER: SELECT ename, dname
FROM emp e, dept d
WHERE e.deptno=d.deptno;
6 Write the SQL to list the average salary for each job type. Do
not display the average if it is less than 2000. (sample tables)
SELECT job, sal FROM emp e
WHERE sal = (SELECT avg(e.sal) FROM emp)
AND sal > 2000
ORDER BY job;
SELECT job,avg(sal) FROM emp
GROUP BY job
HAVING avg(sal) > 2000;
10Write the SQL to list all employees and the name of the
department they work in. (sample tables)
SELECT e.ename, d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno;
2006 - AUTUMN
1 What does the following command do ?
Explain plan for
select * from tab1;
This explains the execution plan of an SQL query. The
results are saved on a plan table. Explain plan is used to aid
performance, alongside with the SQL Trace function.
Explain plan defines how Oracle finds or writes the data.
2 Write the SQL code to display the first two letters of all
employee names.
SELECT ename, substr(ename,1,2) FROM emp;
3 Write the SQL code to list all Managers who earn a
commission.
SELECT ename, comm FROM emp
WHERE job = ‘MANAGER’
AND nvl(comm,0) !=0 ;
6 Write the SQL code to list the employees who are the highest
paid for their job type.
SELECT ename, job
FROM emp
WHERE sal =( SELECT MAX(sal) FROM emp
GROUP BY job)
GROUP BY job;
HAVING MAX(sal)=(SELECT MAX(sal) FROM emp
GROUP BY job);
2 Write the SQL code to ask for an employee name and display
the first two letters of the name. (sample tables)
Select substr(‘&ename’,1,2)
From emp;