0% found this document useful (0 votes)
2 views7 pages

A24 Exp04 DBMS

The document outlines an experiment focused on using aggregate functions and views in Oracle 9i. It provides theoretical explanations of group functions, the HAVING clause, and the creation and use of views, along with practical SQL examples. Additionally, it includes lab assignments that require students to perform specific SQL queries and operations related to employee data.

Uploaded by

vikas.231203101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

A24 Exp04 DBMS

The document outlines an experiment focused on using aggregate functions and views in Oracle 9i. It provides theoretical explanations of group functions, the HAVING clause, and the creation and use of views, along with practical SQL examples. Additionally, it includes lab assignments that require students to perform specific SQL queries and operations related to employee data.

Uploaded by

vikas.231203101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Mumbai University

Vidyavardhini’s College of Engineering and Technology, Vasai

Experiment No: - 4
Experiment Name: - Practical using Aggregate functions and View.

Aim: - Performing practical by using Aggregate functions and View,

Resource required: - Oracle 9i - iSQLplus

Theory
• AGGREGATING DATA USING GROUP FUNCTIONS:

Group Functions: It operate on sets of rows to give one result per group.
Syntax: SELECT [column,] group_function (column), …
FROM table
[WHERE condition]
[GROUP BY column]
[ORDER BY column];

- Types of Group Functions:


1. AVG, COUNT, MAX, MIN, STDDEV, SUM, and VARIANCE
e.g.: SELECT AVG (salary), MAX (salary), MIN (salary), and SUM (salary)
FROM employees
WHERE job_id LIKE ‘%REP%’;
AVG (SALARY) MAX (SALARY) MIN (SALARY) SUM (SALARY)
8150 11000 6000 32600

2. COUNT (*): returns the number of rows in a table.


e.g.: SELECT COUNT (*)
FROM employees
WHERE department_id = 50;

3. DISTINCT: returns the number of distinct non-null values.


e.g.: SELECT COUNT (DISTINCT department_id)
FROM employees;

• Creating groups of data using GROUP BY clause:


Syntax: SELECT column, group_function (column)
From table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];
e.g.: SELECT AVG (salary)
FROM employees
GROUP BY department_id;

15
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

• The HAVING Clause: to restrict groups


Syntax: SELECT column, group_function
From table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

e.g.: SELECT department_id, MAX (salary)


FROM employees
GROUP BY department_id
HAVING MAX (salary)>10000;
- Displays department numbers and maximum salaries for those departments whose
maximum salary is greater than $10,000.

• Nesting Group Functions:


e.g.: SELECT MAX (AVG (salary))
FROM employees
GROUP BY department_id;

• VIEWS:
- Logically represents subsets of data from one or more tables. Why it is used:
- To restrict data access.
- To make complex queries easy
- To represent different views of the same data.

Creating a View:
Syntax: CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias [, alias]…)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]]; e.g.: CREATE
VIEW empvu80

AS SELECT employee_id, last-name, salary


FROM employees
WHERE DEPARTMENT_ID=80;
Removing data from View:

16
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

e.g.: SELECT *
FROM empvu80; Removing View:

Syntax: DROP VIEW view; e.g.:


DROP VIEW empvu80

Conclusion:
You should have learned how to use joins to display data from multiple tables, group
functions and views.

Lab Assignment No- 04


1. Display the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively.

2. Determine the count of the Number of managers without actually listing them. Label the
column Number of Managers.

3. Create a view called EMPLOYEES_VU based on the employee numbers, employee names,
and department numbers from the EMPLOYEE table. Changes the heading for the employee
name to EMPLOYEE. And Display the contents of the EMPLOYEE_VU view.

4. Select the view name and text from the USER_VIEWS data dictionary view.

CODE:

CREATE TABLE employ(


employee_id INTEGER,
last_name VARCHAR2(50),
job_id VARCHAR2(50),
department_id integer,
salary INTEGER,
commision INTEGER,
department_name varchar2(50),
location_id varchar2(50),
city varchar2(50)
);

17
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

INSERT INTO employ VALUES(100, 'King','SA_REP', 80, 10000,2000,'COMPS','E1','mumbai');


