Вы находитесь на странице: 1из 11

Lab Work

Database Systems
THE SAMPLE TABLES
EMP

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SALMAN CLERK 7902 17-DEC-2000 8000 20


7499 ALIA SALESMAN 7698 20-FEB-2001 16000 3000 30

7521 WAHEED SALESMAN 7698 22-FEB-2001 12500 5000 30

7566 JAMIL MANAGER 7839 02-APR-2001 29750 20

7654 MEHVISH SALESMAN 7698 28-SEP-2001 12000 12500 30

7698 BILAL MANAGER 7839 01-MAY-2001 28500 30

7782 QASIM MANAGER 7839 09-JUN-2001 24500 10

7788 SHAMS ANALYST 7566 09-DEC-2002 30000 20

7839 KAMRAN PRESIDENT 17-NOV-2001 50000

7844 TANVEER SALESMAN 7698 08-SEP-2001 15000 0 30


7876 AFTAB CLERK 7788 12-JAN-2003 11000 20

7900 JAVERIA CLERK 7698 03-DEC-2001 9500 30

7902 FOZIA ANALYST 7566 03-DEC-2001 30000 20


7934 MURAD CLERK 7782 23-JAN-2002 13000 10

DEPT
DEPTNO DNAME LOC

10 ACCOUNTING ISLAMABAD
20 RESEARCH KARACHI
30 SALES LAHORE
40 OPERATIONS QUETTA

SALGRADE

GRADE LOSAL HISAL

1 7000 12000
2 12010 14000
3 14010 20000
4 20010 30000
5 30010 99990

The SELECT statement


Spe rows of
cific result
atio table
n of
desi
red
colu
mns
Spe
cific
atio
n of
tabl
e or
tabl
es
Sele
ction
crite
ria
for
rows
For
mati
on
of
grou
ps
with
ident
ical
valu
es in
spec
ified
colu
mn
Sele
ction
of
spec
ific
grou
ps
Sorti
ng
seq
uen
ce
of
EXERCISES

A1 Display the contents of the DEPT, EMP and SALGRADE tables.


[Check your results against the tables listed.]

Select * from Emp


To produce an answer to the request "Give a list of all employees showing their employee
number, name & department number." We will require the following SQL statement,

Select EmpNo, Ename, DeptNo from Emp

A2 Display the name and commission of all the employees.

A3 Display the name and commission of all the employees together with another column that
shows their commission increased by 10%.

A4 Display the job title of all the employees.

To remove duplicates within the result table, use:

SELECT DISTINCT column 1, column2, etc FROM table-name;

A5 Display all the different job titles which currently exist in the company.

SELECT from a single table using the WHERE condition

WHERE =, <>, >, >=, <, <=, (! = means not equal) comparison operators
NOT, AND, OR logical operators
column BETWEEN field value AND field value
Column IN (field value, field value, .......)
Column LIKE '%char string%'
(% means skip 0 or any no of characters; use _ for exactly one character)
Column IS NULL

WHERE
WHERE
WHERE

WHERE
(All these operators can be negated by using NOT)

A6 Display the employee number, name and current job of all those who work in Department 30.

A7 Display the names of all the clerks, showing their employee number and that of their manager.

A8 Display details of all clerks, analysts and salesmen.

A9 Display details of employees who are not clerks, analysts or salesmen.

A10 Display details of all employees whose commission is greater than salary.

A11 Display employee name, job and department number for employees whose names begin with ‘M’.

A12 Display details of employees whose salaries are not between 12,000 and 14,000.

A13 Display details of salesmen and managers in dept 30, whose salary is greater than or equal to
15,000.
(Note: with logical operators - AND precedes OR)
Using the ORDER BY statement to control the display order of selected records
ORDER BY column1, column2, etc ASC/DESC; or
ORDER BY n; (being the nth column)

Note: The default ordering is ascending.


Null values are always displayed first regardless of sort sequence.

A14 Display the employee number, current job and salary of all those who work in Department 30,
with the output in ascending salary order.

A15 Display the employee name and current job of all those who work in Department 30, with the
output in descending salary order.

