Академический Документы
Профессиональный Документы
Культура Документы
Database Systems
THE SAMPLE TABLES
EMP
DEPT
DEPTNO DNAME LOC
10 ACCOUNTING ISLAMABAD
20 RESEARCH KARACHI
30 SALES LAHORE
40 OPERATIONS QUETTA
SALGRADE
1 7000 12000
2 12010 14000
3 14010 20000
4 20010 30000
5 30010 99990
A3 Display the name and commission of all the employees together with another column that
shows their commission increased by 10%.
A5 Display all the different job titles which currently exist in the company.
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.
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)
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?
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:
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?
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.
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
EMPLOYEE
A30
a) Delete all records of Employee
b) Destroy table EMPLOYEEs