Sie sind auf Seite 1von 81

INTRODUCTION

What is database?
A database can be defined as a collection of coherent meaningful data.

What is database management system (DBMS)


To be able to successfully design and maintain database we have to do the following:Identify the objects, their attributes and the relationship between them are stored in the database that is design, build and populated with data for a specific purpose. A system that would help in managing data in such a database these systems were called Database Management Systems (DBMS).DBMS is a system that allows inserting, updating, deleting and processing of data. Benefits of DBMS 1. The amount of data redundancy is stored data can be reduced 2. No more data inconsistencies 3. Stored data can be shared by a single or multiple users 4. Data integrity can be maintained. Data integrity refers to the problem of ensuring that database contains only accurate data 5. Security of data can be simply implemented.

What is relational Database Management System (RDBMS)


A RDBMS that is based on the relational model. RDBMS stores data in the form of related tables. Introduction to structured Query language Structured Query language is a language that provides an interface to relational database systems. SQL is often pronounced sequel. SQL also encompasses DML for insert, update, delete and DDL used for creating and modifying tables and other database structures

Features of SQL
1. SQL can be used by a range of users, including those with little or no

programming experience 2. It is a non procedural language 3. It reduces the amount of time required for creating and maintaining system 4. It is English like language Rules for SQL 1. SQL starts with a verb eg: select statement. This verb may have additional adjectives eg: from 2. Each verb is followed by number of clauses Eg: from, where, having 3. A space separate clauses. Eg: drop table emp 4. A : is used to end SQL statement 5. A identifier can contain up to 30 characters and must start with an alphabetic characters 6. Characters and date literals must be enclosed with in single quotes 7. Comments may be enclosed between /* and */ symbols and may be multilane. Single line comments may be prefixed with a _ symbol Component of SQL
1. DDL(Data Defination Language)

It is a set SQL commands used to create, modify and delete database structure but not data. These commands are normally not used by a general user, who should be accessing the database via an application. They are normally used by the DBA to a limited extent, a database designer or application developer 2. DML(Data Manipulation Language) It is the area of SQL that allows changing data with in the database

3. DCL(Data Control Language) It is the component of SQL statement that controls access to data and to that database. DCL statement are grouped with DML statements 4. DQL(Data Query Language) It is the component of SQL statement that allows getting data from the database and imposing ordering upon it. It includes the select statement. This command is the heart of SQL. It allows getting the data out of the database perform operation with it. When a select is fired against a table or tables the result is compiled into a further temporary table which is displayed or perhaps received by the program i.e. front end

Examples of DDL, DML& DCL commands


DDL: Data Defination Language statement Create- to create object in database Alter- alters the structure of the database Drop- delete objects from database Truncate- removes all records from a tabular, including all space allocated for the records are removed Comment- add comments to the data dictionary Grant- gives users access privileges to database Revoke- withdraw access privileges given with the grant command DML: Data Manipulation Language
Insert- inserts data into a table

Update- updates existing data with in a table Delete- deletes all records from a table Call- call a PL/SQL or java program Lock- table control concurrency

DCL: Data Control Language


Commit- saves work done

Save point- identity a point in a transaction to which you can later rollback Rollback- restore database to original since the last commit DQL: Data Query Language
select- retrieve data from the data base

Basic Data Types numeric(p,s): The maximum precision is 38 digits. Where p is precision, s is sale number(p,s) for example number(7,2) is a number that has 5 digits before the decimal and 2 digit after the decimal char(size): Upto 3276 bytes in PL/SQL. Upto 2000 bytes where size is the number of characters to store. Fixed length strings space padded varchar2(size): Upto 4000 bytes, where size is the number of characters to store variable length strings date: A date between Jan 1,4712BC and Dec 31,9999AD The create table Command: The create table command defines each column of the table uniquely. Each column has a minimum of three attributes. Each table column definition is a separated from the other by a comma. Finally, the SQL statement is terminated with a semicolon.

Rules for creating tables

1. A name can have max upto 30 characters 2. Alphabet from A-Z, a-z and number from 0-9 allows 3. A name should begin with an alphabet 4. The use of the special character like _ is allowed and also recommended

5. SQL reserved words not allowed For eg: create, select and so on Syntax: create table<table name> (<column name1><data type>(<size>),<column name2><data type>(<size>));

Inserting Data into Tables:


Once a table is created, the most natural thing to do is load this table with data to be manipulated later. When inserting a single row of data into the table, the insert operation Create a new row in the database table Loads the values passed into the columns specified. Syntax: Insert into<table name>(<column name1>,<column name2>) values(<expression1>,<expression2>)

Viewing Data in the Table:


Once data has been inserted into a table, the next most logical operation would be to view what has been inserted. The selected SQL verb is used to achieve this. The select command is used to retrieve rows selected from one or more tables

All rows and columns


select<column name1>to<column name n>from tablename; Syntax: select * from <tablename>;

Selected columns and all rows


The retrieval of specific column from a table can be done as shown Syntax: Select<column name1>,<column name2>from <tablename>;

Selected rows and all columns


If information of a particular client is to be retrieved from a table, its retrieval must be based on a specific condition. Syntax: Select * from <tablename> where <condition>; Hence condition is always quantified as<column name=vales>

Selected columns and selected rows


To view a specific set of rows and columns from a table the syntax will be Syntax: Select <column name1>,<column name2> from <tablename> where <condition>;

Eliminating duplicate rows when using a select statement


A table could duplicate rows, in such a case, to view only unique rows the distinct clause can be used. The distinct clause allows removing duplicate from the result set. Syntax: Select distinct<columnname1>,<column name 2>from<tablename>; Select distinct*from<tablename>

Sorting data in a table


Oracle allows data from a table to be viewed in a sorted the rows retrieved from the table will be sorted in either ascending or descending on the condition specified in the selected sentence. The syntax is Syntax: Select * from<tablename> Order by <column name 1>,<column name 2><[sort order]>
6

Creating a table from a table


