You are on page 1of 48

YUGAL KISHOR SAHU BSC 3rdYEAR

INDEX
SQL AND PL/SQL
S.NO. SIGN.
1.  SQL Definition
 Primary key
 Foreign key
 Command
 Aggregate Function
 Normalization of Database
 PL/SQL

TABLE
1. Create employee table insert data in employee table (empno,
empname, job, doj, salary, deptno)
2. Create client table insert data into client table (clno, clname,
address, state, balance)
3. Create salesman table insert data into salesman table (slno,
sname, oddate, salary, address, city)
4. Create tabte student table insert into data student table
(rno,sname,class,msub ,feedue,percent)
QUERY
1. Create a query to display emp no & emp name for all the emp
earn more than average salary.
2. Select empno,empname,job whose date 1-jan-99 to1-dec-99
3. Display emp name,job,salary from emp1 where
empname='sheetal';
4. Display emp name,salary,dep no. work in company as an
army.
5. Display emp no. & salary from that job based upto deptno.
6.
CREATE A QUERY TO DISPLAY DEPT NO. EMP NO. OF JOB WORK IN
EMPLOYEE.

7. Count the total no. of employee


8. List the name of employee having ‘r’ as first letter their name
YUGAL KISHOR SAHU BSC 3rdYEAR

9. Change the balance of client no 1.


10. List the client name who are locate in state CG.
11. Change the size of client name from table client. .
12. Create the table in reference key to the emp relation to
foreign key.
13. Add a colum called ‘telephone’ of datatype varchar2 andsize
10 to the client table
14.
ADD A COLUMN MOBILE NO. OF DATATYPE VARCHAR 2(20) TO THE
SALESMAN TABLE.

15. List the description of each salesman in table salesman.


16. Delete from salesman where salesman name is rajeev
17. Count the total no. of odd date from salesman.
18. Select all salary from both employee and salesman.
19. List all Student whose roll no belong the range between 222
to444
20. List all student whose roll no are not in (999,666,444)
21. List all student whose name are sheetal and yugal
22. list all student whose name start with letter ‘p’;
23. list all student whose name start with letter ‘j’ as student of
BA with economy.
24. List all student whose name started ‘p’ & have a geograpy
25. Count total number of student in class base.
26. Count total no. of student in subject base.
27. list all student of Bsc where main subject in maths
28. List all the student whose main subject is dbms and feedue is 0.

29. List average percent of all class.

30. List all students whose percent is more than average percent.
YUGAL KISHOR SAHU BSC 3rdYEAR

SQL
Definition:- the structured query language (SQL)is a language that enables us to create and
operate on relation databases ,which are sets of related information stored in tables.

First commercially available


implementation osf SQL was released in 1979 by Relation software lnc. ,which is today known
as oracle corporation . thus, Oracle is pioneer RDBMS that started using SQL.

SQL follows the following rules:-

 Structured query language is not case sensitive. Keywords of SQL are written in
uppercase.
 Statements of SQL are dependent on text lines. We can use a single SQL statement on
one or more multiple text line.
 SQL depends on tuple relational calculus and relational algebra.

Datatypes in SQL:
Datatype is used to define the type of data that will be stored in a particular field .

Some of the basic datatypes are as follows-

(1) Char (size) : this data type is used to store character string of fixed length .the size is
given to determine the number of characters we want to store for a field . the maximum
allowable field size is 255 characters .
(2) Varchar (size) or varchar2 (size) : this data type is used to store alphanumeric
data . the maximum allowable field size for this is 2000 characters.
(3) Number (P,S): this data type is used to store numbers (fixed or floating point ).
(4) Data : this data type is used to represent data & time . the standard format is ‘DD-
MON-YY’.
YUGAL KISHOR SAHU BSC 3rdYEAR

SQL COMMANDS
SQL commands are instructions . It is used to communicate with the database and perform the
specific tasks, functions and queries of data.

SQL uses certain commands like Create, Drop, Insert etc. to carry out the required tasks.

These SQL commands are mainly categorized into four categories as:-

 DDL
 DML
 DCL
 TCL
1. DML:-
DML stands for data definition language. It changes the structure of the table
like creating a table, deleting a table, altering a table etc.
Some commands that come under DDL are-
 CREATE
 ALTER
 DROP
2. DML:-
DML stands for data manipulation language. It is used to modify the database.
Some commands that come under DML are-
 INSERT
 UPDATE
 DELETE
3. DCL:-
DCL stands for data control language. It is used to grant and take back authority
from any database user.
Some commands that come under DCL-
 GRANT
 REVOKE
4. TCL:-
TCL stands for transaction control language. Some commands that come under
TCL are-
 COMMIT
 ROOLBACK
 SAVEPOINT

CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns.
To
create a table we will name the table and the columns of the table. We follow the rules to name
tables and columns:-
It must begin with a letter and can be up to 30 characters long.
It must not be duplicate and not any reserved word.
Syntax :-
YUGAL KISHOR SAHU BSC 3rdYEAR

