Sunteți pe pagina 1din 32

DATA BASE MANAGEMENT SYSTEM

Lab Exercise 1
Exercise:
Answer the following questions:

Q1. What is data?


Stored representations of meaningful objects and events .
OR
Data is distinct pieces of information, usually formatted in a special way.
There are two types of data.

a. Structured Data: numbers, text, dates


b. Unstructured Data: images, video, documents

Q2. What do you mean by information?


Data processed to increase knowledge in the person using the data.

Q3. What is database management system? Why do we need a


DBMS?

Data Base Management System:


A software system that is used to create, maintain, and provide controlled
access to user databases.
OR
Database management systems are computer software applications that
interact with the user, other applications, and the database itself to capture
and analyze data.

Need OF A DBMS:
Reasons are as under:

Program-data independence
Planned data redundancy
Improved data consistency
Improved data sharing
Increased application development productivity
Enforcement of standards
Improved data quality
Improved data accessibility and responsiveness

Page 1
DATA BASE MANAGEMENT SYSTEM
Reduced program maintenance
Improved decision support

Q4. Discuss advantages and disadvantages of file oriented system.

Advantages:
The file-oriented system is supported by a conventional operating system.
Permanent records are stored in various files and a number of different
application programs are written to extract records from and add records to
the appropriate files.

Disadvantages:
Data redundancy and Inconsistency
Difficulty in accessing data
Data isolation
Concurrent access anomalies
Security problems
Integrity problems

Q5. What is SQL?


SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in relational database.

SQL is the standard language for Relation Database System. All relational
database management systems like MySQL, MS Access, Oracle, Sybase,
Informix, postgres and SQL Server use SQL as standard database language.

Q6. Why SQL?


Allows user to access data in RDBMS.
Allows user to describe the data.
Allows user to define the data in database and manipulate the data.
Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
Allows users to create and drop databases and tables.

Page 2
DATA BASE MANAGEMENT SYSTEM
Q7. TYPES OF SQL COMMANDS
Types of SQL commands are as under
DDL - Data Definition Language:

Command Description
CREATE Creates a new table, a view of a table, or other object in database
ALTER Modifies an existing database object, such as a table.
DROP Deletes an entire table, a view of a table or other object in the database.

DML - Data Manipulation Language:

Command Description
INSERT Creates a record

UPDATE Modifies records


DELETE Deletes records

DCL - Data Control Language:

Command Description

GRANT Gives a privilege to user


REVOKE Takes back privileges granted from user
DQL - Data Query Language:

Command Description
SELECT Retrieves certain records from one or more tables

Q8. Give names of famous DBMS (minimum five)


Names of famous DBMS are as under
1. MySQL
2. Microsoft SQL Server
3. Microsoft Office Access
4. Microsoft Visual FoxPro,
5. PostgreSQL
6. SQLite
7. Oracle

Page 3
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 2
Exercise:
Answer the following questions:

Q1. What is RDBMS?


RDBMS stands for Relational Database Management System. RDBMS is the
basis for SQL, and for all modern database systems like MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access
OR
RDBMS is a type of database management system (DBMS) that stores data in
the form of related tables. Relational databases are powerful because they
require few assumptions about how data is related or how it will be extracted
from the database. As a result, the same database can be viewed in many
different ways.

Q2. What is table?


The data in RDBMS is stored in database objects called tables. The table is a
collection of related data entries and it consists of columns and rows

A table is the most common and simplest form of data storage in a relational
database.

Q3. What is field?


Every table is broken up into smaller entities called fields. The fields in the
CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information
about every record in the table.

Q4. What is record or row?


A record, also called a row of data, is each individual entry that exists in a
table.

Q5. What is column?


A column is a vertical entity in a table that contains all information associated
with a specific field in a table.

Page 4
DATA BASE MANAGEMENT SYSTEM

Q6. WHAT ARE SQL Constraints:


Constraints are the rules enforced on data columns on table. These are used
to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the database.
Constraints could be column level or table level. Column level constraints are
applied only to one column, whereas table level constraints are applied to the
whole table.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none
is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database
table.
FOREIGN Key: Uniquely identified a rows/records in any another
database table.
CHECK Constraint: The CHECK constraint ensures that all values in a
column satisfy certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.

Page 5
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 3
Exercise:
1. Create the following tables:

