DBMS Observation 8-2-2025 Part-2
DBMS Observation 8-2-2025 Part-2
DEPT TABLE
EMP TABLE
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
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);
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;
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;