DBMS Exp-4
DBMS Exp-4
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];
● 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:
e.g.: SELECT * FROM empvu80;
Removing View:
Syntax: DROP VIEW view; e.g.:
DROP VIEWempvu80
PART B
B.1 CODE
1. AVG, COUNT, MAX, MIN CODE
USE EXP1;
SELECT AVG(SALARY), MAX(SALARY), MIN(SALARY), SUM(SALARY) FROM
EMPLOYEE;
2. COUNT
2a) USE EXP1;
SELECT COUNT (*) FROM EMPLOYEE;
2b) DISTINCT COUNT
USE EXP1;
SELECT DISTINCT JOBID, FIRSTNAME, LASTNAME FROM EMPLOYEE;
3. GROUP BY CLAUSE
USE EXP1;
SELECT AVG(SALARY), FIRSTNAME FROM EMPLOYEE GROUP BY FIRSTNAME;
4. HAVING CLAUSE
USE EXP1;
SELECT JOBID, MAX(SALARY) FROM EMPLOYEE GROUP BY JOBID HAVING
MAX(SALARY)>80000;
5. VIEW
a) To create view
USE EXP1;
CREATE VIEW EMP_HIGH_SALARY AS
SELECT EMPID, FIRSTNAME, LASTNAME, SALARY FROM EMPLOYEE WHERE
SALARY > 90000;
b) To access view table
USE EXP1;
SELECT * FROM EMP_HIGH_SALARY;
c) To delete view table
USE EXP 1
DROP VIEW EMP_HIGH_SALARY;
B.2 OUTPUT
1. AVG, COUNT, MAX, MIN CODE
2a) COUNT
2b) DISTINCT COUNT
3. GROUP BY CLAUSE
4. HAVING CLAUSE
5. VIEW
a) To create view
b) To access view table
B.3 OBSERVATIONS:
In this experiment, Aggregate functions such as SUM, AVG, COUNT, MIN, MAX, ORDER
BY CLAUSE AND HAVING CLAUSE efficiently process large datasets, summarizing data
according to specified criteria. Also, creating Views simplified complex queries by
encapsulating them into reusable virtual tables, enhancing query readability and
maintainability.
B.4 CONCLUSIONS:
In conclusion, the experiment highlighted the usefulness of aggregate functions for data
summarization and views for simplifying complex queries in SQL. These tools are crucial for
efficient data analysis and management in relational databases.