Query
create table client_master
(client_no varchar2(6),name varchar2(20),address1 varchar2(30),
address2 varchar2(30),city varchar2(15),state varchar2(15),
pincode number(6),bal_due number(10,2));

Page 6
DATA BASE MANAGEMENT SYSTEM

Query
create table product_master
(product_no varchar2(10),Description varchar2(30),
Profit_percent number(10),Unit_measure varchar2(10),
Qty_on_hand number(10),Reoder_lvl number(10),Sell_price number(15),
Cost_price number(15));

2. Insert the following data their respective tables:


a) Data for Client_Master Table

Query:
insert all
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0001,'Ivan','Bombay',400054,'Maharashtra',15000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0002,'Vandana','Madras',780001,'Tamilnadu',0)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0003,'Pramada','Bombay',400057,'Maharashtra',5000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0004,'Basu','Bombay',400056,'Maharashtra',0)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(00005,'Ravi','Delhi',100001,null,2000)
INTO client_master(Client_no,name,city,pincode,state,bal_due)
VALUES(0006,'Rukmini','Bombay',400050,'Maharashtra',0)
select * from dual;

Page 7
DATA BASE MANAGEMENT SYSTEM
b) Data for Product Master Table:

Query
insert all
INTO product_master VALUES('P00001','1.44floppies',5,'piece',100,20,525,500)
INTO product_master VALUES('P03453','Monitors',6,'piece',10,3,12000,11200)
INTO product_master VALUES('P06743','Mouse',5,'piece',20,5,1050,500)
INTO product_master VALUES('P07865','1.22floppies',5,'piece',100,20,525,500)
INTO product_master VALUES('P07868','Keyboards',2,'piece',10,3,3150,3050)
INTO product_master VALUES('P07885','CDDrive',2.5,'piece',10,3,5250,5100)
INTO product_master VALUES('P07965','540HDD',4,'piece',10,3,8400,8000)
INTO product_master VALUES('P07975','1.44Drive',5,'piece',10,3,1050,1000)
INTO product_master VALUES('P08865','1.22Drive',5,'piece',2,3,1050,1000)
select * from dual;

3. On the basis of above to two tables answer the following


Queries:

i. Find out the names of all the clients.

Query:
select name from client_master;

ii. Retrieve the list names and citites of all the clients.

Query:
select name,city from client_master;

iii. List the various products available from the product_master table.

Page 8
DATA BASE MANAGEMENT SYSTEM

Query:
select Description from product_master;

iv. List all the clients who are located in Bombay.


Query:
select name from client_master
where city='Bombay';

v. Display the information for the client no 0001 to 0002.


Query:
where client_no in (0001,0002);

vi. Find the products with the description as 1.44 drive and 1.22 Drive.
Query:
select product_no,description from product_master
where description in ('1.44Drive','1.22Drive');

vii. Find all the products whose sell price is greater than 5000.
Query:
select sell_price from product_master
where sell_price>5000;
viii. Find the list of all clients who stay in city Bombay or city Delhi or
Madras.
Query:
select name from client_master
where city='Bombay' OR city='Delhi' OR city='Madras';

ix. Find the product whose selling price is greater than 2000 and less
than or equal to 5000.
Query:
select product_no,description from product_master
where sell_price>2000 and sell_price<=5000;
x. List the name,city and state of clients no in the state of
Maharashtra.
Query:
select name,city,client_no from client_master
where state='Maharashtra';

Page 9
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 4
Q 1. Using the table client master and product master answer the following
questionnaires.

i. Change the selling price of 1.44 floppy drive to rs. 1150.00


Query:
update product_master
set sell_price=1150.00
where description='1.44floppies';

ii. Delete the record with client 0001 from the client Master table.
Query:
delete from client_master
where client_no=0001;

iii. Change the city of client_no 0005 to Bombay.


Query:
update client_master
set city='Bombay'
where client_no=0005;

iv. Change the bal_due of client_no 0001 to 1000.


Query:
update client_master
set bal_due=1000
where client_no=0001;

v. Find the Products whose selling price more than 1500 and also find
the new selling price as original selling price * 15.
Query:
select sell_price,sell_price*15 from product_master
where sell_price>1500;

vi. Find the clients who stay in a city whose second letter is a.
Query:
select name,city from client_master
where city like '_a%'