Syntax; Create table<tablename>(<column name>,<column name> as Select<column name1>,<column name2> from <tablename>;

Inserting data into a table from another table


Syntax: Insert into <tablename> Select<column name1>,<column anme2> from <tablename>;

Insertion of a data set into a table from another table


Syntax: Insert into <tablename> select <column name1>,<column name2> from <tablename> where <condition>;

Delete operations
Delete command deletes rows from the table that satisfies the condition provided by its where clause, and returns the number of records deleted. Delete in SQL is used to remove either All the rows from a table or A set of rows from a table

Removal of all rows


Syntax: delete from <tablename>;

Removal of specific rows


Syntax: delete from <tablename> where <condition>;

Updating the contents of a rows


The update commands is used to change or modify data values in a table

Updating all rows


The update statement updates columns in the existing table rows with new values. The set clause indicates which column data should be modified and the new values that they should hold. The where clause, if given specifies which rows should be updated. Otherwise, all table rows are updated. Syntax: update<tablename> Set<column name1>=<expression1>,<column name2>= <expression2>;

Updating records conditionally


Syntax: update<tablename> Set<column name1>=<expression1>,<column name2>= <expression2> where <condition>;

Modifying the structure of table


The structure of a table can be modified by using the alter table command. Alter table allows changing the structure of an existing table. With alter table it is possible to add or delete columns, create or destroy indexes, changes the data type of existing columns or rename column or the table itself.

Adding new columns


Syntax: alter table <tablename> Add(<new columnname><datatype>(<size>),<new columnname><datatype>(<size>));

Dropping a column from table


Syntax: alter table <tablename> Drop column<columnname>;

Modifying existing column


Syntax: alter table<tablename> Modify(<columnname><new datatype>(<new size>));

Restrictions on the alter table


The following tasks cannot be performed when using the alter table clause change the name of the table change the name of the column Decrease the size of a column if table data exists.

Renaming tables
Oracle allows renaming of tables. The rename operation is done automatically, which means that no other thread can access any of the tables while the rename process is running. Syntax: rename <tablename> to <newtablename>;

Truncating tables
Truncating table empties a table completely logically, this is equivalent to a delete statement that deletes all rows, but there are practical differences are under some circumstances. Truncate table differ from delete in the following ways Truncate operations drop and recreate the table, which is much faster than deleting rows one by one Truncate operations are not transaction safe The number of deleted rows is not returned. Syntax: truncate table<tablename>;

Destroying tables
If a table is dropped all records held within it are lost and cannot be recovered. Syntax: drop table<tablename>;

Examining objects created by a user


Finding out the tables created by a user The command shown below is used to determine the tables to which a user has access. The tables created under the currently selected table space are displayed Select * from tab;

Displaying the tables structure


To display information about the column defined in a table use the following syntax Syntax: desc <tablename>;

Data Constraints
To understand the concept of data constraints, several tables will be created and different types of constraints will be applied to table columns or the table itself.

Applying Data Constraints


Oracle permits data constraints to be attached to table columns via SQL syntax that checks data for integrity prior storage. Once data constraints are part of a table column construct, the oracle database engine checks the data being entered into a table column against the data constraints. If the data passes this check, it is stored in the table column, else the data is rejected. Even if a single column of the record is rejected and not stored in the table.

Types of data constraints


I/O Constraints
The input/output constraints are further divided into distinctly different constraints.

10

The primary key constraint:


A primary key is one or more column in a table used to uniquely identify each row in the table. None of the field that are part of the primary key can contain a null value. A table can have only one primary key. A primary key column in a table has special attributes It defines the column, as a mandatory column (i.e. the column cannot be left blank) The data held across the column must be unique A single column primary key is called a simple key. A multi column key is called composite primary key. The only function of a primary key in a table is to uniquely identify a row Features of a primary key Primary key will not allow duplicate key and null values Primary key constraints defined at column level Syntax: <columnname><datatype>(<size>)primary key At table level Syntax: primary key(<columnname>,<columnname>)

Foreign key (self reference) constraints


Foreign key represent relationship between tables. A foreign key is a column whose values are derived from the primary key of some other table. The key in which a foreign key is defined is called foreign or detailed table. Foreign key defined at the column level Syntax: <columnname><datatype>(<size>) References<tablename>[(<columnname>)] At the table level Syntax: foreign key(<columnname>[<columnname>]) Reference<tablename>[(<columnname>,<columnname>)
11

Unique key constraints


The unique column constraints permit multiple entries of NULL into the column. These null values are clubbed at the top of the column in the order in which they were entered into the table Unique constraints defined at the column level Syntax: <columnname><datatype>(<size>)unique At the table level Syntax: create table tablename(<columnname1><datatype>(<size>), <columnname2><datatype>(<size>), unique(<columnname1>,<columnname2>));

Check constraints
Variation can be applied to a table column by using check constraints. Check constraints defined at column level Syntax: <columnname><datatype>(<size>) check (<logical expression>) At the table level Syntax: check(<logical expression>)..

12

Simple Queries using DDL and DML commands


A. Change the salary of an employee where deptno is 10.
SQL> update emp set sal=4000 where deptno=10; 3 rows updated. SQL> select * from emp; 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-JUN-81 4000 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 4000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------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 4000 10

B. Change the city of the employee to Delhi where department is accounting.


SQL> update dept set loc='DELHI' where dname='ACCOUNTING'; 1 row updated. SQL> select * from dept;

13

DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING DELHI 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

C. Delete from employee where salary is less than 1500.


SQL> delete from emp where sal<1500; 6 rows deleted. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-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 1500 0 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20

D. Add a column called telephone of data type number & size=10 to the employee table.
SQL> alter table emp add telno number(10); Table altered. SQL> desc emp; Name Null? Type ----------------------------------------- -------- --------------EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) TELNO NUMBER(10)

14

E. Change the size of the salary column in employee table to (10,2).


SQL> alter table emp modify sal number(10,2); Table altered. SQL> desc emp; Name Null? Type ----------------------------------------------------- -------- -------------EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(10,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) TELNO NUMBER(10)

F. Destroy the table department along with all data.


SQL>drop table dept; table dropped.

G. Change the name of the emp table to employee master table.


SQL> rename emp to emp_master; Table renamed. SQL> select * from tab; TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------DEPT TABLE EMP_MASTER TABLE BONUS TABLE SALGRADE TABLE

Create a studentdetails table with fields rollno,name,address,date of joining.

15

SQL> create table studentdetails(rollno number(10),name char(25),address varchar2(50),date_of_joining date); Table created. SQL> insert into studentdetails values(&rollno,'&name','&address','&date_of_joining'); Enter value for rollno: 0309801 Enter value for name: abc Enter value for address: h.no:87287,colony,hyd Enter value for date_of_joining: 10-jul-08 old 1: insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') new 1: insert into studentdetails values(0309801,'abc','h.no:87287,colony,hyd','10-jul-08') 1 row created. SQL> ed Wrote file afiedt.buf 1* insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') 2 / Enter value for rollno: 0309823 Enter value for name: xyz Enter value for address: h.no:7832,nagar,hyd Enter value for date_of_joining: 02-sep-08 old 1: insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') new 1: insert into studentdetails values(0309823,'xyz','h.no:7832,nagar,hyd','02-sep-08') 1 row created. SQL> select * from studentdetails; ROLLNO NAME ADDRESS DATE_OF_J ---------- ------------------------- ------------------------- --------309801 abc h.no:87287,colony,hyd 10-JUL-08 309823 xyz h.no:7832,nagar,hyd 02-SEP-08

16

QUERIES
Q1. Describe the details of employee table.
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10

Q2. Describe the structure of the employee table.


SQL> desc emp; Name Null? Type ----------------------------------------- -------- ---------------------------EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2)

Q3. Display department Information from department table. 17

SQL> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

Q4. Display the name & salary of all employees.


SQL> select ename,sal from emp; ENAME SAL ---------- ---------KING 5000 BLAKE 2850 CLARK 2450 JONES 2975 MARTIN 1250 ALLEN 1600 TURNER 1500 JAMES 950 WARD 1250 FORD 3000 SMITH 800 ENAME SAL ---------- ---------SCOTT 3000 ADAMS 1100 MILLER 1300

