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

DBMS Observation 8-2-2025 Part-2

The document provides SQL commands for creating and manipulating two tables, DEPT and EMP, including inserting records and establishing foreign key relationships. It also explains the use of aggregate functions (COUNT, SUM, AVG, MAX, MIN), GROUP BY, and HAVING clauses, along with examples of queries to extract various statistics from the EMP table. Additionally, it covers the creation and deletion of views based on the EMP table.

Uploaded by

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

DBMS Observation 8-2-2025 Part-2

The document provides SQL commands for creating and manipulating two tables, DEPT and EMP, including inserting records and establishing foreign key relationships. It also explains the use of aggregate functions (COUNT, SUM, AVG, MAX, MIN), GROUP BY, and HAVING clauses, along with examples of queries to extract various statistics from the EMP table. Additionally, it covers the creation and deletion of views based on the EMP table.

Uploaded by

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

Queries using Aggregate functions (COUNT, SUM, AVG, MAX and

MIN), GROUP BY, HAVING and Creation and dropping of Views.

DEPT TABLE

DEPTNO DNAME LOC


10 ACCOUNTING NEWYORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

EMP TABLE

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7369 SMITH CLERK 7902 17-Dec-80 800 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30
7566 JONES MANAGER 7839 02-Apr-81 2975 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-May-81 2850 30
7782 CLARK MANAGER 7839 09-Jan-81 2450 10
7788 SCOTT ANALYST 7566 19-Apr-87 3000 20
7839 KING PRESIDENT 17-Nov-81 5000 10
7844 TURNER SALESMAN 7698 08-Sep-81 10000 0 30
7876 ADEMS CLERK 7788 23-May-87 1100 20
7900 JAMES CLERK 7698 03-Dec-81 950 30
7902 FORD ANALYST 7566 03-Dec-81 3000 20
7934 MILLER CLERK 7782 23-Jan-82 1300 10

Task 4.1: Create a table with the name DEPT(DEPTNO,DNAME,LOC) AND INSERT
RECORDS
SQL>CREATE TABLE DEPT
( DEPTNO NUMBER(2) PRIMARY KEY,
DNAME VARCHAR2(10) NOT NULL,
LOC VARCHAR2(8));
SQL> desc dept

SQL> INSERT INTO DEPT VALUES(10,'ACCOUNTING','NEWYORK');

SQL> INSERT INTO DEPT VALUES(20,'RESEARCH','DALLAS');

SQL> INSERT INTO DEPT VALUES(30,'SALES','CHICAGO');

SQL> INSERT INTO DEPT VALUES(40,'OPERATIONS','GOSTON');

SQL> SELECT *FROM DEPT;

Task 4.2: Create a table with the name EMP and set foreign key with DEPTNO which
references to DEPT table and insert rocords
SQL> CREATE TABLE EMP
(EMPNO NUMBER(5) PRIMARY KEY,
ENAME VARCHAR2(10) NOT NULL,
JOB VARCHAR2(10),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2) CHECK(SAL>=500 AND SAL<=10000),
COMM NUMBER(7,2),
DEPTNO NUMBER(2),
FOREIGN KEY(DEPTNO) REFERENCES DEPT);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7369,'SMITH','CLERK',7902,'17-DEC-80',800,20);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB, MGR,HIREDATE,SAL,COMM,DEPTNO)VALUES
(7499,'ALLEN','SALESMAN',7698,'20-FEB-81',1600,300,30);
.

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)VALUES
(7521,'WARD','SALESMAN',7698,'22-FEB-81',1250,500,30);
SQL> INSERT INTO EMP
(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7566,'JONES','MANAGER',7839,'2-APR-81',2975,20);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)VALUES
(7654,'MARTIN','SALESMAN',7698,'28-SEP-81',1250,1400,30);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7698,'BLAKE','MANAGER',7839,'1-MAY-81',2850,30);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7782,'CLARK','MANAGER',7839,'9-JUN-81',2450,10);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7788,'SCOTT','ANALYST',7566,'19-APR-87',3000,20);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,HIREDATE,SAL,DEPTNO)VALUES
(7839,'KING','PRESIDENT','17-NOV-81',5000,10);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)VALUES
(7844,'TURNER','SALESMAN',7698,'8-SEP-81',1000,0,30);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7876,'ADEMS','CLERK',7788,'23-MAY-87',1100,20);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7900,'JAMES','CLERK',7698,'3-DEC-81',950,30);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7902,'FORD','ANALYST',7566,'3-DEC-81',3000,20);

SQL> INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO)VALUES
(7934,'MILLER','CLERK',7782,'23-JAN-82',1300,10);
SQL> SELECT * FROM EMP;