Page
10
DATA BASE MANAGEMENT SYSTEM
vii. Find out the name of all clients have a as the second letter in their in
names.
Query:
select name from client_master
where name like '_a%';

viii. List the products in sorted order of their description.

Query:
select description from product_master order by description;

ix. Count the total number of orders.


Query:
select count(REODER_LVL) from product_master;

x. Calculate the average price of all the products.


Query:
select avg(SELL_PRICE) from product_master;

xi. Calculate the minimum price of the products.


Query:
select min(SELL_PRICE) from product_master;

xii. Determine the maximum and minimum prices. Rename the tittle as
max_price and min_price respectively.
Query:
select max(SELL_PRICE) "max_price",min(SELL_PRICE) "min_price" from
product_master;

xiii. Count the number of products having price greater than or equal to
1500.
Query:
select count(sell_price) from product_master
where sell_price>1500 OR sell_price=1500;

Page
11
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 5
Exercise:
i. Create the following table.

Query:
CREATE TABLE challan_header
(challan_no varchar2(6) not null, s_order_no VARCHAR2(6),
challan_date date not null,billed_yn char(1) DEFAULT 'n',
PRIMARY KEY (challan_no),
FOREIGN KEY (s_order_no) REFERENCES sales_order(s_order_no));

Query:
CREATE TABLE challan_details
(challan_no varchar2(6) not null,Qty_disp number(4,2) not null,
PRIMARY KEY (challan_no),
FOREIGN KEY (challan_no)
REFERENCES product_master(challan_no));

Page
12
DATA BASE MANAGEMENT SYSTEM

ii. Insert following data in the respective tables

Query:
insert all
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH9001','19001','12-Dec-95','Y')
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH865','46865','12-Nov-96','Y')
into
challan_header(challan_no,s_order_no,challan_date,billed_yn)
values('CH3965','10008','12-Oct-95','Y')
select * from dual

Query:
insert all
into challan_details values('CH9001','P00001',4)
into challan_details values('CH9001','P07965',1)
into challan_details values('CH9001','P07885',1)
into challan_details values('CH6865','P07868',3)
into challan_details values('CH6865','P03453',4)
into challan_details values('CH6865','P00001',10)

Page
13
DATA BASE MANAGEMENT SYSTEM
into challan_details values('CH3965','P00001',5)
into challan_details values('CH3965','P07975',2)
select * from dual;

iii. Answer the following Queries

i. Make the Primary key to client_no in client_master.


Query:
alter table client_master
add primary key(client_no);

ii. Add a new column Phone_no in the client_master table.


Query:
alter table client_master
add Phone_no number(11);
iii.Add the not null constraint in Product_master table with
columns Description,profit percent, sell price and cost
price.
Query:
ALTER TABLE product_master
modify
(description varchar2(30) NOT NULL,
Profit_percent number(10) NOT NULL,
sell_price number(15) NOT NULL,
cost_price number(15) not null);

iv. Change the size of client_no field in the client_master


table.
Query:
alter table client_master
modify client_no varchar2(8);
v. Select Product no, description where the profit percent is
between 20 and 30 both inclusive
Query:
Select Product_no,description from product_master
where profit_percent in (20,30);

Page
14
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 6
Objective: The purpose of this exercise is to create Entity Relationship
Diagrams.

Exercise
a) A company has several departments. Each department has a supervisor and
at least one employee. Employees must be assigned to at least one, but
possibly more departments. At least one employee is assigned to a project,
but an employee may be on vacation and not assigned to any projects. The
important data fields are the names of the departments, projects, supervisors
and employees, as well as the supervisor and employee number and a unique
project number.

Company Department Supervisor

Company name Departmentment Supervisor no.


has name Run by
Company Address Department ID Supervisor Name

Is Assigned

Employee Employees of Dept.

Employee no. Departmentment name


involves Department ID
Employee Name Employee No.
Employee Name

works

Employee-Project Project

Employee no. Project No.


Employee name works on
Project No. Project Name
Project Name

Page
15
DATA BASE MANAGEMENT SYSTEM

b) A University contains many Faculties. The Faculties in turn are divided into
several Schools. Each School offers numerous programs and each program
contains many courses. Lecturers can teach many different courses and even
the same course numerous times. Courses can also be taught by many
lecturers. A student is enrolled in only one program but a program can
contain many students. Students can been rolled in many courses at the
same time and the courses have many students enrolled