CREATE TABLE tablename (column_name1 datatype (size), column_name2 datatype


(size) …);
Example :-
SQL> create table emp1(empno number(15), empname char(20),job char(15),doj date,
salary number(10),depno number(20));

Table created.

Insert: if we want to insert value in table then we use insert command:-


Sysntax:- insert into tablename values (column_name1 datatype (size), column_name2
datatype(size) …);
Example:-

SQL> insert into emp1 values(101,'sonu','techer','9-mar-2015',20000,1);


1 row created.

SELECT : The select command of sql lets you make queries on the database. A query is a
command that is given certain specified information from the database tables. It can be used to
retrieve a subset of rows or columns from one or more tables.
Syntax :-
SELECT* FROM <tablename>;
Example:-

SQL> select * from emp1;

EMPNO EMPNAME JOB DOJ SALARY DEPNO

---------- -------------------- --------------- --------- ---------- ----------

101 nomesh teacher 09-MAR-15 20000 1

102 kumesh lecture 09-FEB-17 22000 2

DESCRIBE : To find information about columns like column name, their data types and
other
attributes of a table we can use DESCRIBE command.
Syntax :-
DESCRIBE tablename;
YUGAL KISHOR SAHU BSC 3rdYEAR

Example:-desc emp1;

Name Null? Type

----------------------------------------- -------- ------------------

EMPNO NUMBER(10)

EMPNAME CHAR(20)

JOB CHAR(15)

DOJ DATE

SALARY NUMBER(15)

DEPNO NUMBER(15)

ALTER TABLE : After creating a table one may have need to change the table either by
add
new columns or by modify existing columns. One can do so by using alter table command.
Syntax :-to add a column is
ALTER TABLE tablename ADD(col1 datatype,col2 datatype);

Example:-SQL>alter table emp1 modify(empname char(25));

Table altered.

SQL>desc emp1;

Name Null? Type

----------------------------------------- -------- ------------------

EMPNO NUMBER(10)

EMPNAME CHAR(25)

JOB CHAR(15)

DOJ DATE

SALARY NUMBER(15)

DEPNO NUMBER(15)
YUGAL KISHOR SAHU BSC 3rdYEAR

DELETE : One can delete data fron table by using delete from statement. The delete statement
removes rows from table but it doesn’t release storage space.
Syntax:-
Syntax of delete rows from table is
DELETE FROM tablename WHERE <condition>;
Example:-
SQL>delete from emp1 where empname =’kumesh’;

row deleted.

SQL> select * from emp1;

EMPNO EMPNAME JOB DOJ SALARY DEPNO

---------- -------------------- --------------- --------- ---------- ----------

101 nomesh teacher 09-MAR-15 20000 1

UPDATE : The update command enables user to change the values of existing rows.
Syntax :-Syntax to update value of table is
UPDATE tablename SET col1=value1,col2=value2;
Example:-

SQL> update emp set salary ='40000' where empno='101';

1 row updated.

SQL> select * from emp1;

EMPNO EMPNAME JOB DOJ SALARY


DEPNO

---------- -------------------- --------------- --------- ---------- -------


---

101 nomesh teacher 09-MAR-15 40000 1

DROP TABLE : To remove the definition of oracle table, the drop table statement is used.
Syntax :-Syntax to drop table is
DROP TABLE tablename;

RENAME : One can change the name of a table by rename command.


Syntax :-Syntax to rename table is
YUGAL KISHOR SAHU BSC 3rdYEAR

RENAME oldname TO newname;

Commit : this command makes permanent any changes made by statement that have been
executed since the beginning of this transaction. Generally user uses this command at the end of
every session.

Syntax :-Syntax to commit command:-


SQL>commit;

Keys: keys are attributes or set of attributes used to distinguish one entity from
another in an entity set.

Primary key: the primary key is the term used for the candidate key that is
chosen by the database designer as the principal means of identifying an entity.
EXAMPLE:-

Consider a student table. In the “student” table there are three attributes Stu_ID, Stu_Name
and Stu_Age.

Student

Stu_ID Stu_Name Stu_Age


101 nomesh 23
102 kumesh 24
103 bhupal 28
104 chandu 29
105 mohend 29

Out of these three attributes, one attribute or a set of more than one attributes can be a
primary key. Attribute Stu_Name alone cannot be a primary key as more than one students
can have same name. Attribute Stu_Age alone cannot be a primary key as more than one
students can have same age.Attribute Stu_ID alone is a primary key as each student has a
unique id that can identify the student record in the table.

Foreign key: a foreign key is an attributes or set of attributes in a relation of


database that serve as the primary key of another relation in the same database .
EXAMPLE:-

Consider two tables “Student” and “Department” having attributes as stu_id, stu_name, and
course in Student table and Dept_name and stu_id in table Department.
YUGAL KISHOR SAHU BSC 3rdYEAR

Student

Stu_id Stu_name Course