Q5. Display name & job for all employees.


SQL> select ename,job from emp; ENAME JOB ---------- --------KING PRESIDENT BLAKE MANAGER CLARK MANAGER JONES MANAGER MARTIN SALESMAN ALLEN SALESMAN TURNER SALESMAN JAMES CLERK WARD SALESMAN FORD ANALYST SMITH CLERK

18

ENAME JOB ---------- --------SCOTT ANALYST ADAMS CLERK MILLER CLERK

Q6.Display the names of employees who are working as clerk.


SQL> select ename from emp where job='CLERK'; ENAME ---------JAMES SMITH ADAMS MILLER

Q7. Display name of employees who earn commission.


SQL> select ename from emp where comm>=0; ENAME ---------MARTIN ALLEN TURNER WARD

Q8. Display the name of employee who doesnt earn commission.


SQL> select ename from emp where comm is null or comm=0; ENAME ---------KING BLAKE CLARK JONES TURNER JAMES FORD SMITH SCOTT ADAMS MILLER

19

Q9. Display the name of all employees who are working under deptno 10.
SQL> select ename from emp where deptno=10; ENAME ---------KING CLARK MILLER

Q10. Describe the structure of dept table.


SQL> desc dept; Name Null? Type ----------------------------------------------------- -------- -----------DEPTNO NOT NULL NUMBER(2) MARTIN ALLEN WARD

Q11. Display all the tables present in the database.


SQL> select * from tab; TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------STUDENTINFO TABLE EMPLOYEINFO TABLE DEPARTMENTINFO TABLE PAGESLIP TABLE DEPT TABLE EMP TABLE BONUS TABLE SALGRADE TABLE DUMMY TABLE CUSTOMER TABLE ORD TABLE TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------ITEM TABLE PRODUCT TABLE PRICE TABLE STUDENT8024 TABLE STUDENT8033 TABLE STUDENT8071 TABLE STUDENT8075 TABLE

20

STUDENT8083 STUDENT8085 STD24 EMP8033

TABLE TABLE TABLE TABLE

TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------EMPLOYE007 TABLE ENROLL TABLE ENROLL8033 TABLE ENROLL8080 TABLE EMPLOYEE007 TABLE ENROLL8012 TABLE DEPART8082 TABLE DEPT8033 TABLE EMP28 TABLE EMP8027 TABLE BIN$7lmfGzcKQgGyLNZo/xTsWg==$0 TABLE

Q12.Display all details of department table.


SQL> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

Q13. Display employee numbers, ename, hire date, salary such that employee number appears first.
SQL> select empno,ename,hiredate,sal from emp; EMPNO ENAME HIREDATE ---------- ---------- --------- ---------7839 KING 17-NOV-81 7698 BLAKE 01-MAY-81 7782 CLARK 09-JUN-81 7566 JONES 02-APR-81 7654 MARTIN 28-SEP-81 7499 ALLEN 20-FEB-81 7844 TURNER 08-SEP-81 7900 JAMES 03-DEC-81 7521 WARD 22-FEB-81 7902 FORD 03-DEC-81 7369 SMITH 17-DEC-80 EMPNO ENAME HIREDATE ---------- ---------- --------- ---------SAL 5000 2850 2450 2975 1250 1600 1500 950 1250 3000 800 SAL

21

7788 SCOTT 7876 ADAMS 7934 MILLER

09-DEC-82 12-JAN-83 23-JAN-82

3000 1100 1300

Q14. Display ename from employee table.


SQL> select ename from emp; ENAME ---------KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH ENAME ---------SCOTT ADAMS MILLER

Q15.Display the original salary increased by 400 and display both.


SQL> select ename,sal,sal+400 from emp; ENAME SAL SAL+400

---------- ---------- ---------KING 5000 5400 BLAKE 2850 3250 CLARK 2450 2850 JONES 2975 3375 MARTIN 1250 1650 ALLEN 1600 2000 TURNER 1500 1900 JAMES 950 1350 WARD 1250 1650 FORD 3000 3400 SMITH 800 1200 ENAME SAL SAL+400 ---------- ---------- ---------SCOTT 3000 3400 ADAMS 1100 1500 MILLER 1300 1700

22

Q16. Display the name as last name, multiply the salary by 12 and display annual salary from emp table.
SQL> select ename as LASTNAME,sal,sal*12 as annualsalary from emp; LASTNAME SAL annualsalary ---------- ---------- ---------KING 5000 60000 BLAKE 2850 34200 CLARK 2450 29400 JONES 2975 35700 MARTIN 1250 15000 ALLEN 1600 19200 TURNER 1500 18000 JAMES 950 11400 WARD 1250 15000 FORD 3000 36000 SMITH 800 9600 LASTNAME SAL annualsalary ---------- ---------- ---------SCOTT 3000 36000 ADAMS 1100 13200 MILLER 1300 15600

Q17. Display empno,ename,job,for all employee provided it starting date as hiredate.


SQL> select empno,ename,job,hiredate as startdate from emp; EMPNO ENAME JOB STARTDATE ---------- ---------- --------- --------7839 KING PRESIDENT 17-NOV-81 7698 BLAKE MANAGER 01-MAY-81 7782 CLARK MANAGER 09-JUN-81 7566 JONES MANAGER 02-APR-81 7654 MARTIN SALESMAN 28-SEP-81 7499 ALLEN SALESMAN 20-FEB-81 7844 TURNER SALESMAN 08-SEP-81 7900 JAMES CLERK 03-DEC-81 7521 WARD SALESMAN 22-FEB-81 7902 FORD ANALYST 03-DEC-81 7369 SMITH CLERK 17-DEC-80 EMPNO ENAME JOB STARTDATE ---------- ---------- --------- --------7788 SCOTT ANALYST 09-DEC-82 7876 ADAMS CLERK 12-JAN-83 7934 MILLER CLERK 23-JAN-82

23

Q18. Combine the lastname with job as employee details from emp table.
SQL>select ename||job as employeedetails from emp EMPLOYEEDETAILS ------------------KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER MARTINSALESMAN ALLENSALESMAN TURNERSALESMAN JAMESCLERK WARDSALESMAN FORDANALYST SMITHCLERK EMPLOYEEDETAILS ------------------SCOTTANALYST ADAMSCLERK MILLERCLERK

Q19. Display the lastname with job reported by 'is a' and name as employee details.
SQL>select ename||' is a '||job as employeedetails from emp;

EMPLOYEEDETAILS ------------------------KING is a PRESIDENT BLAKE is a MANAGER CLARK is a MANAGER JONES is a MANAGER MARTIN is a SALESMAN ALLEN is a SALESMAN TURNER is a SALESMAN JAMES is a CLERK WARD is a SALESMAN FORD is a ANALYST SMITH is a CLERK EMPLOYEEDETAILS ------------------------SCOTT is a ANALYST

24

ADAMS is a CLERK MILLER is a CLERK

Q20. Display the unique job from unique table.


SQL> select job from emp union select job from emp; JOB --------ANALYST CLERK MANAGER PRESIDENT SALESMAN

