You are on page 1of 23

IS3319 Previous Exam Questions SQL

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 show the date of next Tuesday.


Select next_day(sysdate, ‘TUESDAY’) from sys.dual;

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;

Describe what is wrong with the following code:

Create table 2table


(student_id number(4) primary key,
student_name char(10),
student_address varchar(100));
insert into 2table values
(12345, ‘john’, ‘cork’);
insert into 2table values (&&id, &&name,'&&address');
insert into mydata2 values (&id, &name, '&address');

See Question 7 Spring 2007

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);

3 What does the following command do?


Create table tab2 (suname char(10) NULL, age number(3),
address varchar(20) default ‘none supplied’, primary key
(surname));
Create a table called tab2. Create a field column called
suname with a length of 10, which allows for Null Values.
The second column number, uses numbers as a data type
and has a max length of 3. Address is the next column, which
allows for numbers and characters to a max of 20. If no
value is supplied, the walue of ‘none supplied’ is used. The
PK is always (surname) defined at the end.
ERROR!? Suname & surname, suname allowing nulls but
being a PK!?

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’;

5 Determine the names of employees who earn the smallest salary


in their department. Show the SQL code. (sample tables)
SELECT ename, deptno, sal FROM emp e
WHERE sal = (SELECT min(sal) FROM emp
WHERE deptno = e.deptno);

6 Describe the need for foreign keys.


A foreign key represents a key that is primary in another
table (for example in table dept, deptno is a primary key;
deptno is then a foreign key in the emp table). Foreign keys
inforce the rule of referential integrity, that link tables
together.

7 Explain the benefit of the soundex() function.


Using soundex can help when it comes to the user entering in
information. For example, if the user is entering an
employee name (eg. KING), but misspells it (KONG), SQL
will still return KING. Without soundex, SQL will return no
values. Syntax SELECT ename FROM emp WHERE
soundex(ename) = UPPER(soundex(‘&name’);

8 What is a check constraint used for? Give an SQL example of


its use.
A CHECK constraint is used to limit the values that can be
placed in a column. The check constraints are used to
enforce domain integrity, and are the most flexible. This
ensures the accuracy and reliability of the data in the
database.
CREATE TABLE money
(name char(10), sal number(7,2), constraint mycheckcon
check (sal > 4.8));

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');

3 What does the following command do?


Create table mytab (name char(10) NULL, age number(3)
default ‘none supplied’, address varchar(20),
primary key (name));
Creates a table called mytab
With column name – 10 characters, allows for null
characters
Age defined a number type 3 characters max, with default
value of ‘none supplied’
Address mixed characters (max 20), if there is unused
characters, they are not used.
Primary key is assigned to name, ALWAYS LAST THING
IN TABLE DEFINITION!!
MISTAKE!! NAME = PK BUT NULLS ALLOWED!!??

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');

5 Using a correlated sub-query, determine the names of employees


who earn less than the average salary for their department.
Show the SQL code. (sample tables)
SELECT ename, job, sal
FROM emp e
WHERE sal < (SELECT avg(sal) FROM emp
WHERE deptno =e.deptno)
ORDER BY deptno;

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;

8 What is the instr() function used for? Give an SQL example of


its use.
The instr() is used to find out what positon a
character/characters occur in a particular string-
Instr(‘Diploma’,’o’); would return number
5,Instr(‘Seventy’,’e’,3)
Would return 4 as we started at position 3

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%';

10 Write the SQL code to list the smallest salary


in each department (sample tables).
SELECT ename, sal, deptno
FROM emp
WHERE (deptno, sal) in (
SELECT deptno, min(sal)
FROM emp
GROUP BY deptno);
2007 - SPRING

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;

OUTER: SELECT ename, dname


FROM emp, dept
WHERE emp.deptno (+) =dept.deptno;
SELF: SELECT e.ename, m.ename
FROM emp e, emp m
WHERE e.mgr = m.empno;
A non equi-join joins two tables whose condition is
established using all comparison operators except the equal
(=) operator

SELECT e.ename, e.sal


FROM emp e, salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal;
3 Write the SQL code to ask for a job type and display the first
two letters of the job type. (sample tables)
SELECT substr(job,1,2)
FROM emp
WHERE job= UPPER(‘&job’);

4 Write the SQL command to determine the number of times the


letter ‘S’ appears in each employee name. (sample tables)
SELECT ename, length(ename) –
length(replace(ename,’S’,’’))
FROM emp;
5 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%’);

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;

7 Describe what is wrong with the following code:


Create table 123table
(student_id number(4) primary key,
student_name char(10),
student_address varchar(100));
insert into 123table values
(12345, ‘john’, ‘cork’);
insert into 123table values (&&id, &&name,'&&address');
insert into mydata2 values (&id, &name, '&address');
For starters, in the first insert into statement, the number
12345, is too big for the number of size 4. The table name is
invalid because it begins with a number. There is also
incompatible types, missing single quotes on strings.
Unknown table error, trying to insert into mydata2.
8 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 ename, sal FROM emp e
WHERE sal > (SELECT avg(sal)FROM emp
WHERE deptno = e.deptno);

9 Write the SQL script to determine how many years an employee


has worked in the company. (sample tables)
SELECT ename,
ROUND(MONTHS_BETWEEN(sysdate,hiredate)/12)
“Years in Company”
FROM emp;

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 ;

4 Determine the department (deptno) where the highest earner in


the company works? Write the SQL code to determine this.
(sample tables)
SELECT deptno, sal FROM emp e
WHERE e.sal = (SELECT max(sal) FROM emp);

5 Describe why you would use the nvl() function.


nvl() is used to replace null values in a table with specified
values. The function does not replace the value in the
database, but replaces it temporarily for presentation.
(Example, nvl(comm,0) will replace null comm values with
0.) This is useful to avoid encountering null values where
they might be expected (like commission).

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);

Select job, MAX(sal)


from emp
group by job;

7 Write the SQL code to show the date of next Tuesday.


SELECT NEXT_DAY(sysdate,’TUESDAY’) FROM
sys.dual;
8 Write the SQL code to list all employees whose name starts
with an ‘S’. (sample tables)
SELECT ename FROM emp
WHERE ename LIKE ‘S%’;
9 Give examples of a correlated sub-query, describing the
problem the code is addressing – i.e. what is the code doing?
Names of employees who earn more than the average salary
for their department.
SELECT ename, deptno, sal
FROM emp e
WHERE sal > (SELECT avg(sal)
FROM emp
WHERE deptno = e.deptno);
The problem here is that the salary in the inner select
statement will return the overall salary of each department
combined.

10 Describe the purpose of an index.


An index allows a database to run faster. Without an index,
the database system reads through the entire table (this
process is called a 'table scan') to locate the desired
information. With the proper index in place, the database
system can then first go through the index to find out where
to retrieve the data, and then go to these locations directly to
get the needed data. This is much faster.
2006 - SPRING
1 Write the SQL code to list all employees who earn the
minimum salary for their department - do not use a correlated
sub-query. (sample tables)
Select ename, sal, deptno
From emp
Where (deptno, sal) IN (
Select deptno, min(sal)
From emp
Group by deptno);

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;

3 Write the SQL code to list all Clerks in department 20 who do


not earn a commission. (sample tables)
Select ename, job, deptno, comm.
From emp

You might also like