101 nomesh Computer
105 mohend physics
107 kumesh maths
108 bhupal english

Department

Dept_name Stu_id
CS_Department 105
CS_Department 101
Science_Department 101
Math_Department 108

In the tables, one attribute is common that is stu_id, but it has different key constraints for both
tables. In the Student table the field stu_id is a primary key because it is uniquely identifying all
other fields of the Student table.

On the other hand, stu_id is a foreign key attribute for the Department table because it is acting
as a primary key attribute for the Student table.

It means both that both the Student and Department table are linked with one another because of
the stu_id attribute.

AGGREGATE FUNCTION

An aggregate function performs a calculation on a set of values, and returns a


single value. Aggregate functions are often used with the GROUP BY clause of
the SELECT statement.

Transact-SQL provides the following aggregate functions:

 COUNT
 SUM
 AVG
 MAX
 MIN

1. COUNT FUNCTION

o COUNT function is used to Count the number of rows in a database table. It can work
on both numeric and non-numeric data types.
YUGAL KISHOR SAHU BSC 3rdYEAR

o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.

Syntax:-Count ({*l[DISTINCT l ALL]expr})

Example:-The following examples use COUNT as an aggregate function:


SELECT COUNT(*) "Total"

FROM employees;

Total

----------

17

2. SUM Function

Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.

Syntax:-SUM([DISTINCT \ALL]n)

Example:-The following example calculates the sum of all salaries in the


sample hr.employees table:

SELECT SUM(salary) "Total"

FROM employees;

Total

----------

691400

3. AVG function

The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.

Syntax:-AVG([DISTINCT l ALL]n)
YUGAL KISHOR SAHU BSC 3rdYEAR

Example:-The following example calculates the average salary of all employees in


the hr.employees table:
SELECT AVG(salary) "Average"

FROM employees;

Average

--------------

6461.83178

4. MAX Function

MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.

Syntax:-MAX([DISTINCT l ALL]expr)

Example:-The following example determines the highest salary in the hr.employees table:

SELECT MAX(salary) "Maximum"

FROM employees;

Maximum

----------

24000

5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.

Syntax:-MIN([DISTINCT l ALL]expr)

Example:-The following statement returns the earliest hire date in the hr.employees table:

SELECT MIN(hire_date) "Earliest"

FROM employees;
YUGAL KISHOR SAHU BSC 3rdYEAR

Earliest

---------

13-JAN-01

NORMALIZATION OF DATABASE

o Normalization is the process of organizing the data in the database.


o Normalization is used to minimize the redundancy from a relation or set of relations. It
is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
o Normalization divides the larger table into the smaller table and links them using
relationship.
o The normal form is used to reduce redundancy from the database table.

Here are the most commonly used normal forms:

 First normal form(1NF)


 Second normal form(2NF)
 Third normal form(3NF)
 Boyce & Codd normal form (BCNF)
 4NF (Fourth Normal Form) Rules
 5NF (Fifth Normal Form) Rules

1NF (First Normal Form) Rules


 Each table cell should contain a single value.
 Each record needs to be unique.

Example:- we have “table 1” of student relation.

Stu_no Stu_name Stu_phone Stu_state


1 Ram 9875012399 Haryana
9340514609
2 Ram 9760413899 Punjab
3 Riya Punjab

Below student relation is not in 1NF because of multi-valued attribute stu_phone. It’s
decomposition into 1NF is given by “table2”.
YUGAL KISHOR SAHU BSC 3rdYEAR

Stu_no Stu_name Stu_phone Stu_state


1 Ram 9875012399 Haryana
2 Ram 9340514609 Haryana
3 Ram 9760413899 Punjab
4 Suresh Punjab

2NF (Second Normal Form) Rules

 Rule 1- Be in 1NF
 Rule 2- Single Column Primary Key

Example:- Let us consider a “TEACHER” table.

Teach_id subject Teach_age


25 chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38

 In the above table, non primary attribute teach_age is dependent on teach-id is a proper
subset of a candidate key. Hence, it is not in 2NF.
 To convert the given table into 2NF, we decompose the above table into two tables:
Teacher_Detail-

Teache_id Teach_age
25 30
47 35
83 38

 Teacher_subject-

Teach_id Subject
25 chemistry
25 Biology
47 English
83 Math
83 Computer

3NF (Third Normal Form) Rules


YUGAL KISHOR SAHU BSC 3rdYEAR

 Rule 1- Be in 2NF
 Rule 2- Has no transitive functional dependencies

Example:- consider a table “Book_detail”.

Book_id Genre_id Genre type Price


1 1 Gardening 25.99
2 2 Sports 14.99
3 1 Gardening 10.00
4 3 Travel 12.99
5 2 Sports 17.99

 In the above table, Book_id determines Genre_id and Genre_id determines Genre type.
Therefore Book_id determines Genre type through Genre_id and we have transitive
functional dependency. Hence it is not in 3NF form.
 To convert the above table into 3NF we decompose it into two tables.
 TABLE_BOOK-