Q21. Display ename from emp table whose department number is 30.
SQL> select ename,deptno from emp where deptno='30'; ename DEPTNO ---------- ---------BLAKE 30 MARTIN 30 ALLEN 30 TURNER 30 JAMES 30 WARD 30

Q22. Display all details of manager from emp.


SQL> select ename,job from emp where job='MANAGER'; ename JOB ---------- --------BLAKE MANAGER CLARK MANAGER JONES MANAGER

Q23. Display ename as LASTNAME from emp where deptno is 10.


SQL> select ename as LASTNAME from emp where deptno=10; ename ---------KING CLARK MILLER

25

Q24. Display LASTNAME and salary of employee earning more than 1000.
SQL> select ename as LASTNAME,sal from emp where sal>1000; LASTNAME SAL ---------- -------------------- ---------KING 5000 BLAKE 2850 CLARK 2450 JONES 2975 MARTIN 1250 ALLEN 1600 TURNER 1500 WARD 1250 FORD 3000 SCOTT 3000 ADAMS 1100 LASTNAME SAL ---------- ---------MILLER 1300

Q25. Display ename as lastname and deptno from emp where empno=7934.
SQL> select ename as LASTNAME,deptno from emp where empno=7934; LASTNAME DEPTNO ---------- ---------MILLER 10

Q26. Display LASTNAME,sal from emp whose sal is in between 2500 and 3500.
SQL> select LASTNAME,sal from emp where sal between 2500 and 3500; LASTNAME SAL ---------- ---------BLAKE 2850 JONES 2975 FORD 3000 SCOTT 3000

Q27. Display empno,LASTNAME,job where empno is 7839 and 7698.

26

SQL> select empno,ename as lastname,job from emp where empno=any(7839,7698); EMPNO LASTNAME JOB ---------- ---------- --------7698 BLAKE MANAGER 7839 KING PRESIDENT

Q28.Display the name of employee who are not manager.


SQL> select ename from emp where job!='MANAGER'; ename ---------KING MARTIN ALLEN TURNER JAMES WARD FORD SMITH SCOTT ADAMS MILLER

Q29. List the name and job of employee who does not report to manager.
SQL> select ename,job from emp where job! =any('CLERK','ANALYST','MANAGER','SALESMAN'); ename JOB ---------- --------KING PRESIDENT

Q30.Display the original salary and salary increased by 10% of employees.


SQL> select ename,sal,sal+sal*0.1 "salaryincreased by 10%" from emp; ename SAL salaryincreased by 10% ---------- ---------- -----------KING 5000 5500 BLAKE 2850 3135 CLARK 2450 2695 JONES 2975 3272.5 MARTIN 1250 1375 ALLEN 1600 1760 TURNER 1500 1650

27

JAMES WARD ford SMITH

950 1250 3000 800

1045 1375 3300 880

Q31. List the name of analyst & salesman.


SQL> select ename,job from emp where job='ANALYST' or job='SALESMAN'; ENAME JOB ---------- --------MARTIN SALESMAN ALLEN SALESMAN TURNER SALESMAN WARD SALESMAN FORD ANALYST SCOTT ANALYST

Q32. Display the names of employee working in deptno 30 or 40 or 50 & working as a clerk.
SQL>select ename from emp where deptno=any(30,40,50) and job='CLERK'; ENAME ---------JAMES

Q33. Display the names of employee working in deptno 30 or 40 or50 0r working as a clerk.
SQL> select ename from emp where deptno=all(30,40,50) or job='CLERK'; ENAME ---------JAMES SMITH ADAMS MILLER

Q34. List the employee details not belonging to dept no 10 or 20.


SQL> select ename,deptno from emp where deptno<>'10' and deptno<>'20'; (or) select ename,deptno from emp where deptno!=all(10,20); ENAME DEPTNO ---------- ---------BLAKE 30

28

MARTIN ALLEN TURNER JAMES WARD

30 30 30 30 30

Q35. Display the names of employee whose name start with s.


SQL> select ename from emp where ename like 'S%'; ENAME ---------SMITH SCOTT

Q36. Dispaly the names of employee whose name end with s.


SQL> select ename from emp where ename like '%S'; ENAME ---------JONES JAMES ADAMS

Q37. Display the employee no,employee name and salary in ascending order of salary.
SQL> select ename,empno,sal from emp order by sal; ENAME EMPNO SAL ---------- ---------- ---------SMITH 7369 800 JAMES 7900 950 ADAMS 7876 1100 WARD 7521 1250 MARTIN 7654 1250 MILLER 7934 1300 TURNER 7844 1500 ALLEN 7499 1600 CLARK 7782 2450 BLAKE 7698 2850 JONES 7566 2975 ENAME EMPNO SAL ---------- ---------- ---------FORD 7902 3000 SCOTT 7788 3000

29

KING

7839

5000

Q38. Display the name and hire date in ascending order of hire data.
SQL> select ename,hiredate from emp order by hiredate; ENAME HIREDATE ---------- --------SMITH 17-DEC-80 ALLEN 20-FEB-81 WARD 22-FEB-81 JONES 02-APR-81 BLAKE 01-MAY-81 CLARK 09-JUN-81 TURNER 08-SEP-81 MARTIN 28-SEP-81 KING 17-NOV-81 JAMES 03-DEC-81 FORD 03-DEC-81 ENAME HIREDATE ---------- --------MILLER 23-JAN-82 SCOTT 09-DEC-82 ADAMS 12-JAN-83

Q39. Display ename ,job ,startdate of employee who's hiredate in range of 20-feb and 1-mar-81.
SQL> select ename,sal,job,hiredate as startdate from emp where hiredate between '20-FEB-81' and '1-MAR-81'; ENAME SAL JOB startdate ---------- ---------- --------- --------ALLEN 1600 SALESMAN 20-FEB-81 WARD 1250 SALESMAN 22-FEB-81

Q40. Display ename & hiredate of every employee who was hired in 1982.
SQL> select ename,hiredate from emp where hiredate between '01-jan-82' and '31-dec-82'; ENAME HIREDATE ---------- --------SCOTT 09-DEC-82 MILLER 23-JAN-82

30

Q41. Display the name of employee whose 3rd char is 'A'.


SQL> select ename from emp where ename like '__A%'; ENAME ---------BLAKE CLARK ADAMS

Q42. Display ename,deptno,location for all employee who earn commission.


SQL> select emp.ename,emp.deptno,dept.loc from emp,dept where emp.deptno=dept.deptno; ENAME DEPTNO LOC ---------- ---------- ------------KING 10 NEW YORK BLAKE 30 CHICAGO CLARK 10 NEW YORK JONES 20 DALLAS MARTIN 30 CHICAGO ALLEN 30 CHICAGO TURNER 30 CHICAGO JAMES 30 CHICAGO WARD 30 CHICAGO FORD 20 DALLAS SMITH 20 DALLAS ENAME DEPTNO LOC ---------- ---------- ------------SCOTT 20 DALLAS ADAMS 20 DALLAS MILLER 10 NEW YORK