Faculty University

Faculty contains University


name name

divided

School Program student

School Program Student name


Name Offers code enrolled
Student ID

enrolled
hire enrolled

course std-course
Lecturer
Course name Course name
Lecturer enrolled
Course ID Course ID
ID Student Name
Student ID

Lect-Course
teaches

teaches Lecturer ID
Course name,id

Page
16
DATA BASE MANAGEMENT SYSTEM
c) A doctor can be scheduled for many appointments, but may not have any scheduled at
all. Each appointment is scheduled with exactly 1 doctor. A patient can schedule 1 or more
appointments. One appointment is scheduled with exactly 1 patient. An appointment must
generate exactly 1 bill, a bill is generated by only 1 appointment. One payment is applied to
exactly 1 bill, and 1 bill can be paid off over time by several payments. A bill can be
outstanding, having nothing yet paid on it at all. One patient can make many payments, but
a single payment is made by only 1 patient. Some patients are insured by an insurance
company. If they are insured, they can only carry insurance with one company. An insurance
compnay can have many patients carry their policies. For patients that carry insurance, the
insurance company will make payments, each single payment is made by exactly 1 insurance
company

Doctor Appointment Bill

Doctor name Scheduled


Appointment Genrates
Bill Type
No.
Doctor ID Bill No.

Scheduled Applied

Patitent Payment

Patient name make


Payment Type
Patient Ph No. Payment Time
Patient Address Payment No.

Insured Insured

Insurance Company

Company name

Company Address

Page
17
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 7
Objective: The purpose of this exercise is to learn Normalization and
Normal Forms.

Exercise 1:
Normalize the following two problems up to 3Nnf

0NF
project (project_code, project_title, project_manager, project_budget
Employee_no, Employee_name, Department_no, Department_name,
Hourly_rate, totall staff, Average hourly rate)

1NF
project (project_code, project_title, project_manager, project_budget)
employee(Project_code, Employee_no, Employee_name,
Department_no, Department_name)

Page
18
DATA BASE MANAGEMENT SYSTEM

2NF
project (project_code, project_title, project_manager, project_budget)
project employee(project_code, employee_no)
employee(Employee_no, Employee_name, Department_no,
Department_name)

3NF
project (project_code, project_title, project_manager, project_budget)
project employee(project_code, employee_no)
employee(Employee_no, Employee_name, Department_No)
Department(Department_no, Department_name)

***********************************************************

Page
19
DATA BASE MANAGEMENT SYSTEM

0NF
CustomerNo, CustomerName, CustomerAddress, Sales order number, Sales
order date, ClerkNo, ClerkName, ItemNo, Description, Quantity, Unit Price,
total, Order Total

1NF
CustomerNo, CustomerName, CustomerAddress, Sales order number, Sales
order date, ClerkNo, ClerkName, ItemNo, Description, Quantity, Unit Price

2NF
ItemNo, Description
SalesOrderNo, ItemNo, Quantity, Unit Price
SalesOrderNo, Date, CustomerNo, CustomerName, CustomerAddress, ClerkNo,
ClerkName

3NF
CustomerNo, CustomerName, CustomerAddress
ClerkNo, ClerkName
SalesOrderNo, Date, CustomerNo, ClerkNo
ItemNo, Description
SalesOrderNo, ItemNo, Quantity, Unit Price

Page
20
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 8
Exercise I:

Problem Statement:
Create location, department, job_grade, and employee tables with the
following columns.
Location: (location_id: number, city: string)

Department: (department_id: number, department_name: string, head: number,


department_location: number)

Job_grade: (job_grade: string, lower_bound: number, upper_bound: number)

Employee: (employee_id: number, first_name: string, last_name: string, join_date:


date, manager_id: number, salary: number)

Solution:
1. Understand create table syntax.

Create table tablename

(col-name1 datatype constraint,col-name2 datatype constraint..);

2. Decide the name of the table

Location: (location_id: number, city: string)

Table name: Location

Department: (department_id: number, department_name: string, head:


number, department_location: number)

Table name: Depatment

Job_grade: (job_grade: string, lower_bound: number, upper_bound:


number)

Page
21
DATA BASE MANAGEMENT SYSTEM
Table name: Job_grade