Book_id Genre_id Prince


1 1 25.99
2 2 14.99
3 1 10.00
4 3 12.99
5 2 17.99

 TABLE_GENRE-

Genre_id Genre type


1 Gardening
2 Sports
3 Travel

BCNF (Boyce-Codd Normal Form)

 Even when a database is in 3rd Normal Form, still there would be anomalies resulted if it
has more than one Candidate Key.

Example:- Let us assume there is a company where employees work in more than one
department.
YUGAL KISHOR SAHU BSC 3rdYEAR

EMPLOYEE

Emp_id Emp_country Emp_dept Dept_type Emp_dept_no


264 INDIA Designing D394 283
264 INDIA Testing D394 300
364 UK Stores D283 232
364 UK Developing D283 549

In the above table partial dependencies are:

Emp_id→ Emp_country

Emp_dept→ (dept_type, Emp_dept_no)

Candidate key: Emp_id, Emp_dept

The table is not in BCNF because neither Emp_dept nor Emp_id alone are keys.

To convert the above table into BCNF, we decompose it into three tables:

Emp_country-
Emp_id Emp_country
264 INDIA
264 INDIA

Emp_dept-
Emp_dept Dept_type Emp_dept_no
Designing D394 283
Testing D394 300
Stores D283 232
Developing D283 549

Emp_id Emp_dept
D394 283
D394 300
D283 232
D283 549

Functional dependencies are:

Emp_id → Emp_country

Emp_dept → (Dept_type, Emp_dept_no)

Candidate keys are:-


YUGAL KISHOR SAHU BSC 3rdYEAR

For the first table: Emp_id

For the second table: Emp_dept

For the third table: (Emp_id, Emp_dept)

Now this is in BCNF because left side part of both the functional dependencies is a key.

4NF (Fourth Normal Form) Rules

If no database table instance contains two or more, independent and multivalued data describing
the relevant entity, then it is in 4th Normal Form.

Example:- Student

Stu_id Course Hobby


21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey

In the student relation, a student with Stu_id contains two courses, computer and math and
hobbies dancing and singing. So there is a multi – valued dependency on stu_id, which leads to
unnecessary repetition of data.

So to make the above table into 4NF, we can decompose it into two tables:

STUDENT_COURSE:
Stu_id Course
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics

STUDENT_HOBBY:
Stu_id Hobby
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
YUGAL KISHOR SAHU BSC 3rdYEAR

5NF (Fifth Normal Form) Rules


A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed into any number
of smaller tables without loss of data.

Example:- consider a table name “class”.

Subject Lecturer Semester


Computer Anshika Sem1
Computer John Sem1
Math John Sem1
Math Akash Sem2
Chemistry Praveen Sem1

In the above table “class”, john takes both Computer and Math class for Sem1 but he does not
take Math class for Sem2. In this case combination of all these fields required to identify a valid
data.

So, to convert the above table into 5NF, we can decompose it into three tables P1, P2 and P3.

P1:
Semester Subject
Sem1 Computer
Sem1 Math
Sem1 Chemistry
Sem2 Math

P2:

Subject Lecturer
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen

Semester Lecturer
Sem1 Anshika
YUGAL KISHOR SAHU BSC 3rdYEAR

Sem1 John
Sem1 John
Sem2 Akash
Sem1 Praveen
P3:

PL/SQL

The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the Oracle relational database. Following are
certain notable facts about PL/SQL −
 PL/SQL is a completely portable, high-performance transaction-processing language.
 PL/SQL provides a built-in, interpreted and OS independent programming environment.
 PL/SQL can also directly be called from the command-line SQL*Plus interface.
 Direct call can also be made from external programming language calls to database.
 PL/SQL's general syntax is based on that of ADA and Pascal programming language.

Features of PL/SQL
PL/SQL has the following features −

 PL/SQL is tightly integrated with SQL.


 It offers extensive error checking.
 It offers numerous data types.
 It offers a variety of programming structures.
 It supports structured programming through functions and procedures.
 It supports object-oriented programming.
 It supports the development of web applications and server pages.

Advantages of PL/SQL

PL/SQL has the following advantages −


 SQL is the standard database language and PL/SQL is strongly integrated with SQL.
PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations
and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding
DDL statements in PL/SQL blocks.
 PL/SQL allows sending an entire block of statements to the database at one time. This
reduces network traffic and provides high performance for the applications.
 PL/SQL gives high productivity to programmers as it can query, transform, and update
data in a database.
 PL/SQL saves time on design and debugging by strong features, such as exception
handling, encapsulation, data hiding, and object-oriented data types.
YUGAL KISHOR SAHU BSC 3rdYEAR

 Applications written in PL/SQL are fully portable.


 PL/SQL provides high security level.
 PL/SQL provides access to predefined SQL packages.
 PL/SQL provides support for Object-Oriented Programming.
 PL/SQL provides support for developing Web Applications and Server Pages.