AGGREGATE FUNCTIONS
The following are the most commonly used SQL aggregate functions:
• AVG – calculates the average of a set of values.
• COUNT – counts rows in a specified table or view.
• MIN – gets the minimum value in a set of values.
• MAX – gets the maximum value in a set of values.
• SUM – calculates the sum of values.

SQL | GROUP BY
The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of
some functions. i.e if a particular column has same values in different rows then it will arrange
these rows in a group.
Important Points:
• GROUP BY clause is used with the SELECT statement.
• In the query, GROUP BY clause is placed after the WHERE clause.
• In the query, GROUP BY clause is placed before ORDER BY clause if used any.
Syntax:
SELECT column1, function_name(column2)
FROM table_name WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;
function_name: Name of the function used for example, SUM() , AVG().
table_name: Name of the table.
condition: Condition used.

HAVING Clause
We can use HAVING clause to place conditions to decide which group will be the part of final
result-set. Also we can not use the aggregate functions like SUM(), COUNT() etc. with WHERE
clause. So we have to use HAVING clause if we want to use any of these functions in the
conditions
Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING condition
ORDER BY column1, column2;

Task 4.3: Write a query to display count of different jobs in the table ‘EMP’:
SQL> SELECT COUNT(DISTINCT JOB) FROM EMP;

Task 4.4: Write a query to display TOTAL salary of all employees in the table ‘EMP’:
SQL> SELECT SUM(SAL) FROM EMP;

Task 4.5: Write a query to display maximum of salary in the table ‘EMP’:
SQL> SELECT MAX(SAL) FROM EMP;

Task 4.6: Write a query to display maximum of salary among job type ‘SALESMAN’ in
the table ‘EMP’:
SQL> SELECT MAX(SAL) FROM EMP WHERE JOB='SALESMAN';

Task 4.7: Write a query to display minimum of salary in the table ‘EMP’:
SQL> SELECT MIN(SAL) FROM EMP;

Task 4.8: Write a query to display average salary from DEPTNO is 20 in the table ‘EMP’
SQL> SELECT AVG(SAL) FROM EMP WHERE DEPTNO=20;

Task 4.9: Write a query to display departmentwise count of employees in the table ‘EMP’
SQL> SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO;

Task 4.10: Write a query to display departmentwise total salary of employees in the table
‘EMP’
SQL> SELECT DEPTNO,SUM(SAL) FROM EMP GROUP BY DEPTNO;
Task 4.11: Write a query to display jobwise count of employees in descending order in the
table ‘EMP’
SQL> SELECT JOB,COUNT(*) FROM EMP GROUP BY JOB ORDER BY
COUNT(*) DESC;

Task 4.12: Write a query to display jobwise sum,average, maximum and minimum salaries
of employees in the table ‘EMP’
SQL> SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),MIN(SAL) FROM
EMP GROUP BY JOB;
Task 4.12: Write a query to display jobwise average salary of employees in the table ‘EMP’
except job category Manager
SQL> SELECT JOB,AVG(SAL) FROM EMP WHERE JOB!='MANAGER'
GROUP BY JOB;

Task 4.13: Write a query to display jobwise maximum salary of employees whose
maximum salary having greater than 500 in the table ‘EMP’
SQL> SELECT JOB,MAX(SAL) FROM EMP GROUP BY JOB HAVING
MAX(SAL)>=500;

Task 4.14: Write a query to display jobwise sum,average, maximum and minimum salary
of employees working in department no 20 whose average salary having greater than 1000
in the table ‘EMP’
SQL> select job,sum(sal),avg(sal),max(sal),min(sal) from emp where
deptno=20 group by job having avg(sal)>1000 order by sum(sal);

VIEWS
Views:A view is a logical table based on a table or another view. A view contains no data of its
own but is like a window through which data from tables can be viewed or changed.
Consider EMP table.
Consider Dept table

Task 4.15: Write a query to create a view from the table ‘emp’ to display only
empno,ename,sal
SQL> CREATE OR REPLACE VIEW EMP_VIEW1 AS SELECT EMPNO,ENAME,SAL
FROM EMP;

SQL> SELECT * FROM EMP_VIEW1;

Task 4.16: Write a query to create a view from the table ‘emp’ to display only empno,
ename, salary, deptno of employees of department number 20
SQL> CREATE OR REPLACE VIEW EMP_VIEW2 AS SELECT EMPNO,ENAME,SAL,
DEPTNO FROM EMP WHERE DEPTNO=20;

SQL> SELECT *FROM EMP_VIEW2;

Task 4.17: Write a query to DELETE a view emp_view1


SQL> DROP VIEW EMP_VIEW1;

SQL> DESC EMP_VIEW1;


ERROR:
ORA-04043: object emp_view does not exist

You might also like