INSERT INTO employ VALUES(101, 'Kochhar', 'ST_CLERK', 67,20000,3000,'IT','E2','delhi');
INSERT INTO employ VALUES(102, 'De Haan', 'AD_VP', 92,30000,1500,'EXTC','E3','pune');
INSERT INTO employ VALUES(103, 'Hunold', 'IT_PROG', 45, 40000,0,'VLSI','E4','kolhapur');
INSERT INTO employ VALUES(104, 'Ernst', 'IT_PROJ', 80,50000,5000,'CSE','E5','nagpur');

SELECT employee_id AS "Employee ID",


last_name AS "Last Name",
job_id AS "Job ID",
department_id AS "Department ID",
salary AS "Salary",
commision AS "Commission",
department_name AS "Department Name",
location_id AS "Location ID",
city AS "City"
FROM employ;

SELECT last_name AS "Last Name",


department_name AS "Department Name"
From employ;

CREATE TABLE DEPARTMENTS (


department_id INTEGER PRIMARY KEY,
department_name VARCHAR(100),
location VARCHAR2(50)
);

INSERT INTO DEPARTMENTS VALUES (80, 'Human Resources', 'New York');


INSERT INTO DEPARTMENTS VALUES (62, 'Information Technology', 'San Francisco');
INSERT INTO DEPARTMENTS VALUES (77, 'Sales', 'Chicago');
INSERT INTO DEPARTMENTS VALUES (80, 'Marketing', 'Los Angeles');
INSERT INTO DEPARTMENTS VALUES (92, 'Finance', 'London');

drop table DEPARTMENTS

SELECT *
FROM employ
CROSS JOIN DEPARTMENTS;

SELECT last_name,
department_name,
location_id,
city
FROM employ
WHERE commision IS NOT NULL;

18
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

SELECT DISTINCT job_id, city


FROM employ
WHERE department_id = 80;

SELECT employ.employee_id,
employ.last_name,
employ.job_id,
employ.department_id,
employ.salary,
DEPARTMENTS.department_name,
DEPARTMENTS.location
FROM employ
LEFT OUTER JOIN DEPARTMENTS
ON employ.department_id = DEPARTMENTS.department_id;

SELECT employ.employee_id, employ.last_name, employ.job_id, employ.salary, employ.city,


DEPARTMENTS.department_name, DEPARTMENTS.location
FROM employ
RIGHT OUTER JOIN DEPARTMENTS
ON employ.department_id = DEPARTMENTS.department_id;

select * from employ;


select * from DEPARTMENTS;

SELECT employ.employee_id, employ.last_name, employ.job_id, employ.salary, employ.city,


DEPARTMENTS.department_name, DEPARTMENTS.location
FROM employ
FULL OUTER JOIN DEPARTMENTS
ON employ.department_id = DEPARTMENTS.department_id;

SELECT employ.employee_id, employ.last_name,


employ.department_id, DEPARTMENTS.department_id,
DEPARTMENTS.location
FROM employ, DEPARTMENTS
WHERE employ.department_id = DEPARTMENTS.department_id;

SELECT department_id, department_name, location


FROM DEPARTMENTS;
NATURAL JOIN location;

--ASSIGNMENT 4

SELECT
MAX(salary) AS "Maximum",
MIN(salary) AS "Minimum",
SUM(salary) AS "Sum",
AVG(salary) AS "Average"
FROM employ;
19
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

SELECT COUNT(*) AS "Number of Managers"


FROM employ
WHERE job_id = 'MANAGER';

CREATE VIEW EMPLOYEES_VU AS


SELECT employee_id AS "Employee Number",
last_name AS "EMPLOYEE",
department_id AS "Department Number"
FROM employ;

select * from EMPLOYEES_VU;

SELECT view_name, text


FROM user_views;

--experiment 4

SELECT department_id,
SUM(salary) AS total_salary
FROM employ
GROUP BY department_id
HAVING SUM(salary) > 50000;

SELECT department_id, SUM(salary) AS total_salary


FROM employ
GROUP BY department_id;

OUTPUT:

20
Mumbai University
Vidyavardhini’s College of Engineering and Technology, Vasai

21

You might also like