Syntax:
The Basic Syntax of PL/SQL programs are divided and written in logical blocks of code. Each
block consists of three sub-parts −

S.No Sections & Description

Declarations
1 This section starts with the keyword DECLARE. It is an optional section and
defines all variables, cursors, subprograms, and other elements to be used in
the program.

Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a
2 mandatory section. It consists of the executable PL/SQL statements of the
program. It should have at least one executable line of code, which may be just
a NULL command to indicate that nothing should be executed.

Exception Handling
3 This section starts with the keyword EXCEPTION. This optional section
contains exception(s) that handle errors in the program.

 Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested
within other PL/SQL blocks using BEGIN and END.

Following is the basic structure of a PL/SQL block –

 DECLARE
 <declarations section>
 BEGIN
 <executable command(s)>
 EXCEPTION
 <exception handling>
 END;
YUGAL KISHOR SAHU BSC 3rdYEAR

Example:-

The 'Hello World' Example-

 DECLARE
 message varchar2(20):= 'Hello, World!';
 BEGIN
 dbms_output.put_line(message);
 END;
 /

The end; line signals the end of the PL/SQL block. To run the code from the SQL
command line, we may need to type / at the beginning of the first blank line after the
last line of the code.
When the above code is executed at the SQL prompt, it produces the following result −
Hello World

PL/SQL procedure successfully completed.


YUGAL KISHOR SAHU BSC 3rdYEAR

Table

Q 01:- CREATE EMPLOYEE TABLE INSERT DATA IN EMPLOYEE TABLE(EMPNO,


EMPNAME, JOB, DOJ, SALARY, DEPTNO )

Coding:-
SQL> create table emp1(empno number,empname char(20),job char(15),doj date,salary
number,depno number);

Table created.

SQL> insert into emp1 values(121,'sheetal','lecturer','20-aug-1998',10000,1);

1 row created.

SQL> insert into emp1 values(102,'yugalkishor','techer','20-jan-1999',11000,2);

1 row created.

SQL> insert into emp1 values(234,'rajeev','professor','21-feb-1998',12000,3);

1 row created.

SQL> insert into emp1 values(123,'pravin','clerk','12-sep-2000',13000,4);

1 row created.

SQL> insert into emp1 values(345,'neeraj','principal','22-dec-1998',14000,5);1

row created.
YUGAL KISHOR SAHU BSC 3rdYEAR

SQL> insert into emp1 values(321,'rohit','programmer','23-mar-1999',15000,6);

1 row created.

SQL> insert into emp1 values(512,'vikram','penter','25-dec-1998',16000,7);

1 row created.

SQL> insert into emp1 values(124,'bittu','police','16-dec-1998',17000,8);

1 row created.

SQL> insert into emp1 values(125,'manoj','army','23-aug-1998',18000,9);

1 row created.

SQL> insert into emp1 values(126,'jitendra','electrician','02-mar-1999',20000,10);

1 row created.

SQL> insert into emp1 values(127,'rekhu','engineer','05-aug-1999',25000,11);

1 row created.

SQL> select * from emp1;


YUGAL KISHOR SAHU BSC 3rdYEAR

EMPNO EMPNAME JOB DOJ SALARY DEPNO

121 sheetal lecturer 20-AUG-98 10000 1

102 yugalkishor techer 20-JAN-99 11000 2

234 rajeev professor 21-FEB-98 12000 3

123 pravin clerk 12-SEP-99 13000 4

345 neeraj principal 22-DEC-98 14000 5

321 rohit programmer 23-MAR-99 15000 6

512 vikram penter 25-DEC-98 16000 7

124 bittu police 16-DEC-98 17000 8

125 manoj army 23-AUG-98 18000 9

126 jitendra electrician 02-MAR-99 20000 10

127 rekhu engineer 05-AUG-99 25000 11

11 rows selected.
YUGAL KISHOR SAHU BSC 3rdYEAR

Q 02:- CREATE CLIENT TABLE INSERT DATA INTO CLIENT TABLE (CLNO, CLNAME,
ADDRESS , STATE, BALANCE)

Coding:-
SQL> create table t2(clno number,clname char(9),address char(9),state char(9),balance
number);

Table created.

SQL> insert into t2 values(1,'yugal','piproud','cg',10000);

1 row created.

SQL> insert into t2 values(2,'sheetal','nayapara','cg',1000);

1 row created.

SQL> insert into t2 values(3,'pravin','patewa','cg',20000);

1 row created.

SQL> insert into t2 values(4,'neeraj','kurra','mp',15000);

1 row created.

SQL> insert into t2 values(5,'rajeev','pahanda','up',25000);


YUGAL KISHOR SAHU Bsc 3rd year(cs)

1 row created.

SQL> insert into t2 values(6,'khoman','nawapara','delhi',23000);

1 row created.

SQL> insert into t2 values(7,'jagendra','rajim','karnatak',24000);

1 row created.

SQL> insert into t2 values(8,'jeevan','naagav','udisa',21000);

1 row created.