Q43. Display ename,deptno for all employee who have 'A' in their name.
SQL> select ename,deptno from emp where ename like '%%A%%'; ENAME DEPTNO ---------- ---------BLAKE 30 CLARK 10 MARTIN 30 ALLEN 30 JAMES 30 WARD 30 ADAMS 20

31

Q44. Display ename,empno along with their manager no and name.


SQL> select w.empno,w.ename,w.mgr,m.empno,m.ename from emp w,emp m where w.mgr=m.empno; EMPNO ENAME MGR ---------- ---------- ---------- ---------7698 BLAKE 7839 7782 CLARK 7839 7566 JONES 7839 7654 MARTIN 7698 7499 ALLEN 7698 7844 TURNER 7698 7900 JAMES 7698 7521 WARD 7698 7902 FORD 7566 7369 SMITH 7902 7788 SCOTT 7566 EMPNO ENAME ---------7839 KING 7839 KING 7839 KING 7698 BLAKE 7698 BLAKE 7698 BLAKE 7698 BLAKE 7698 BLAKE 7566 JONES 7902 FORD 7566 JONES

EMPNO ENAME MGR EMPNO ENAME ---------- ---------- ---------- ---------- ---------7876 ADAMS 7788 7788 SCOTT 7934 MILLER 7782 7782 CLARK

Q45. Display the name of employee who are working as salesman clerk & analyst.
SQL> select ename from emp where job in ('ANALYST','SALESMAN','CLERK'); ENAME ---------MARTIN ALLEN TURNER JAMES WARD FORD SMITH SCOTT ADAMS MILLER

Q46. Display the total no of employees working in the company.


SQL> select count(ename) from emp; COUNT(ENAME) ------------

32

14

Q47. Display total salary paid to all employees working in the company.
SQL> select sum(sal) from emp; SUM(SAL) ---------29025

Q48. Display average salary from emp table.


SQL> select avg(sal) from emp; AVG(SAL) ---------2073.21429

Q49. Display maximum salary paid to clerk.


SQL> select max(sal) from emp where job='CLERK'; MAX(SAL) ---------1300

Q50. Display total salary drawn by analyst, whose department no is 20.


SQL> select sum(sal) from emp where job='ANALYST' and deptno=20; SUM(SAL) ---------6000

Q51. Display ename from emp table in order of salary.


SQL> select ename,sal from emp order by sal; ENAME SAL ---------- ---------SMITH 800 JAMES 950 ADAMS 1100

33

WARD MARTIN MILLER TURNER ALLEN CLARK BLAKE JONES

1250 1250 1300 1500 1600 2450 2850 2975

ENAME SAL ---------- ---------SCOTT 3000 FORD 3000 KING 5000

Q52. Display minimum salary paid to clerk.


SQL> select min(sal) from emp where job='CLERK'; MIN(SAL) ---------800

Q53. display ename from emp in decending order of salary.


SQL> select ename from emp order by sal desc; ENAME ---------KING SCOTT FORD JONES BLAKE CLARK ALLEN TURNER MILLER WARD MARTIN ENAME ---------ADAMS JAMES SMITH

Q54. Display details of employee in order of empno.


SQL> select * from emp order by empno;

34

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-JUN-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 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------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

Q55. Display empno ename depno salary sorted their output in order of name deptno salary.
SQL> select ename,empno,deptno from emp order by ename,deptno,sal;

ENAME EMPNO DEPTNO ---------- ---------- ---------ADAMS 7876 20 ALLEN 7499 30 BLAKE 7698 30 CLARK 7782 10 FORD 7902 20 JAMES 7900 30 JONES 7566 20 KING 7839 10 MARTIN 7654 30 MILLER 7934 10 SCOTT 7788 20 ENAME EMPNO DEPTNO ---------- ---------- ---------SMITH 7369 20 TURNER 7844 30 WARD 7521 30

35

Q56. Display ename along with their annual salary, highest salary should come first.
SQL> select ename,sal*12 as annualsalary from emp order by sal desc; ENAME ANNUALSALARY ---------- -----------KING 60000 SCOTT 36000 FORD 36000 JONES 35700 BLAKE 34200 CLARK 29400 ALLEN 19200 TURNER 18000 MILLER 15600 WARD 15000 MARTIN 15000 ENAME ANNUALSALARY ---------- -----------ADAMS 13200 JAMES 11400 SMITH 9600

Q57. Display deptno,total no of emp working in each dept.


SQL> select deptno,count(deptno) as totalemployees from emp group by deptno; DEPTNO TOTALEMPLOYEES ---------- -------------30 6 20 5 10 3

Q58. Display the various jobs and total no of employees in each job.
SQL> select job,count(job) from emp group by job; JOB COUNT(JOB) --------- ---------SALESMAN 4 CLERK 4 PRESIDENT 1 MANAGER 3 ANALYST 2

36

Q59. Display the deptno,total salary for each dept.


SQL> select deptno,sum(sal) as total from emp group by deptno; DEPTNO TOTAL ---------- ---------30 9400 20 10875 10 8750

Q60. Display each job along with min salary, paid in each job.
SQL> select job,min(sal) as minimumsalary from emp group by job; JOB MINIMUMSALARY --------- ------------SALESMAN 1250 CLERK 800 PRESIDENT 5000 MANAGER 2450 ANALYST 3000

Q61. Display the deptno,if morethan a 3 employees are working in each department.
SQL> select deptno from emp having count(deptno)>3 group by deptno; DEPTNO ---------30 20

Q62. Display the various jobs along with total salary for each job and group by total salary more than 4000.
SQL> select job,sum(sal) as totalsalary from emp having sum(sal)>4000 group by job; JOB TOTALSALARY --------- ----------SALESMAN 5600 CLERK 4150 PRESIDENT 5000

37

MANAGER ANALYST

8275 6000

Q63. Display ename, who is working in accounting dept.


SQL> select emp.ename,dept.dname from emp ,dept where emp.deptno=dept.deptno and dept.dname='ACCOUNT ING'; ENAME DNAME ---------- -------------KING ACCOUNTING CLARK ACCOUNTING MILLER ACCOUNTING

Q64. Display the ename who is working in Chicago.


SQL> select emp.ename, dept.loc from emp,dept where emp.deptno=dept.deptno and dept.loc='CHICAGO; ENAME LOC ---------- ------------BLAKE CHICAGO MARTIN CHICAGO ALLEN CHICAGO TURNER CHICAGO JAMES CHICAGO WARD CHICAGO

Q65. Display the ename in upper case.


SQL> select upper(ename) from emp; UPPER(ENAME) ---------KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH UPPER(ENAME)

38

---------SCOTT ADAMS MILLER

Q66. Display the ename in lower case.


SQL> select lower(ename) from emp; LOWER(ENAME) ---------king blake clark jones martin allen turner james ward ford smith LOWER(ENAME) ---------scott adams miller

Q67. Display ename in proper case.


SQL> select initcap(ename) from emp; INITCAP(ENAME) ---------King Blake Clark Jones Martin Allen Turner James Ward Ford Smith INITCAP(ENAME) ---------Scott Adams

39

Miller

Q68. Find the length of your name.


SQL> select length('ikram') from dual;