Employee: (employee_id: number, first_name: string, last_name: string,


join_date: date, manager_id: number, salary: number)

Table name: Employee

3. Decide the name of each column and its data type.

For table Location:

Column Data type


location_id number
City varchar
For table Department:

Column Data type


department_id number
department_name varchar
head number
department_location number

For table Job_grade:

Column Data type


job_grade varchar
lower_bound number
upper_bound Number

For table Employee:

Column Data type


employee_id Number
first_name Varchar
last_name Varchar
join_date Date

Page
22
DATA BASE MANAGEMENT SYSTEM
manager_id number
salary Number

4. Use the create table syntax to create the said tables.

Query:

For table Location:


create table location
(location_id number(4),city varchar2(10));

For table Depatment:


create table department
(department_id number(4), department_name varchar2(20),
head number(4), department_location number(4));

For table Job_grade:


create table job_grade
(job_grade varchar2(2), lower_bound number(4),
upper_bound number(4));
For table Employee:
create table employee
(employee_id number(4), first_name varchar2(10),
last_name varchar2(10), join_date date,
manager_id number(4), salary number(8));

5. Create primary key constraint for each table as understand from


logical table structure.

Query:

For table Location:


ALTER TABLE Location
ADD PRIMARY KEY (location_Id);

Page
23
DATA BASE MANAGEMENT SYSTEM

For table Depatment:


ALTER TABLE Department
ADD PRIMARY KEY (department_Id);
For table Job_grade:
No need of primary key

For table Employee:


ALTER TABLE Employee
ADD PRIMARY KEY (employee_Id);

Exercise 2:

Problem Statement:
Alter employee table to add job_grade column which is of string
data type.

1. Learn alter table syntax.


ALTER TABLE table_name
ADD column_name datatype;
2. Define the new column and its data type.

New Column Data type


Job_grade varchar
3. Use the alter table syntax.

Query:
ALTER TABLE employee
ADD job_grade varchar2(2);

Exercise 3:
Problem Statement:
Alter employee table to make job_grade a foreign key to job_grade
table, manager_id a foreign key to employee table, department_id a
foreign key to department table.

Page
24
DATA BASE MANAGEMENT SYSTEM

1. Learn alter table syntax.


ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id);

2. . Define the new constraint [type, name, columns effected]

job_grade (FK) reference to job_grade table


manager_id(FK) reference employee table
department_id(FK) reference department table.

3. Use the alter table syntax for adding constraints.


Query:
ALTER TABLE employee
ADD FOREIGN KEY (job_grade)
REFERENCES job_grade (job_grade);

ALTER TABLE employee


ADD FOREIGN KEY (manager_id)
REFERENCES employee (manager_id);

ALTER TABLE employee


ADD FOREIGN KEY (department_id)
REFERENCES department (department_id);

Exercise 4:

Problem Statement:
Create a dummy table called my_employee with the same definition as
employee table and then drop the table.

Q1. Use create table to create my_employee.


Query:
create table myemployee
as select * from employee;
Q2. Use drop table to drop my_employee.
Query:
Drop table myemployee;

Page
25
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 9
Exercise 1:
Problem Statement:
Insert data into location, department, job_grade & employee tables.

Q1. Decide the data to add in location.


Location id location name
2. Add to location one row at a time using insert into syntax
Query:

Q3. Decide the data to add in department.


Department id,department name
Q4. Add to department one row at a time using insert into syntax.
Query:
Insert into department
(department_id,department_name)
Values(10,CCSIS);
Q5. Decide the data to add in job_grade
Job id, job position
Q6. Add to job_grade one row at a time using the insert into syntax.
Query:
Insert into job_grade
(job_id,job_position)
Values(5432,Clerk);
Q7. Decide the data to add in employee.
Employee no,employee name, salary

Q8. Add to employee one row at a time using the insert into
syntax.
Query:
Insert into employee
(Employee no,employee name, salary)
Values(111,Mike,5000,);

Page
26
DATA BASE MANAGEMENT SYSTEM

Exercise 2:
Problem Statement:
Give a list of all employees (names as first_name, last_name) who
belong to one department_id.

1. Use SELECT FROM WHERE syntax.


2. Select should include first_name and last_name in the given format.
3. from should include employee
4. where should include condition on department_id

Query:
Select first_name,last_name from emp
Where deptno=10;