SQL> select * from t2;

CLNO CLNAME ADDRESS STATE BALANCE

1 yugal Piproud cg 10000

2 sheetal nayapara cg 1000

3 pravin patewa cg 20000

4 neeraj kurra mp 15000

5 rajeev pahanda up 25000

6 khoman nawapara delhi 23000

7 jagendra rajim karnatak 24000

8 jeevan naagav udisa 21000

8 rows selected
YUGAL KISHOR SAHU Bsc 3rd year(cs)

Q 03:- CREATE SALESMAN TABLE INSERT DATA INTO SALESMAN TABLE (SLNO,
SNAME, ODDATE, SALARY , ADDRESS , CITY)

Coding:-
SQL> create table t3(sno number,sname char(9),oddate date,salary number,address
char(9),city char(20));

Table created.

SQL> insert into t3 values(1,'rajeev','20-jan-1999',10000,'piproud','raiour');

1 row created.

SQL> insert into t3 values(2,'yugal','20-nov-1999',12000,'kurra','dhamtari');

1 row created.

SQL> insert into t3 values(3,'pravin','11-aug-1998',13000,'kumhari','raipur');

1 row created.

SQL> insert into t3 values(4,'neeraj','12-aug-1999',14000,'gobra','rajim');

1 row created.

SQL> insert into t3 values(5,'sheetal','13-june-1997',14000,'lafin','durg');


YUGAL KISHOR SAHU Bsc 3rd year(cs)

1 row created.

SQL> select * from t3;

SNO SNAME ODDATE SALARY ADDRESS CITY

1rajeev 20-JAN-99 10000 piproud raiour

2 yugal 20-NOV-99 12000 kurra dhamtari

3 pravin 11-AUG-98 13000 kumhari raipur

4 neeraj 12-AUG-99 14000 gobra rajim

5 sheetal 13-JUN-97 14000 lafin durg


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUE4:- CREATE TABTE STUDENT TABLE INSERT INTO DATA STUDENT TABLE

(RNO,SNAME,CLASS,MSUB ,FEEDUE,PERCENT)
Coding:-
SQL> create table t4(rno number,sname char(15),class varchar(20),msub char(20),feedue
number,percent varchar(20));

Table created.

SQL> insert into t4 values(111,'sheetal','BSc','cs',8000,80);

1 row created.

SQL> insert into t4 values(222,'yugal','11th','commerce',9000,76);

1 row created.

SQL> insert into t4 values(333,'neeraj','BSc','maths',10000,70);

1 row created.