LENGTH('IKRAM') ---------------5

Q69. Find the length of all employees in emp table.


SQL> select ename,length(ENAME) from emp; ENAME LENGTH(ENAME) ---------- ------------KING 4 BLAKE 5 CLARK 5 JONES 5 MARTIN 6 ALLEN 5 TURNER 6 JAMES 5 WARD 4 FORD 4 SMITH 5 ENAME LENGTH(ENAME) ---------- ------------SCOTT 5 ADAMS 5 MILLER 6

Q70. Using appropriate function and extract the second character from string DCET.
SQL> select substr('DCET',2,2) from dual; SU -CE

Q71. Replace every appearance of alphabet a to b in a string Allens.


SQL> select translate('ALLENS','A','B') from dual;

40

TRANSL -----BLLENS

Q72. Display information from emp table whenever job as a manager is called it should be display as boss.
SQL> select replace(job,'MANAGER','BOSS') from emp; REPLACE(JOB,'MANAGER','BOSS') -----------------------------------PRESIDENT BOSS BOSS BOSS SALESMAN SALESMAN SALESMAN CLERK SALESMAN ANALYST CLERK REPLACE(JOB,'MANAGER','BOSS') -----------------------------------ANALYST CLERK CLERK

Q73. Display your age in days.


SQL>select trunc(sysdate-to_date('31-jul-1990')) from dual; TRUNC(SYSDATE-TO_DATE('31-JUL-1990')) ------------------------------------7364

Q74. Display your age in months.


SQL> select months_between(sysdate,'31-jul-1990') from dual; MONTHS_BETWEEN(SYSDATE,'31-JUL-1990') ------------------------------------241.919384

41

Q75. Find the day of the nearest Saturday form the current day.
SQL> select next_day(sysdate,'SATURDAY') from dual; NEXT_DAY( --------02-OCT-10

Q76. Display the date three months before the current day.
SQL> select add_months(sysdate,-3)from dual; ADD_MONTH --------21-JUN-10

Q77. Display those employ whose name contain not less than the four character.
SQL> select ename from emp where length(ename )>4; ENAME ---------BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES SMITH SCOTT ADAMS MILLER

Q78. Display the department name where atleast two employees are working.
SQL> select ename,dept.deptno,dname from emp,dept where emp.deptno=dept.deptno and emp.deptno in(select deptno from emp group by deptno having count(deptno)>2); ENAME DEPTNO DNAME ---------- ---------- -------------WARD 30 SALES JAMES 30 SALES TURNER 30 SALES ALLEN 30 SALES MARTIN 30 SALES

42

BLAKE ADAMS SCOTT SMITH FORD JONES

30 SALES 20 RESEARCH 20 RESEARCH 20 RESEARCH 20 RESEARCH 20 RESEARCH

ENAME DEPTNO DNAME ---------- ---------- -------------MILLER 10 ACCOUNTING CLARK 10 ACCOUNTING KING 10 ACCOUNTING

Q79. Display the names of those manager whose salary should be greater than the average salary of the company.
SQL> select ename,sal from emp where job='MANAGER' and sal>(select avg(sal) from emp); ENAME SAL ---------- ---------BLAKE 2850 CLARK 2450 JONES 2975

Q80. Display those employees whose salary is even number.


SQL> select ename from emp where mod(sal,2)=0; ENAME ---------KING BLAKE CLARK MARTIN ALLEN TURNER JAMES WARD FORD SMITH SCOTT ENAME ---------ADAMS MILLER

Q81. Display the names of employee who earn highest salary.

43

SQL> select ename from emp where sal=(select max(sal) from emp); ENAME ---------KING

Q82. Display the ename ,employee number ,job of all employees who is working as a clerk and earning highest salary among clerk.
SQL> select ename,empno,job from emp where job='CLERK' and sal=(select max(sal) from emp group by job having job='CLERK'); ENAME EMPNO JOB ---------- ---------- --------MILLER 7934 CLERK

Q83. Display the names of employee a who earns the salary more than the john and Scott.
SQL> select ename,sal from emp where sal>any(select sal from emp where ename='SCOTT' or ename='JONES'); ENAME SAL ---------- ---------KING 5000 SCOTT 3000 FORD 3000

Q84. Display the name who earn the highest salary in there respective dept.
SQL> select ename,sal,deptno from emp where sal in(select max(sal) from emp group by deptno) ENAME SAL DEPTNO ---------- ---------- ---------BLAKE 2850 30 SCOTT 3000 20 FORD 3000 20 KING 5000 10

Q85. Display the names of employees who earns highest salary in there respective jobs.

44

SQL> select ename,sal,deptno from emp where sal in(select max(sal) from emp group by job) ENAME SAL DEPTNO ---------- ---------- ---------ALLEN 1600 30 MILLER 1300 10 KING 5000 10 JONES 2975 20 SCOTT 3000 20 FORD 3000 20

Q86. Display the second highest salary.


SQL> select max(sal) from emp where sal<(select max(sal) from emp); MAX(SAL) ---------3000

Q87. Display the third highest salary.


SQL> select max(sal) from emp where sal <(select max(sal) from emp where sal <(select max(sal) from emp)) MAX(SAL) ---------2975

Q88. Display the fourth highest salary.


SQL> select max(sal) from emp where sal <(select max(sal) from emp where sal <(select max(sal) from emp where sal <(select max(sal) from emp))); MAX(SAL) ---------2850

Q89. list all the enames ,job and salary, dname in company except the clerk.
SQL> select emp.ename,emp.job,emp.sal,dept.dname from emp,dept where dept.deptno=emp.deptno and job! ='CLERK';

45

ENAME JOB SAL DNAME ---------- --------- ---------- -------------CLARK MANAGER 2450 ACCOUNTING KING PRESIDENT 5000 ACCOUNTING JONES MANAGER 2975 RESEARCH FORD ANALYST 3000 RESEARCH SCOTT ANALYST 3000 RESEARCH ALLEN SALESMAN 1600 SALES TURNER SALESMAN 1500 SALES MARTIN SALESMAN 1250 SALES BLAKE MANAGER 2850 SALES WARD SALESMAN 1250 SALES

Q90. Display the current day.


SQL> select trunc(sysdate) from dual; TRUNC(SYS --------28-SEP-10

Q91. Display the common job between dept no 10 and 20.


SQL> select job from emp where deptno=10 intersect select job from emp where deptno=20; JOB --------CLERK MANAGER

Q92. Display the job which are unique to dept no 10.


SQL> select unique job from emp where deptno=10; JOB --------CLERK PRESIDENT MANAGER

Q93. Display those employees whose salary is more than 3000 after giving 20% rise.
SQL> select ename,sal+sal*0.2 from emp where sal+sal*0.2>3000;

46

ENAME SAL+SAL*0.2 ---------- ----------KING 6000 BLAKE 3420 JONES 3570 FORD 3600 SCOTT 3600

Q94. Display the date in the following format


A) jan-31-90 b) 01/03/90 c) 30th October 1990