Exercise 3:

Problem Statement:
Select employee last_names from employee table who belong to a
certain department_id and have a salary greater than 5000.
1. Use SELECT FROM WHERE syntax.
2. Use AND operator for 2 conditions in WHERE.
Query:
Select last_name from emp
Where department=10 and sal>5000;
Exercise 4:
Problem Statement:
Select employee last_name with first letter in capital, all smalls and all
capitals from employee table for all employees.
Solution:
1. Use select from where clause.
2. Use initcap, lower, and upper functions on last_name in select clause
to get the result.

Query:
Select initcap(last_name),
lower(last_name),
upper(last_name) from emp;

Page
27
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 10
Exercise 1:
Problem Statement:
Select the average salary of all employees in department with
department_id = 2.
1. Use the AVG aggregate function in select clause.
2. Use the department_id = 2 condition in where clause.

Query:
Select avg(sal) from emp
Where deptno=2;

Exercise 2:

Problem Statement:
Select AVG salary of each department which has employees in employee
table.
1. Use the AVG aggregate function in select clause.
2. Since we want average per department, have department_id in select
clause.
3. Include a grouping clause by department_id to indicate you want to
aggregate data by department_id
Query:
Select avg(sal) from emp
Where GROUP BY department_id;
Exercise 3:
Problem Statement:
Select minimum salary of all departments where the minimum salary is less
than 1000.
1. Use the MIN aggregate function in select clause.
2. Since we want average per department, have department_id in select
clause.
3. Include a grouping clause by department_id to indicate you want to
aggregate data by department_id.
4. Exclude groupings which have a minimum salary greater than 1000
using having clause.

Page
28
DATA BASE MANAGEMENT SYSTEM
Query:
select min(salary)from employee group by(dept_id) HAVING
min(salary)<1000;

Exercise 4:
Problem Statement:
Give a list of all employees who earn a salary greater than 10000 or work
in job grade MANAGER.
1. Write a select from where which gives all employees with salary
greater than 10000.
2. Write a select from where which gives all employees whose job grade
in manager.
3. Combine them using UNION clause.
Query:
select * from employee where salary>10000
union
select * from employee where job_grade='manager';

Page
29
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 11
Exercise 1:
Problem Statement:
Select the salary and additional HRA (7.5% of the salary) for each
employee in employee table rounded to a whole number.
1. Use select from where clause and arithmetic to compute HRA.
2. Use the round function to round off the computed HRA to the nearest
whole number.
Query:
select salary ,round(salary*.075) "HRA" from employee;
Exercise 2:

Problem Statement:
Select employee last_name of all employees whose salary is greater than
the salary of employee with id = 2.

1. Use an inner query to first get the salary of employee_id = 2.


2. The result from the inner query is than given in the where clause of
the outer query that selects all employees with salary greater than that.
Query:
select sal from emp
where sal>(select sal from employee where emp_id=2);

Exercise 3:

Problem Statement:
Select all employees whose salary is greater than the salaries of both
employees with ids 2 & 3.
3. Use an inner query to first get the salary of employee_id in (2,3).
4. The result from the inner query is than given in the where clause of
the outer query using the all operator that selects all employees with
salary greater than that.

Query:
Select sal from emp
Where sal>(select sal from emp where emp_id in(2,3));

Page
30
DATA BASE MANAGEMENT SYSTEM

Exercise 4:
Problem Statement:
Select employee lastname and the corresponding department_name for
all employees in employees table.
1. Use select from where clause with the from coming from
employee and department tables and a condition joining these
two table using the foreign key department_id which connects
both the tables with an equality condition.
Query:
Select lastname,deptname from emp
join dept
on emp.dept_id=dept.dept_id;

Page
31
DATA BASE MANAGEMENT SYSTEM

Lab Exercise 12

Objective:
The purpose of this exercise is to learn SQL Aggregate function.

Instructions:
SQL aggregate functions return a single value, calculated from values in a
column.
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum

1. Display Order price average.


2. Display First order date
3. Display Last order date
4. Count number of customers
5. Find sum of orders for each customer
Query:
Select AVG(o_price),COUNT(o_price),FIRST(customer),LAST(customer),

MAX(o_price),MIN(o_price),SUM(o_price) From orders;

Page
32

S-ar putea să vă placă și