A16 Display the employee name and current job of all those who work in Department 30, with the
output in descending salary order within each job.

A17 Display employee details for departments 10 and 30, in name order, within each department

SIMPLE FUNCTIONS.

SQL provides some simple aggregating functions that enable us to derive information about the rows in
a table.
COUNT(*) returns a single value, the number of rows in the table
MAX(attribute) returns the largest value for the attribute
MIN(attribute) returns the smallest value for the attribute
AVG(attribute) works out the average value for this attribute
SUM(attribute) works out the total value for this attribute
For all the following exercises, you should check your results carefully against the provided
table listings.

A18 What are the lowest and highest basic salaries within the company?

A19 How many people have a salary greater than 20,000?

NULL Values

If a column has no value for any particular row, it is referred to as a NULL value. This is not the same
as a zero in a numeric column, or a space in a character column.

In MS-SQL Server, the IsNull function can be used to convert a null into a specified value e.g.
IsNull(COMM, 0) will return the actual value of COMM if it has a value, or a zero if COMM is null. This is
often necessary when performing calculations, or formatting for output since null values will be totally
ignored by many functions and operations.

¾ Oracle has a function called NVL which allows you do a similar thing. It actually returns the first non-
null value in a list, but used as below enables a null to be treated as a specific number:

Select ename NVL(comm,0) from emp;

A column can be tested using IS NULL or IS NOT NULL e.g.

SELECT ...FROM... WHERE column_name IS NOT NULL;

A20 What are the highest and lowest incomes (i.e. to include commission) in the Sales Department?
G RO UP I NG DATA

The GROUP BY clause splits the table into specified groups, returning one summary row for each group
which is then used in the SELECT clause.

In each of the following queries, you may treat the President as not an employee (as he isn’t given a
deptno). However, here’s a question for the sharper ones: how your queries change if you did need to
include him as an employee?

A21 How many people are there in each department?

A22 How many people are there in each type of job in each department?

A23 For each department, find the average salary and the total salary bill excluding commission.

A24 Find the maximum commission earned, and the number of people in each department.

THE HAV I NG CLAUS E .

The HAVING clause is used to specify which GROUPS are to be displayed.

WHERE restricts which rows a SELECT works on and HAVING restricts which groups.

A25 Display the department number and number of employees in departments with fewer than 6
employees.

A26

a) Write statement to create table with these fields.


b) write statements to insert the following data into the table just created

EMPLOYEE

EMPID EMPNAM JOB HIREDATE SAL COMM DEPTNO


E
7369 Saleem CLERK 17-DEC-2000 6000 20
7499 Ali Nawaz SALESPERSON 20-FEB-2001 8000 2000 30
7521 Warda SALESPERSON 22-FEB-2001 9500 3500 30
7566 Jamil MANAGER 02-APR-2001 35000 20
7654 Maria SALESPERSON 28-SEP-2001 8700 2500 30

A27 Write statements to


a) Show all the records from table EMPLOYEE
b) Show all the employees who’s job is ‘CLERK’
c) Display the no of employees who are earning commission more then 1000.
d) Display name and HIREDATE of the employees who’s name starts with ‘a’ and has ‘e’ as
second last character.
e) Display Gross Salary of each employee along with employee’s name (hint: gross salary = sal
+ comm).
f) Display all the different job titles, which currently exist in the company.
g) Display the department and number of employees in departments with fewer than 6 employees.
h) How many people are there in each type of job in each department?
i) Find the employee name and its salary who is earning maximum salary in department 20
j) Display the no of employees in each department
A28 Write statements to
a) Delete all the records from EMPLOYEE table.
b) Delete all employees having NULL commission.
c) Delete all employees whose salary is not between 9000 and 20000
d) Delete the employees those were employed before 01-jan-2001
e) Delete employees working in department 20.

A29 Write statements to


a) Change the department no. to 30 where job is not ‘CLERK’ or ‘SALESMAN’.
b) Add 1000 to the salary of those employees earning commission greater than 2500.
c) Change all salesmen to Manager, those getting commission greater than salary.

A30
a) Delete all records of Employee
b) Destroy table EMPLOYEEs

Вам также может понравиться