A) SQL> select to_char(to_date('jan 31 90','mon dd yy')) from dual; TO_CHAR(T --------31-JAN-90 B) SQL> select to_char(to_date('01-mar-90'),'dd/mm/yy') from dual; TO_CHAR( -------01/03/90 c) SQL> select to_char(to_date('30-october-1990'),'ddth monthyyyy') from dual; TO_CHAR(TO_DATE(' ----------------30th october 1990

Q95. Find 2 to the power of 3.


SQL> select power(2,3) from dual; POWER(2,3) ---------8

Q96.Display all rows from emp table, that should wait after every screen full of information.
SQL> set pause on SQL> select * from emp;

47

EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO ---------- ---------- --------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 01-MAY-81 2850 30 7782 CLARK MANAGER 09-JUN-81 2450 10 7566 JONES MANAGER 02-APR-81 2975 20 7654 MARTIN SALESMAN 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 08-SEP-81 1500 0 30 7900 JAMES CLERK 03-DEC-81 950 30 7521 WARD SALESMAN 22-FEB-81 1250 500 30 7902 FORD ANALYST 03-DEC-81 3000 20 7369 SMITH CLERK 17-DEC-80 800 20 EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO ---------- ---------- --------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 09-DEC-82 3000 20 7876 ADAMS CLERK 12-JAN-83 1100 20 7934 MILLER CLERK 23-JAN-82 1300 10

Q97. Display those employees whose salary is in odd number.


SQL> select ename from emp where mod(sal,2)!=0; ENAME ---------JONES

Q98. Display ename,sal,hra,pf,da and total sal for each employee. the output should be in the order of total salary (hra=15% of sal,da=10% of sal,pf=5% of sal, total=sal+hra+da-pf.
SQL> select ename,sal,sal*0.15 as HRA,sal*0.05 as PF,sal*0.1 as DA,sal+ (sal*0.15)+(sal*0.05)+(sal*0. 1) as TOTAL from emp; ENAME SAL HRA PF ---------- ---------- ---------- ---------- ---------KING 5000 750 250 BLAKE 2850 427.5 142.5 CLARK 2450 367.5 122.5 JONES 2975 446.25 148.75 MARTIN 1250 187.5 62.5 ALLEN 1600 240 80 TURNER 1500 225 75 JAMES 950 142.5 47.5 WARD 1250 187.5 62.5 FORD 3000 450 150 SMITH 800 120 40 DA TOTAL ---------500 6500 285 3705 245 3185 297.5 3867.5 125 1625 160 2080 150 1950 95 1235 125 1625 300 3900 80 1040

48

ENAME SAL HRA PF DA TOTAL ---------- ---------- ---------- ---------- ---------- ---------SCOTT 3000 450 150 300 3900 ADAMS 1100 165 55 110 1430 MILLER 1300 195 65 130 1690

Q99.Create a employee view with fields empno,ename,sal,deptno.


SQL> create view vemp as (select empno,ename,sal,deptno from emp);

*Create the table as client master with fields Columnname datatype size default attribute Clientno varchar2 6 primary key/ First letter must start with c not
49

NULL Name Address City varchar2 varchar2 varchar2 20 30 15

SQL> create table client_mast89(clientno varchar2(6) primary key check(clientno like c%), 2 name varchar2(20) NOT NULL, 3 address varchar2(30), 4 city varchar2(15)); Table created.

*Create the table with product master Column datatype size default Productno varchar2 6 description profit_percent sellprice costprice varchar2 number number number 15 4,2 8,2 8,2

attribute primary key/ first letter must start with p NOTNULL NOTNULL NOTNULL NOTNULL

SQL> create table product_mast89(productno varchar2(6) primary key check(productno like p%), 2 description varchar2(15) NOT NULL, 3 profit_percent number(4,2) NOT NULL, 4 sellprice number(8,2) NOT NULL check(sellprice<>0), 5 costprice number(8,2) NOT NULL check(costprice<>0)); Table created.

*Create table sales_order Column datatype size default


50

attribute

Orderno varchar2 Clientno varchar2 Orderdate date Delyaddr varchar2 Delydate date Order status varchar2

6 6 6 6 6 10

primary key/first letter start with 0 foreign key client no. of client _mast NOT NULL values(in proces ,fullfilled,back Order,cancel)

SQL> create table sales_order34(orderno varchar2(6) primary key check(orderno like o%), clientno varchar2(6), orderdate date NOT NULL, delydate date, delyaddr varchar2(20), orderstatus varchar2(10) check(orderstatus in (inprocess,fullfilled,backorder,cancelled)),foreign key(clientno) references client_mast89(clientno));

PL/SQL PROGRAMS
Program to find even or odd
SQL> set serveroutput on 1 declare 2 num number; 3 begin 4 num:=&num; 5 if(mod(num,2)=0) then 6 dbms_output.put_line('given num is even no'); 7 else 8 dbms_output.put_line('given num is odd no'); 9 end if; 10* end;

51

11 / Enter value for num: 8 old 4: num:=&num; new 4: num:=8; given num is even no PL/SQL procedure successfully completed.

Program for fibonacci series


1 declare 2 a number; 3 b number; 4 c number; 5 x number; 6 i number; 7 begin 8 a:=&a; 9 b:=0; 10 c:=1; 11 i:=3; 12 dbms_output.put_line(b); 13 dbms_output.put_line(c); 14 while i<=a loop 15 x:=b+c; 16 dbms_output.put_line(x); 17 b:=c; 18 c:=x; 19 i:=i+1; 20 end loop; 21* end; 22 / Enter value for a: 5 old 8: a:=&a; new 8: a:=5; 0 1 1 2 3 52

PL/SQL procedure successfully completed.

Program to find the factorial of a number


1 declare 2 n number; 3 i number; 4 begin 5 n:=&n; 6 i:=n-1; 7 loop 8 n:=n*i; 9 i:=i-1; 10 exit when i=1; 11 end loop; 12 dbms_output.put_line('fact of given no is:'); 13 dbms_output.put_line(n); 14* end; 15 / Enter value for n: 5 old 5: n:=&n; new 5: n:=5; fact of given no is: 120 PL/SQL procedure successfully completed.

Program for finding prime number


SQL> declare 2 n number;

53

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

i number(2); begin n:=&n; i:=n/2; loop if(mod(n,i)=0) then dbms_output.put_line('given num is not prime'); exit; else dbms_output.put_line('given num is prime'); exit; end if; i:=i-1; exit when i=2; end loop; end; /

Enter value for n: 5 old 5: n:=&n; new 5: n:=5; PL/SQL procedure successfully completed.

Program for multipliaction


1 declare 2 n number; 3 i number:=1; 4 a number; 5 begin 6 n:=&n; 7 loop 8 a:=n*i; 9 dbms_output.put_line(n||'*'||i||'='||a); 10 i:=i+1; 11 exit when i=11; 12 end loop; 13* end; 14 / Enter value for n: 5

54

old 6: n:=&n; new 6: n:=5; 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50 PL/SQL procedure successfully completed.

55

FUNCTIONS
Program to convert given currency into dollar
SQL> create or replace function doll(d number) return number is 2 begin 3 return(d*39.91); 4 end; 5 / Function created. SQL> select doll(12) from dual; DOLL(12) ---------478.92

Program to determine temperature in fahrenheit


1 create or replace function fah(f number) return number is 2 c number; 3 begin 4 c:=5/9*(f-32); 5 return(c); 6* end; 7 / Function created. SQL> select fah(32) from dual; FAH(32) ---------0

Program to find a number is prime or not

56

1 create or replace function pri(n number) return varchar2 is 2 b varchar2(20):='prime'; 3 begin 4 for i in 2..n-1 loop 5 if(mod(n,i)=0) then 6 b:='not prime'; 7 end if; 8 end loop; 9 return(b); 10* end; 11 / Function created. SQL> select pri(5) from dual; PRI(5) -------------------------------------------------------------------------------prime

Program to reverse a string


SQL> create or replace function rev(st varchar2) return varchar2 is 2 l number; 3 s varchar2(20); 4 begin 5 l:=length(st); 6 for i in reverse l..l loop 7 s:=s||substr(st,i,l); 8 end loop; 9 return(s); 10 end; 11 / Function created. SQL> select rev('ROCK') from dual; REV('ROCK') -------------------------------------------------------------------------------KCOR

57

PROCEDURES
Program to display salary of an employee
SQL> set serveroutput on SQL> create or replace procedure dispasal(name emp.ename %type)is vsal emp.sal%type; 2 begin 3 select sal into vsal from emp where ename=name; 4 dbms_output.put_line('the sal of emp is:'||vsal); 5 end; 6 / Procedure created. SQL> execute dispasal('KING'); the sal of emp is:5000 PL/SQL procedure successfully completed.

Program to update salary of an employee


SQL> set serveroutput on SQL> create or replace procedure procl(name emp.ename %type,inc number) is 2 vsal emp.sal%type; 3 begin 4 update emp set sal=sal+inc where ename=name; 5 select sal into vsal from emp where ename=name; 6 dbms_output.put_line('sal of emp updated is :'||vsal); 7 end; 8 / Procedure created. SQL> execute procl('KING',5000);

58

sal of emp updated is :10000 PL/SQL procedure successfully completed. SQL> select ename,sal from emp; ENAME SAL ---------- ---------SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 10000 TURNER 1500 ADAMS 1100 ENAME SAL ---------- ---------JAMES 950 FORD 3000 MILLER 1300

59

TRIGGER PROGRAMS
Create a trigger to implement the condition that the number of records should not cross 5
SQL> create table std0308(sid numeric(20),sname char(20),branch char(20)); Table created. SQL> create or replace trigger cse 2 after insert 3 on std0308 4 declare 5 c integer; 6 begin 7 select count(*) into c 8 from std0308; 9 if(c>5) 10 then raise_application_error(-20501,'cant cross 5 records'); 11 end if; 12 end; 13 / Trigger created.

SQL> insert into std0308 2 values(2345,'asd','sce'); Insert into std0308 * 60

ERROR at line 1: ORA-20501:cant cross 5 records ORA-06512:at SCOTT.CSE,line 7 ORA-04088: error during execution of trigger SCOTT.CSE

Create a trigger to implement the condition that only three students can be admitted in cse branch
SQL> create or replace trigger cse 2 after insert 3 on std0308 4 declare 5 c integer; 6 begin 7 select count(*) into c 8 from std0308; 9 if(c>3) 10 then raise_application_error(-20501,'cant cross more records'); 11 end if; 12 end; 13 / Trigger created.

SQL> insert into std0308 2 values(9,cxf,cse); Insert into std0308 * ERROR at line 1: ORA-20501:cant cross more records ORA-06512:at SCOTT.CSE,line 7 ORA-04088: error during execution of trigger SCOTT.CSE

61

Create a trigger to implement the condition that total salary of all employee should not cross 80000
SQL> create or replace trigger r11 2 after insert 3 on emp5030 4 declare 5 c integer; 6 begin 7 select sum(sal) into c 8 from emp5030; 9 if(c>80000) 10 then raise_application_error(-20501,'cant cross 80000'); 11 end if; 12 end; 13 / Trigger created.

SQL> insert into emp5030 2 values(50000); Insert into emp5030 * ERROR at line 1: ORA-20506:cant cross 80000 ORA-06512: at SCOTT.R11,line 7 ORA-04088: error during execution of trigger SCOTT.R11

FORMS
To Log on to Oracle 10g Forms/Reports, Go to Start Programs Oracle 10g Developer Suite Forms Developer, Click Start OC4J Instance(Very Important you must keep OC4J running while using

62

Oracle 10g Forms/Reports!), After OC4J Initialized, Then Click Forms Builder.

Immediately, You will see the window for Form Builder.

Go to tools and Click on the Data Block Wizard, Now You will see the Welcome to Data Block Wizard Window.

63

Click Next to Proceed, You will see the window for the Data Block Wizard. Select table or View option and Click next. Now you will see a window that prompts you to select table or view. Click on Browse button, The Connect Window will appear. Type Password and Database to Connect to the Database.

Now You will see list of tables Created on Database, Select a table, select all the columns, by clicking on button >> .Then Click Next.

64

Now a window appears that prompts you to Enter a name for your Data Block. Type table name selected Previously if the block is empty and Click Next.

You Will now see the Congratulation window, make sure that Create the Data Block, then call the Layout Wizard is selected and Click Next.

65

Now you will see welcome to layout wizard window, Click Next.

Now you will see the below window, click Next.

66

Now you will see a window as shown below, make sure the Data Block Contains the Name of selected table, Then click the double right arrow, all the column will be selected. Then click next.

Now you will see a window that prompt you to set height and width. Click next to accept the default values.

67

Now you will see a Layout Wizard that prompts you to select a title for the form that you are creating. Click next after typing as student form.

Congratulation! You have now successfully created your form. Click Finish to View the form.

68

Now you will see the layout of form. Then go to programs and Click run form.

Student Form

69

Library Form

Pay bill Form

70

REPORTS
71

To Log on to Oracle 10g Forms/Reports, Go to Start Programs Oracle 10g Developer Suite Forms Developer, Click Start OC4J Instance(Very Important you must keep OC4J running while using Oracle 10g Forms/Reports!), After OC4J Initialized, Then Click Report Builder. Now Welcome to Report Builder Wizard will appear Click Ok.

Welcome To report Wizard appears, Then Click Next.

72

Now a window appears as below, select Create both Web and paper Layout. Then Click Next.

Now a wizard appears that prompts to select the type of view. Select Tabular and Click next.

73

Now Select SQL Query and Click Next.

74

Now Type the Query and Click connect and Provide login, password. Then Click Query Builder.

75

Now select all the column of the table by clicking on the double right arrow. Then Click next.

Again Click next. Now a wizard appears that prompts to select width. Click next to select the default values.

76

Now you will see a wizard that prompts to select the colour of the layout. Select a colour and Click next.

Thats all , it finishes describing the report. Click finish.

77

Now a wizard appears as shown below. To view complete layouts go to programs, and click run web layout

78

79

Student Report

Library Report

80

Pay Bill Report

81

Das könnte Ihnen auch gefallen