2CSE DBMS Lab Programs
2CSE DBMS Lab Programs
(1)EMPLOYEE:
(3)DEPT_LOCATIONS
CREATE TABLE DEPT_LOCATIONS(DNUMBER NUMBER(1),VARCHAR2(9));
(4).PROJECT:
LOWER(DNAME)
--------------
accounting
research
sales
operations
LIST ALL EMPLOYEE NAMES WHO HAVE A NAME EXACTLY 4 CHARACTERS IN LENGTH.
ENAME
----------
WARD
KING
FORD
ENAME
----------
KING
Table created.
14 rows selected.
8*7/2+9-6*2
-----------
25
Savepoint created.
1 row deleted.
Rollback complete.
EMPNO TOTALSAL
---------- ----------
7369 900
7499 2000
7521 1850
7566 3075
7654 2750
7698 2950
7782 2550
7788 3100
7839 5100
7844 1600
7876 1200
7900 1050
7902 3100
7934 1400
14 rows selected.
EMPLOYEE_NUMBER EMPLOYEE_N
--------------- ----------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
EMPNO
----------
7369
7566
7698
7782
7788
7839
7876
7900
7902
7934
10 rows selected.
DISPLAY THE CURRENT DATE AND TIME OF THE DAY.
SYSDATE TO_CHAR(
--------- --------
21-JAN-07 02:53:56
FIND ALL EMPLOYEE NAMES WHOSE JOB DOES NOT START WITH M.
SQL> SELECT ENAME, JOB FROM EMP WHERE JOB NOT LIKE 'M%';
ENAME JOB
---------- ---------
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
MARTIN SALESMAN
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK
11 rows selected.
SQL> COMMIT;
Commit complete.
SQL> SELECT MAX (SAL) MAX_SAL, MAX (COMM) MAX_COMM FROM EMP WHERE DEPTNO = 30;
MAX_SAL MAX_COMM
---------- ----------
2950 1400
SQL> SELECT ENAME, DEPTNO FROM EMP WHERE DEPTNO IN(10,20) ORDER BY ENAME;
ENAME DEPTNO
---------- ----------
ADAMS 20
CLARK 10
FORD 20
JONES 20
KING 10
MILLER 10
SCOTT 20
SMITH 20
8 rows selected.
CALCULATE THE NUMBER OF EMPLOYEES IN DEPT 20.
NO_EMP
----------
5
ORDER THE OUTPUT IN ASCENDING ORDER OF APPRAISAL DATE.
APPRAISAL
---------
17-DEC-81
20-FEB-82
22-FEB-82
02-APR-82
01-MAY-82
09-JUN-82
08-SEP-82
28-SEP-82
17-NOV-82
03-DEC-82
03-DEC-82
23-JAN-83
19-APR-88
23-MAY-88
14 rows selected.
CALCULATE THE AVERAGE SALARY OF EACH DEPARTMENT.
DEPTNO AVG_SAL
---------- ----------
10 3017
20 2275
30 1667
JOB ROUND(AVG(SAL+NVL(COMM,0)))
--------- ---------------------------
ANALYST 3100
CLERK 1138
MANAGER 2858
PRESIDENT 5100
SALESMAN 2050
SQL> SELECT ENAME FROM EMP WHERE ENAME LIKE '%LL%' OR ENAME LIKE '%TH%';
ENAME
----------
SMITH
ALLEN
MILLER
DNAME SUBST
-------------- -----
ACCOUNTING COUNT
RESEARCH SEARC
SALES LES
OPERATIONS ERATI
ENAME
----------
JONES
BLAKE
CLARK
LIST THE DEPARTMENT NAME OF EACH EMPLOYEE.
ENAME DNAME
---------- --------------
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
14 rows selected.
SQL> SELECT * FROM EMP WHERE SAL IN (SELECT MIN (SAL) FROM EMP);
LIST THE ENAMES AND THEIR SALARY INCREASED BY 20% AND EXPRESSED AS WHOLE NUMBER.
14 rows selected.
TO_CHAR(SYSDA
-------------
21ST JAN. '07
DISPLAY THE SYSTEM DATE IN THE FOLLOWING FORMAT.
A) 09/08/2002.
B) 09TH AUG TWO THOUSAND TWO.
C) 09TH AUGUST TWO THOUSAND TWO.
TO_CHAR(SY
----------
22/01/2007
TO_CHAR(SYSDATE,'DDTHMONYEAR')
---------------------------------------------------
22ND JAN TWO THOUSAND SEVEN
TO_CHAR(SYSDATE,'DDTHMONTHYEAR')
---------------------------------------------------------
22ND JANUARY TWO THOUSAND SEVEN
SQL> SELECT ENAME FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP);
ENAME
----------
KING
SQL> SELECT DNAME FROM DEPT WHERE DEPTNO = (SELECT DEPTNO FROM EMP GROUP BY
DEPTNO HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM EMP GROUP BY DEPTNO));
DNAME
--------------
SALES
SQL> SELECT DNAME FROM DEPT WHERE DEPTNO = (SELECT DEPTNO FROM EMP GROUP BY
DEPTNO HAVING COUNT(*) = (SELECT MIN(COUNT(*)) FROM EMP GROUP BY DEPTNO));
DNAME
--------------
ACCOUNTING
LIST DETAILS OF EMPLOYEE MOST RECENTLY HIRED.
LIST EMPLOYEE NAMES, NUMBERS, JOB, AND DEPARTMENT NAMES OF ALL CLERKS.
SQL> SELECT ENAME, EMPNO, JOB, DNAME FROM EMP, DEPT WHERE
2 JOB = 'CLERK' AND EMP.DEPTNO = DEPT.DEPTNO;
ENAME
----------
MILLER
CREATE A VIEW V1 WITH THE COLUMNS EMPNO, SAL, AND DEPTNO FROM EMP TABLE.
View created.
SQL> SELECT * FROM V1;
14 rows selected.
ADD A NEW COLUMN NAMED NUMBER OF EMPLOYEES (NOE) WITH DATATYPE OF NUMBER AND
LENGTH 2.
Table altered.
Table altered.
DISPLAY EMPLOYEE DETAILS WHOSE NAME DOES NOT START WITH ‘S’ USING ASCII
FUNCTION.
ENAME
----------
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
KING
TURNER
ADAMS
JAMES
FORD
MILLER
12 rows selected.
LIST MAX SAL, MIN SAL AND AVG SAL OF DEPTS 10, 30.
13 rows selected.
ENAME LEN
---------- ----------
WARD 4
KING 4
FORD 4
SMITH 5
JONES 5
BLAKE 5
SCOTT 5
JAMES 5
ADAMS 5
CLARK 5
ALLEN 5
MARTIN 6
TURNER 6
MILLER 6
14 rows selected.
LIST MGR AND THE NO. OF EMPLOYEES REPORT TO THEM IN SORTED ORDER.
SQL> SELECT DISTINCT MGR, COUNT(*) NUM FROM EMP WHERE MGR IS
2 NOT NULL GROUP BY MGR ORDER BY NUM;
MGR NUM
---------- ----------
7782 1
7788 1
7902 1
7566 2
7839 3
7698 5
6 rows selected.
LIST EMPLOYEE NAMES APPENDING ‘SRI’ TO BEGINNING AND ‘GARU’ TO THE END.
ENAME
-------------------
SRI SMITH GARU
SRI ALLEN GARU
SRI WARD GARU
SRI JONES GARU
SRI MARTIN GARU
SRI BLAKE GARU
SRI CLARK GARU
SRI SCOTT GARU
SRI KING GARU
SRI TURNER GARU
SRI ADAMS GARU
SRI JAMES GARU
SRI FORD GARU
SRI MILLER GARU
14 rows selected.
LIST EMPLOYEE NAMES WITH ALL CAPITAL LETTERS, SMALL LETTERS & FIRST LETTER ONLY
AS CAPITAL.
14 rows selected.
YEAR COUNT(EMPNO)
---- ------------
1980 1
1981 10
1982 1
1987 2
ENAME YEAR_OF_JOINING
---------- ------------------------------------------
SMITH NINETEEN EIGHTY
ALLEN NINETEEN EIGHTY-ONE
WARD NINETEEN EIGHTY-ONE
JONES NINETEEN EIGHTY-ONE
MARTIN NINETEEN EIGHTY-ONE
BLAKE NINETEEN EIGHTY-ONE
CLARK NINETEEN EIGHTY-ONE
SCOTT NINETEEN EIGHTY-SEVEN
KING NINETEEN EIGHTY-ONE
TURNER NINETEEN EIGHTY-ONE
ADAMS NINETEEN EIGHTY-SEVEN
JAMES NINETEEN EIGHTY-ONE
FORD NINETEEN EIGHTY-ONE
MILLER NINETEEN EIGHTY-TWO
14 rows selected.
ENAME
----------
ALLEN
ADAMS
EMPNO TOT_SAL
---------- ----------
7369 900
7499 2000
7521 1850
7566 3075
7654 2750
7698 2950
7782 2550
7788 3100
7839 5100
7844 1600
7876 1200
7900 1050
7902 3100
7934 1400
14 rows selected.
TOTAL
----------
14
DISPLAY TODAY’S DATE, DAY BEFORE YESTERDAY’S DATE & DAY AFTER TOMORROW’S DATE.
SQL> SELECT SYSDATE TODAY, SYSDATE-2 DAYB4YES, SYSDATE+2 DAYAFTERTO FROM DUAL;
LIST MGRS NAMES AND THEIR J.DATES COMPLETELY SPELLED IN ALPHABETICAL ORDER OF
NAMES.
TIME SYSDATE+2
-------- ---------
09:50:38 20-FEB-07
EMPDETAILS
---------------------------------------------------------------------
SMITH-----CLERK-----900
ALLEN-----SALESMAN-----1700
WARD-----SALESMAN-----1350
JONES-----MANAGER-----3075
MARTIN-----SALESMAN-----1350
BLAKE-----MANAGER-----2950
CLARK-----MANAGER-----2550
SCOTT-----ANALYST-----3100
KING-----PRESIDENT-----5100
TURNER-----SALESMAN-----1600
ADAMS-----CLERK-----1200
JAMES-----CLERK-----1050
FORD-----ANALYST-----3100
MILLER-----CLERK-----1400
14 rows selected.
DBMS LAB MAUAL
BRANCH: CSE
8. UNION: This query is used to display the combined rows of two different queries, which are
having the same structure, without duplicate rows.
10. MINUS: This query is used to display all the rows in relation_1,which are not having in the
relation_2.
Syntax: SELECT field_1,field_2,......FROM relation_1
WHERE(Condition) MINUS SELECT field_1,field_2,.....
FROM relation_2 WHERE(Conditon);
1. NOT NULL: When a column is defined as NOTNULL, then that column becomes a
mandatory column. It implies that a value must be entered into the column if the record is to
be accepted for storage in the table.
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) NOT NULL, );
Example:
CREATE TABLE student (sno NUMBER(3)NOT NULL, name CHAR(10));
2. UNIQUE: The purpose of a unique key is to ensure that information in the column(s) is
unique i.e. a value entered in column(s) defined in the unique constraint must not be repeated
across the column(s). A table may have many unique keys.
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) UNIQUE, ….);
Example:
CREATE TABLE student (sno NUMBER(3) UNIQUE, name CHAR(10));
3. CHECK: Specifies a condition that each row in the table must satisfy. To satisfy the
constraint, each row in the table must make the condition either TRUE or unknown (due to a
null).
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical
expression), ….);
Example: CREATE TABLE student (sno NUMBER (3), name CHAR(10),class
CHAR(5),CHECK(class IN(‘CSE’,’CAD’,’VLSI’));
5. FOREIGN KEY: It is a table level constraint. We cannot add this at column level. To
reference any primary key column from other table this constraint can be used. The table in
which the foreign key is defined is called a detail table. The table that defines the primary key
and is referenced by the foreign key is called the master table.
Syntax: CREATE TABLE Table_Name(column_name data_type(size)
FOREIGN KEY(column_name) REFERENCES table_name);
Example:
CREATE TABLE subject (scode NUMBER (3) PRIMARY KEY,
subname CHAR(10),fcode NUMBER(3),
FOREIGN KEY(fcode) REFERENCE faculty );
(or)
3) Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN),
GROUP BY, HAVING and Creation and dropping of Views.
Aggregative operators: In addition to simply retrieving data, we often want to perform some
computation or summarization. SQL allows the use of arithmetic expressions. We now
consider a powerful class of constructs for computing aggregate values such as MIN and
SUM.
1. Count: COUNT following by a column name returns the count of tuple in that column. If
DISTINCT keyword is used then it will return only the count of unique tuple in the column.
Otherwise, it will return count of all the tuples (including duplicates) count (*) indicates all the
tuples of the column.
Syntax: COUNT (Column name)
Example: SELECT COUNT (Sal) FROM emp;
2. SUM: SUM followed by a column name returns the sum of all the values in that column.
Syntax: SUM (Column name)
Example: SELECT SUM (Sal) From emp;
3. AVG: AVG followed by a column name returns the average value of that column values.
Syntax: AVG (n1,n2..)
Example: Select AVG(10, 15, 30) FROM DUAL;
4. MAX: MAX followed by a column name returns the maximum value of that column.
Syntax: MAX (Column name)
Example: SELECT MAX (Sal) FROM emp;
SQL> select deptno,max(sal) from emp group by deptno;
DEPTNO MAX(SAL)
------ --------
10 5000
20 3000
30 2850
DEPTNO MAX(SAL)
----- --------
30 2850
5. MIN: MIN followed by column name returns the minimum value of that column.
Syntax: MIN (Column name)
Example: SELECT MIN (Sal) FROM emp;
DEPTNO MIN(SAL)
----- --------
10 1300
VIEW: In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table.
A view is a virtual table, which consists of a set of columns from one or more tables. It
is similar to a table but it doest not store in the database. View is a query stored as an object.
Syntax: CREATE VIEW view_name AS SELECT set of fields FROM relation_name
WHERE (Condition)
1. Example:
SQL>CREATE VIEW employee AS SELECT empno,ename,job FROM EMP
WHERE job = ‘clerk’;
View created.
SQL> SELECT * FROM EMPLOYEE;
EMPNO ENAME JOB
---- ------ -------
7369 SMITH CLERK
7876 ADAMS CLERK
7900 JAMES CLERK
7934 MILLER CLERK
2.Example:
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
DROP VIEW: This query is used to delete a view , which has been already created.
Syntax: DROP VIEW View_name;
Example : SQL> DROP VIEW EMPLOYEE;
View dropped
2. String functions:
Concat: CONCAT returns char1 concatenated with char2. Both char1 and char2 can be any
of the datatypes
SQL>SELECT CONCAT(‘ORACLE’,’CORPORATION’)FROM DUAL;
ORACLECORPORATION
Lpad: LPAD returns expr1, left-padded to length n characters with the sequence of
characters in expr2.
SQL>SELECT LPAD(‘ORACLE’,15,’*’)FROM DUAL;
*********ORACLE
Rpad: RPAD returns expr1, right-padded to length n characters with expr2, replicated as
many times as necessary.
SQL>SELECT RPAD (‘ORACLE’,15,’*’)FROM DUAL;
ORACLE*********
Instr: The INSTR functions search string for substring. The function returns an integer
indicating the position of the character in string that is the first character of this occurrence.
SQL>SELECT INSTR('CORPORATE FLOOR','OR',3,2)FROM DUAL;
14
3. Date functions:
Sysdate:
SQL>SELECT SYSDATE FROM DUAL;
29-DEC-08
next_day:
SQL>SELECT NEXT_DAY(SYSDATE,’WED’)FROM DUAL;
05-JAN-09
add_months:
SQL>SELECT ADD_MONTHS(SYSDATE,2)FROM DUAL;
28-FEB-09
last_day:
SQL>SELECT LAST_DAY(SYSDATE)FROM DUAL;
31-DEC-08
months_between:
SQL>SELECT MONTHS_BETWEEN(SYSDATE,HIREDATE)FROM EMP;
4
Least:
SQL>SELECT LEAST('10-JAN-07','12-OCT-07')FROM DUAL;
10-JAN-07
Greatest:
SQL>SELECT GREATEST('10-JAN-07','12-OCT-07')FROM DUAL;
10-JAN-07
Trunc:
SQL>SELECT TRUNC(SYSDATE,'DAY')FROM DUAL;
28-DEC-08
Round:
SQL>SELECT ROUND(SYSDATE,'DAY')FROM DUAL;
28-DEC-08
to_char:
SQL> select to_char(sysdate, "dd\mm\yy") from dual;
24-mar-05.
to_date:
SQL> select to_date(sysdate, "dd\mm\yy") from dual;
24-mar-o5.
Simple queries:
1. Write a query to retrieve the details of all employees working in the company.
8 rows selected.
FNAME SALARY
--------------- ----------
John 30000
Franklin 40000
Alicia 25000
Jennifer 43000
Ramesh 38000
Joyce 25000
Ahmad 25000
James 55000
8 rows selected.
SALARY SSN
---------- ---------
30000 123456789
40000 333445555
25000 999887777
43000 987654321
38000 666884444
25000 453453453
25000 987987987
55000 888665555
8 rows selected.
SALARY
----------
25000
30000
38000
40000
43000
55000
6 rows selected.
6. Write a query to retrieve birth date and address of the employee(s) whose name is ‘John B
Smith’.
A.SQL> select bdate, address from employee where fname='John'and minit='B' and
lname='Smith';
BDATE ADDRESS
--------- ------------------------------
09-JAN-65 731 fondren, Houston, TX
8. Write a query to retrieve all employees who were born during the 1950s
A.SQL> select * from employee where bdate like '%5_';
9. Write a query to retrieve all employees whose salary is between $30,000 and $40,000.
A.SQL> select * from employee where salary between 30000 and 40000;
Write a query to retrieve names and salaries of employees in the descending order of their
salaries
A. 1* select fname,salary from employee order by salary desc
SQL> /
FNAME SALARY
--------------- ----------
James 55000
Jennifer 43000
Franklin 40000
Ramesh 38000
John 30000
Alicia 25000
Joyce 25000
Ahmad 25000
8 rows selected.
10. Write a query to retrieve the names of all employees who do not have supervisors.
SQL> select fname from employee where superssn is null;
FNAME
---------------
James
2. Write a query to retrieve employee’s first and last name and first and last name of his
or her immediate supervisor.
SQL>select e1.fname,e1.lname,e2.fname "supervisor fname",e2.lname "supervisor lname" from
employee e1,employee e2 where e1.superssn=e2.ssn
OR
(USING OUTER JOIN )
SQL>select e1.fname ,e1.lname, e2.fname "supervisor fname", e2.lname "supervisor lname"
from employee e1,employee e2 where e1.superssn= e2.ssn(+)
NOTE: + is specified in above using ‘outer join’ to an attribute in join condition where we
don’t have null value.
Ex: ssn don’t have null value but superssn has. So, I gave (+) to ssn.
3. Find the sum of the salaries of all employees, the maximum salary, the minimum
salary and the average salary.
SQL> select sum(salary),max(salary),min(salary),avg(salary) from employee
4. Find the sum of the salaries of all employees, the maximum salary, the minimum
salary and the average salary of all employees of the ‘Research’ department.
SQL>select sum(salary),max(salary),min(salary),avg(salary) from employee e,department d
where d.dname='Research' and d.dnumber=e.dno
COUNT(*)
---------
4
6. For each department, retrieve the department number, the number of employees in the
department and their average salary.
7. For each project, retrieve the project number, Project name and the number of
employees who work on that project.
SQL>select pno,pname,count(pno) "Employees working" from project p,works_on w where
p.pnumber=w.pno group by pno,pname
6 rows selected.
8. For each project on which more than two employees work, retrieve the project number,
project name and the number of employees who work on the project.
SQL>select pno,pname,count(pno) from project p,works_on w where p.pnumber=w.pno group
by pno,pname having count(pno)>2
Nested queries
1. For each department that has more than two employees, retrieve the department
number and the number of its employees who are making more than or equal to
$30,000.
A . SQL> select dno,dname,count(*) from employee e,department d where
e.dno=d.dnumber and salary>=30000 and dno in( select e2.dno
from employee e2 group by e2.dno having count(*) >2) group by dno,dname
OUTPUT:
DNO DNAME COUNT(*)
---------- --------------- ----------
4 Administration 1
5 Research 3
2. Write a nested query to retrieve the names of employees who have no dependents.
SQL> select fname from employee e1 where e1.ssn not in(select essn from dependant d
where e1.ssn=d.essn)
OUTPUT:
FNAME
---------------
Alicia
Ramesh
Joyce
Ahmad
James
3. Write a nested query to list the names of managers who have at least one dependent.
A . SQL> select fname from employee,department where mgrssn=ssn and exists(
select * from dependant where mgrssn=essn)
OUTPUT:
FNAME
---------------
Franklin
Jennifer
4. Write a nested query to retrieve the names of all employees who have two or more
dependents.
A: SQL>
select fname from employee where ssn in( select essn from dependant where
essn=ssn group by(essn) having count(*)>2)
OUTPUT:
FNAME
---------------
John
Franklin
5. Write a nested query to retrieve the SSNs of all employees who work on project
number 1, 2 or 3.
A: SQL>
select ssn from employee where ssn in( select distinct essn from works_on
where pno in(1,2,3))
OUTPUT:
SSN
---------
123456789
333445555
453453453
666884444
NOTE: ‘distinct’ may or mayn’t be present
6. Write a nested query to retrieve the names of all employees whose salary is greater
than the salary of all the employees in department 5.
A: SQL> select fname from employee where salary > all (select salary from employee
where dno=5)
OUTPUT:
FNAME
---------------
Jennifer
James
SNO SNAME
---- ------
1 kumar
2 ravi
3 ramu
5 lalitha
9 devi
SNO SNAME
---- ------
1 kumar
2 ravi
3 ramu
5 lalitha
9 devi
3. SQL> SELECT * FROM student INTERSECT SELECT * FROM std;
SNO SNAME
---- -------
1 Kumar
SNO SNAME
---- -------
2 RAVI
3 RAMU