SQL> insert into t4 values(444,'rajeev’,’BSc’,’physics’,11000,60);

1 row created.

SQL> insert into t4 values(666,’pravin’,'BA','geograpy',13000,85);


YUGAL KISHOR SAHU Bsc 3rd year(cs)

1 row created.

SQL> insert into t4

values(888,'jitendra','BA','economy',14000,77);1 row created.

SQL> insert into t4 values(999,'bed','Bcom','account',15000,76);

1 row created.

SQL> select * from t4;

RNO SNAME CLASS MSUB FEEDUE PERCENT

111 Sheetal BSc cs 8000 80

222 Yugal 11th commerce 9000 76

333 neeraj BSc maths 10000


70
444 rajeev BSc physics 11000 60

666 pravin BA geograpy 13000 85

888 jitendra BA economy 14000 77

999 bed Bcom account 15000 76

7 rows selected
QUERY 1:-CREATE A QUERY TO DISPLAY EMP NO & EMP NAME FOR ALL THE
EMP EARN MORE THAN AVERAGE SALARY.

Coding:-
SQL> select empno,empname from emp1 where salary>(select avg(salary)from emp1);

EMPNO EMPNAME

512 vikram

124 bittu

125 manoj

126 jitendra

127 rekhu

QUERY 2:-SELECT EMPNO ,EMPNAME,JOB WHOSE DATE 1-JAN-99 TO1-DEC-


99.

SQL> select empno,empname,job from emp1 where doj between '20-jan-1999'and'20-dec-


1999';

EMPNO EMPNAME JOB

121 sheetal Lecturer

234 rajeev Professor

125 manoj amry


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 3:-DISPLAY EMP NAME,JOB,SALARY FROM EMP 1 WHERE


EMPNAME='SHEETAL';

Coding:-
SQL> select empname,job,salary from emp1 where empname='sheetal';

EMPNAME JOB SALARY

sheetal lecturer 10000

QUERY 4:- DISPLAY EMP NAME ,SALARY,DEP NO. WORK IN COMPANY AS AN


TECHER .

Coding:-
SQL> select empname,job,salary from emp1 where job='techer';

EMPNAME JOB SALARY DEPNO

yugal techer 11000 2


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 5:- DISPLAY EMP NO . & SALARY FROM THAT JOB BASED UPTO DEPTNO.
Coding :-

SQL> select empno,job,salary from emp1 order by depno;

EMPNO JOB SALARY

121 lecturer 10000

102 techer 11000

234 professor 12000

123 clerk 13000

345 principal 14000

321 programmer 15000

512 penter 16000

124 police 17000

125 army 18000

126 electrician 20000

127 engineer 25000

11 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 6:-CREATE A QUERY TO DISPLAY DEPT NO. EMP NO. OF JOB WORK IN
EMPLOYEE.

SQL> select empno,depno,job from emp1;

EMPNO DEPNO JOB

121 1 lecturer

102 2 techer

234 3 professor

123 4 clerk

345 5 principal

321 6 programmer

512 7 penter

124 8 police

125 9 army

126 10 electrician

127 11 engineer
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 7:-COUNT THE TOTAL NO. OF EMPLOYEE.

Coding:-
SQL> select count(empno)from emp1;

COUNT(EMPNO)

11

QUERY8:-LIST THE NAME OF EMPLOYEE HAVING ‘R’ AS FIRST LETTER THEIR


NAME.

Coding:-

SQL> select * from emp1 where empname like'r%';

EMPNO EMPNAME JOB DOJ SALARY DEPNO

234 rajeev professor 21-FEB-98 12000 3

321 rohit programmer 23-MAR-99 15000 6

127 rekhu engineer 05-AUG-99 25000 11


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 9:-CHANGE THE BALANCE OF CLIENT NO 1.

CODING:-

SQL> update t2 set balance ='9000'where clno='1';

1 row updated.

SQL> select * from t2;

CLNO CLNAME ADDRESS STATE BALANCE

1 yugal Piproud cg 9000

2 sheetal nayapara cg 1000

3 pravin patewa cg 20000

4 neeraj kurra mp 15000

5 rajeev pahanda up 25000

6 khoman nawapara delhi 23000

7 jagendra rajim karnatak 24000

8 jeevan naagav udisa 21000


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 10:- LIST THE CLIENT NAME WHO ARE LOCATE IN STATE CG.
CODING:-
SQL> select clname from t2 where state='cg';

CLNAME

yugal

sheetal

pravin

QUERY 11:- CHANGE THE SIZE OF CLIENT NAME FROM TABLE CLIENT.
CODING:-
SQL> alter table t2 modify(clname char(15));

Table altered.

SQL> select * from t2;

CLNO CLNAME ADDRESS STATE BALANCE

1 yugal Piproud cg 10000

2 sheetal Nayapara cg 1000

3 pravin patewa cg 20000

4 neeraj kurra mp 15000

5 rajeev pahanda up 25000

6 khoman nawapara delhi 23000

7 jagendra rajim karnatak 24000

8 jeevan naagav udisa 21000

8 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 12:-CREATE THE TABLE IN REFERENCE KEY TO THE EMP RELATION TO


FOREIGN KEY.

Coding:-
SQL> create table company(rno varchar(15) primary key,clno number,name char(20));

Table created.

SQL> insert into company values(221,4,'sheetal');

1 row created.

SQL> insert into company values(223,5,'yugal');

1 row created.

SQL> insert into company values(224,6,'sonu');

1 row created.

SQL> insert into company values(225,6,'rekhu');

1 row created.
YUGAL KISHOR SAHU Bsc 3rd year(cs)

SQL> select * from company;

RNO CLNO NAME

221 4sheetal

223 5 yugal

224 6 sonu

225 6 rekhu

SQL> create table company1(rno varchar(13) references company,clno number,name


char(20));

Table created.

SQL> insert into company1 values(221,4,'sheetal');

1 row created.

SQL> select * from company1;

RNO CLNO NAME

221 4sheetal
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 13:-ADD A COLUM CALLED ‘TELEPHONE’ OF DATATYPE VARCHAR2 AND


SIZE 10 TO THE CLIENT TABLE.

Coding:-
SQL> alter table t2 add(telephone varchar(10));

Table altered.

SQL> select * from t2;

CLNO CLNAME ADDRESS STATE BALANCE TELEPHONE

1 yugal pahanda cg 9000

2 Sheetal kurra cg 1000

3 Pravin patewa cg 20000

4 neeraj patewa mp 15000

5 rajeev pahanda up 25000

6 khoman nawapara delhi 23000

7 jagendra rajim karnatak 24000

8 jeevan naagav udisa 21000


YUGAL KISHOR SAHU

QUERY 14:-ADD A COLUMN MOBILE NO. OF DATATYPE VARCHAR2(20) TO THE


SALESMAN TABLE.
SQL> alter table t3 add(mno varchar(20));

Table altered.

SQL> select * from t3;

SNO SNAME ODDATE SALARY ADDRESS CITY MNO

1 rajeev 20-JAN-99 10000 pahanda raiour

2 yugal 20-NOV-99 12000 kurra dhamtari

3 pravin 11-AUG-98 13000 kumhari raipur

4 neeraj 12-AUG-99 14000 gobra rajim

5 sheetal 13-JUN-97 14000 lafin durg

QUERY 15.LIST THE DESCRIPTION OF EACH SALESMAN.


CODING:-
SQL> desc t3;

Name Null? Type

SNO NUMBER

SNAME CHAR(9)

ODDATE DATE

SALARY NUMBER

ADDRESS CHAR(9)

CITY CHAR(9)
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 16:- DELETE FROM SALESMAN WHERE SALESMAN NAME IS RAJEEV.


CODING:-

SQL> delete from t3 where sname='rajeev';

1 row deleted.

SQL> select * from t3;

SNO SNAME ODDATE SALARY ADDRESS CITY MNO

2 yugal 20-NOV-99 12000 kurra dhamtari

3 pravin 11-AUG-98 13000 kumhari raipur

4 neeraj 12-AUG-99 14000 gobra rajim

5sheetal 13-JUN-97 14000 lafin durg

QUERY 17:- COUNT THE TOTAL NO . OF ODD DATE FROM SALESMAN.

Coding:-
SQL> select count(oddate)from t3;

COUNT(ODDATE)

5
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 18:- SELECT ALL SALARY FROM BOTH EMPLOYEE AND SALESMAN.
CODING:-

SQL> select salary from emp1 union all select salary from t3;

SALARY

10000

11000

12000

13000

14000

15000

16000

17000

18000

20000

25000

10000

12000

13000

14000

14000

16 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 19:- LIST ALL STUDENT WHOSE ROLL NO BELONG THE RANGE BETWEEN
222TO444.
Coding:-

SQL> select * from t4 where rno between '222' and '444';

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ---------- ----------

222 yugal 11th commerce 9000 76

333 neeraj BSc maths 10000 70

444 rajeev BSc physics 11000 60

QUERY20 LIST ALL STUDENT WHOSE ROLL NO ARE NOT IN (999,666,444)-

CODING:-

SQL> select * from t4 where rno not in(999,666,444);

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

111 sheetal BSc cs 8000 80

222 yugal 11th commerce 9000 76

333 neeraj BSc maths 10000 70

888 jitendra BA economy 14000 77


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 21:-LIST ALL STUDENT WHOSE NAME ARE SHEETAL AND YUGAL.
CODING:-
SQL> select * from t4 where sname in('sheetal','yugal');

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

111 sheetal BSc cs 8000 80

222 yugal 11th commerce 9000 76

QUERY 22:- LIST ALL STUDENT WHOSE NAME START WITH LETTER ‘P’;
CODING:-
SQL> select * from t4 where sname like 'p%';

RNO SNAME CLASS MSUB FEEDU PERCENT

---------- - ------------- - -------------------- - ------------------- ----------

666 pravin BA geograpy 13000 85

Query 23:-list all student whose name start with letter ‘j’ a s student of BA
with economy.

Coding:-
SQL> select * from t4 where sname like 'j%' and msub='economy';

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

888 jitendra BA economy 14000 77


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 24:- LIST ALL STUDENT WHOSE NAME STARTED ‘P’ & HAVE A
GEOGRAPY

Coding:-
SQL> select * from t4 where sname like'p%' and msub='geograpy';

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

666 pravin BA geograpy 13000 85

QUERY 25:-COUNT TOTAL NUMBER OF STUDENT IN CLASS BASE.

CODING:-

SQL> select count(rno)from t4 group by class;

COUNT(RNO)

----------

2
YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 26:-COUNT TOTAL NO. OF STUDENT IN SUBJECT BASE.

Coding:-
SQL> select count(rno)from t4 group by msub;

COUNT(RNO)

----------

6 rows selected.

QUERY 27:-LIST ALL STUDENT OF BSC WHERE MAIN SUBJECT IN MATHS

CODING:-
SQL> select*from t4 where class='BSc' and msub='maths';

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

333 neeraj BSc maths 10000 70


YUGAL KISHOR SAHU Bsc 3rd year(cs)

QUERY 28:- LIST ALL STUDENT OF WHERE MAIN

SUBJECT PHYSICS & FEEDUE 11000

CODING:-
SQL> select*from t4 where msub='physics' and feedue='11000';

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

444 rajeev BSc physics 11000 60

QUERY 29:- LIST AVERAGE PERCENT OF ALL

CLASS .

CODING:-
SQL> select avg(percent) from t4;

AVG(PERCENT)

------------

74.5
YUGAL KISHOR SAHU Bsc 3rd year(cs)

Q30:- LIST ALL STUDENT WHOSE PERCENT IS MORE THAN AVERAGE PERCENT .
CODING:-
SQL> select * from t4 where percent>(select avg(percent) from t4);

RNO SNAME CLASS MSUB FEEDUE PERCENT

---------- --------------- -------------------- -------------------- ----------

111 Sheetal BSc cs 8000 80

222 Yugal 11th commerce 9000 76


333 Neeraj BSc maths 10000
70
444 rajeev BSc physics 11000
60
666 pravin BA geograpy 13000
85
888 jitendra BA economy 14000
77
999 bed Bcom account 15000 76

You might also like