Record For New

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 128

B.

TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Introduction to SQL
Structure Query Language(SQL) is a database query language used for storing and managing data in
Relational DBMS. SQL was the first commercial language introduced for E.F Codd's Relational model
of database. Today almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the
standard database query language. SQL is used to perform all types of data operations in RDBMS.

SQL Command
SQL defines following data languages to manipulate data of RDBMS.
DDL : Data Definition Language
All DDL commands are auto-committed. That means it saves all the changes permanently in the database.

Command Description

Create to create new table or database

Alter for alteration

Truncate delete data from table

Drop to drop a table

Rename to rename a table

DML : Data Manipulation Language


DML commands are not auto-committed. It means changes are not permanent to database, they can be
rolled back.

Command Description

Insert to insert a new row

Update to update existing row

Delete to delete a row

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 1
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Merge merging two rows or two tables

TCL : Transaction Control Language


These commands are to keep a check on other commands and their affect on the database. These
commands can annul changes made by other commands by rolling back to original state. It can also make
changes permanent.

Command Description

commit to permanently save

rollback to undo change

savepoint to save temporarily

DCL : Data Control Language


Data control language provides command to grant and take back authority.

Command Description

grant grant permission of right

revoke take back permission.

DQL : Data Query Language

Command Description

select retrieve records from one or more table

create command
create is a DDL command used to create a table or a database.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 2
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Creating a Database
To create a database in RDBMS, create command is uses. Following is the Syntax,
create database database-name;

Creating a Table
create command is also used to create a table. We can specify names and datatypes of various columns
along.Following is the Syntax,
create table command will tell the database system to create a new table with given table name and
column information.
create table table-name
{
column-name1 datatype1,
column-name2 datatype2,
column-name3 datatype3,
column-name4 datatype4
};

Alter command
alter command is used for alteration of table structures. There are various uses of alter command, such as,

 to add a column to existing table


 to rename any existing column
 to change datatype of any column or to modify its size.
 alter is also used to drop a column.

To Add Column to existing Table


Using alter command we can add a column to an existing table. Following is the Syntax,

alter table table-name add(column-name datatype);


Here is an Example for this,

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 3
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

alter table Student add(address char);


The above command will add a new column address to the Student table

To Add Multiple Column to existing Table


Using alter command we can even add multiple columns to an existing table. Following is the Syntax,

alter table table-name add(column-name1 datatype1, column-name2 datatype2, column-name


3 datatype3);
To Add column with Default Value
alter command can add a new column to an existing table with default values. Following is the Syntax,

alter table table-name add(column-name1 datatype1 default data);

To Modify an existing Column


alter command is used to modify data type of an existing column . Following is the Syntax,

alter table table-name modify(column-name datatype);

To Rename a column
Using alter command you can rename an existing column. Following is the Syntax,

alter table table-name rename old-column-name to column-name;

To Drop a Column
alter command is also used to drop columns also. Following is the Syntax,

alter table table-name drop(column-name);

SQL queries to Truncate, Drop or Rename a Table


truncate command
truncate command removes all records from a table. But this command will not destroy the table's
structure. When we apply truncate command on a table its Primary key is initialized. Following is its
Syntax,

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 4
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

truncate table table-name


truncate command is different from delete command. delete command will delete all the rows from a
table whereas truncate command re-initializes a table(like a newly created table).

drop command
drop query completely removes a table from database. This command will also destroy the table structure.
Following is its Syntax,

drop table table-name

rename query
rename command is used to rename a table. Following is its Syntax,

rename table old-table-name to new-table-name

DML command
Data Manipulation Language (DML) statements are used for managing data in database. DML commands
are not auto-committed. It means changes made by DML command are not permanent to database, it can
be rolled back.

1) INSERT command
Insert command is used to insert data into a table. Following is its general syntax,

INSERT into table-name values(data1,data2,..)


INSERT into Student values(102,'Alex',null);
Example to Insert Default value to a column
INSERT into Student values(103,'Chris',default)

2) UPDATE command
Update command is used to update a row of a table. Following is its general syntax,

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 5
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

UPDATE table-name set column-name = value where condition;


Example to Update multiple columns
UPDATE Student set s_name='Abhi',age=17 where s_id=103;
3) Delete command
Delete command is used to delete data from a table. Delete command can also be used with condition to
delete a particular row. Following is its general syntax,

DELETE from table-name;

Example to Delete all Records from a Table


DELETE from Student;
The above command will delete all the records from Student table.

Example to Delete a particular Record from a Table


Consider the following Student table

DELETE from Student where s_id=103;

TCL command
Transaction Control Language(TCL) commands are used to manage transactions in database.These are
used to manage the changes made by DML statements. It also allows statements to be grouped together
into logical transactions.

Commit command
Commit command is used to permanently save any transaaction into database.
Following is Commit command's syntax,

commit;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 6
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Rollback command
This command restores the database to last commited state. It is also use with savepoint command to
jump to a savepoint in a transaction.
Following is Rollback command's syntax,

rollback to savepoint-name;

Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that point
whenever necessary.
Following is savepoint command's syntax,

savepoint savepoint-name;

DCL Commands

Data Control Language(DCL) is used to control privilege in Database. To perform any operation in the
database, such as for creating tables, sequences or views we need privileges. Privileges are of two types,

 System : creating session, table etc are all types of system privilege.
 Object : any command or query to work on tables comes under object privilege.

DCL defines two commands,

 Grant : Gives user access privileges to database.


 Revoke : Take back permissions from user.

To Allow a User to create Session


grant create session to username;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 7
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

To Allow a User to create Table


grant create table to username;

To provide User with some Space on Tablespace to store Table


alter user username quota unlimited on system;

To Grant all privilege to a User


grant sysdba to username

To Grant permission to Create any Table


grant create any table to username

To Grant permission to Drop any Table


grant drop any table to username

To take back Permissions


revoke create table from username

WHERE clause
Where clause is used to specify condition while retriving data from table. Where clause is used mostly
with Select, Update and Delete query. If condititon specified by where clause is true then only the result
from table is returned.

Syntax for WHERE clause


SELECT column-name1,
column-name2,
column-name3,

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 8
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

column-nameN
from table-name WHERE [condition];

SELECT Query
Select query is used to retrieve data from a tables. It is the most used SQL query. We can retrieve
complete tables, or partial by mentioning conditions using WHERE clause.

Syntax of SELECT Query


SELECT column-name1, column-name2, column-name3, column-nameN from table-name;

Example to Select all Records from Table


A special character asterisk * is used to address all the data(belonging to all columns) in a
query. SELECT statement uses * character to retrieve all records from a table.

Example to Select particular Record based on Condition


SELECT * from Student WHERE s_name = 'Abhi';
Example to Perform Simple Calculations using Select Query
SELECT eid, name, salary+3000 from Employee;

Like clause
Like clause is used as condition in SQL query. Like clause compares data with an expression using
wildcard operators. It is used to find similar data from the table.

Wildcard operators
There are two wildcard operators that are used in like clause.

Percent sign % : represents zero, one or more than one character.

Underscore sign _ : represents only one character.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 9
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Example of LIKE clause


Consider the following Student table.
s_id s_Name age

101 Adam 15

102 Alex 18

103 Abhi 17

SELECT * from Student where s_name like 'A%';

The above query will return all records where s_name starts with character 'A'.
s_id s_Name age

101 Adam 15

102 Alex 18

103 Abhi 17

Example
SELECT * from Student where s_name like '_d%';

The above query will return all records from Student table where s_name contain 'd' as second character.
s_id s_Name age

101 Adam 15

Example
SELECT * from Student where s_name like '%x';

The above query will return all records from Student table where s_name contain 'x' as last character.
s_id s_Name age

102 Alex 18

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 10
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Order By Clause
Order by clause is used with Select statement for arranging retrieved data in sorted order.
The Order by clause by default sort data in ascending order.
To sort data in descending order DESC keyword is used with Order by clause.

Syntax of Order By

SELECT column-list|* from table-name order byasc|desc;

SELECT * from Emp order by salary;

Example of Order by DESC


Consider the Emp table described above,
SELECT * from Emp order by salary DESC;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 11
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Group By Clause
Group by clause is used to group the results of a SELECT query based on one or more columns. It is also
used with SQL functions to group the result from one or more tables.
Syntax for using Group by in a statement.

SELECT column_name, function(column_name)

FROM table_name

WHERE condition

GROUP BY column_name

Example of Group by in a Statement


Consider the following Emp table.
eid name age Salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 9000

405 Tiger 35 8000

Here we want to find name and age of employees grouped by their salaries
SQL query for the above requirement will be,
SELECT name, age from Emp group by salary

Result will be,


name age

Rohan 34

shane 29

anu 22

Example of Group by in a Statement with WHERE clause


Consider the following Emp table

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 12
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

eid name age Salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 9000

405 Tiger 35 8000

SQL query will be,


select name, salary from Emp where age > 25 group by salary

Result will be.


name salary

Rohan 6000

Shane 8000

Scott 9000

You must remember that Group By clause will always come at the end, just like the Order by clause.
HAVING Clause
having clause is used with SQL Queries to give more precise condition for a statement. It is used to
mention condition in Group based SQL functions, just like WHERE clause.
Syntax for having will be,
select column_name, function(column_name)

FROM table_name

WHERE column_name condition

GROUP BY column_name

HAVING function(column_name) condition

Example of HAVING Statement


Consider the following Sale table.
oid order_name previous_balance customer

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 13
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

11 ord1 2000 Alex

12 ord2 1000 Adam

13 ord3 2000 Abhi

14 ord4 1000 Adam

15 ord5 2000 Alex

Suppose we want to find the customer whose previous_balance sum is more than 3000.
We will use the below SQL query,
SELECT *

from sale group customer

having sum(previous_balance) > 3000

Result will be,


oid order_name previous_balance customer

11 ord1 2000 Alex

Distinct keyword
The distinct keyword is used with Select statement to retrieve unique values from the
table. Distinctremoves all the duplicate records while retrieving from database.

Syntax for DISTINCT Keyword


SELECTdistinct column-name from table-name;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 14
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Example
Consider the following Emp table.
eid name age Salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 10000

404 Scott 44 10000

405 Tiger 35 8000

select distinct salary from Emp;

The above query will return only the unique salary from Emp table
salary

5000

8000

10000

AND & OR operator


AND and OR operators are used with Where clause to make more precise conditions for fetching data
from database by combining more than one condition together.

AND operator
AND operator is used to set multiple conditions with Where clause.

Example of AND
Consider the following Emp table
eid name age salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 15
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

405 Tiger 35 9000

SELECT * from Emp WHERE salary < 10000 AND age > 25

The above query will return records where salary is less than 10000 and age greater than 25.
eid name age salary

402 Shane 29 8000

405 Tiger 35 9000

OR operator
OR operator is also used to combine multiple conditions with Where clause. The only difference between
AND and OR is their behaviour. When we use AND to combine two or more than two conditions, records
satisfying all the condition will be in the result. But in case of OR, atleast one condition from the
conditions specified must be satisfied by any record to be in the result.

Example of OR
Consider the following Emp table
eid name age salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

405 Tiger 35 9000

SELECT * from Emp WHERE salary > 10000 OR age > 25

The above query will return records where either salary is greater than 10000 or age greater than 25.
402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 16
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

405 Tiger 35 9000

SQL Functions
SQL provides many built-in functions to perform operations on data. These functions are useful while
performing mathematical calculations, string concatenations, sub-strings etc. SQL functions are divided
into two catagories,

Aggregrate Functions

Scalar Functions

Aggregrate Functions
These functions return a single value after calculating from a group of values.Following are some
frequently used Aggregrate functions.

1) AVG()
Average returns average value after calculating from values in a numeric column.
Its general Syntax is,
SELECT AVG(column_name) from table_name

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 17
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Example using AVG()


Consider following Emp table
eid name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query to find average of salary will be,


SELECT avg(salary) from Emp;

Result of the above query will be,


avg(salary)

8200

2) COUNT()
Count returns the number of rows present in the table either based on some condition or without
condition.
Its general Syntax is,
SELECT COUNT(column_name) from table-name

Example using COUNT()


Consider following Emp table
Eid name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 18
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL query to count employees, satisfying specified condition is,


SELECT COUNT(name) from Emp where salary = 8000;

Result of the above query will be,


count(name)

Example of COUNT(distinct)
Consider following Emp table
Eid name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query is,


SELECT COUNT(distinct salary) from emp;

Result of the above query will be,


count(distinct salary)

3) FIRST()
First function returns first value of a selected column
Syntax for FIRST function is,
SELECT FIRST(column_name) from table-name

Example of FIRST()
Consider following Emp table
Eid name age salary

401 Anu 22 9000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 19
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query
SELECT FIRST(salary) from Emp;

Result will be,


first(salary)

9000

4) LAST()
LAST return the return last value from selected column
Syntax of LAST function is,
SELECT LAST(column_name) from table-name

Example of LAST()
Consider following Emp table
Eid Name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query will be,


SELECT LAST(salary) from emp;

Result of the above query will be,


last(salary)

8000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 20
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

5) MAX()
MAX function returns maximum value from selected column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name

Example of MAX()
Consider following Emp table
Eid Name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query to find Maximum salary is,


SELECT MAX(salary) from emp;

Result of the above query will be,


MAX(salary)

10000

6) MIN()
MIN function returns minimum value from a selected column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name

Example of MIN()
Consider following Emp table,
Eid name age salary

401 Anu 22 9000

402 Shane 29 8000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 21
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query to find minimum salary is,


SELECT MIN(salary) from emp;

Result will be,


MIN(salary)

6000

7) SUM()
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name

Example of SUM()
Consider following Emp table
Eid name age salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

SQL query to find sum of salaries will be,


SELECT SUM(salary) from emp;

Result of above query is,


SUM(salary)

41000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 22
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Scalar Functions
Scalar functions return a single value from an input value. Following are soe frequently used Scalar
Functions.
1) UCASE()
UCASE function is used to convert value of string column to Uppercase character.
Syntax of UCASE,
SELECT UCASE(column_name) from table-name

Example of UCASE()
Consider following Emp table
Eid name age salary

401 anu 22 9000

402 shane 29 8000

403 rohan 34 6000

404 scott 44 10000

405 Tiger 35 8000

SQL query for using UCASE is,


SELECT UCASE(name) from emp;

Result is,
UCASE(name)

ANU

SHANE

ROHAN

SCOTT

TIGER

2) LCASE()
LCASE function is used to convert value of string column to Lowecase character.
Syntax for LCASE is,
SELECT LCASE(column_name) from table-name

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 23
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Example of LCASE()
Consider following Emp table
Eid name age salary

401 anu 22 9000

402 shane 29 8000

403 rohan 34 6000

404 scott 44 10000

405 Tiger 35 8000

SQL query for converting string value to Lower case is,


SELECT LCASE(name) from emp;

Result will be,


LCASE(name)

Anu

Shane

Rohan

Scott

Tiger

3) MID()
MID function is used to extract substrings from column values of string type in a table.
Syntax for MID function is,
SELECT MID(column_name, start, length) from table-name

Example of MID()
Consider following Emp table
Eid name age salary

401 anu 22 9000

402 shane 29 8000

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 24
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

403 rohan 34 6000

404 scott 44 10000

405 Tiger 35 8000

SQL query will be,


select MID(name,2,2) from emp;

Result will come out to be,


MID(name,2,2)

Nu

Ha

Oh

Co

Ig

4) ROUND()
ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal
point values. Syntax of Round function is,
SELECT ROUND(column_name, decimals) from table-name

Example of ROUND()
Consider following Emp table
Eid name age salary

401 anu 22 9000.67

402 shane 29 8000.98

403 rohan 34 6000.45

404 scott 44 10000

405 Tiger 35 8000.01

SQL query is,


SELECT ROUND(salary) from emp;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 25
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Result will be,


ROUND(salary)

9001

8001

6000

10000

8000

Query: Find all the students who can graduate (i.e. who have taken all the subjects required for one to
graduate)
Unfortunately, there is no direct way in which we can express the division operator. Let's walk through
step by step how to write the query for the division operator.

1. Find all the students


Create a set of all students that have taken courses. This can be done easily by the following
command.
Create table AllStudents as SELECT DISTINCT Student_Name FROM Course_Taken

This gives the following table AllStudents:

Joins in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data.
SQL Join is used for combining column from two or more tables by using values common to both
tables. Join Keyword is used in SQL queries for joining two or more tables. Minimum required condition
for joining table, is (n-1) where n, is number of tables. A table can also join to itself known as, Self Join.

Types of Join
The following are the types of JOIN that we can use in SQL.

1. Inner Join

Natural Join,Equi Join(=),Conditional Join

2. Outer Join

Left Outer Join,Right Outer Join,Full Outer Join

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 26
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Cross JOIN or Cartesian Product


This type of JOIN returns the cartesian product of rows from the tables in Join. It will return a table which
consists of records which combines each row from the first table with each row of the second table.
Cross JOIN Syntax is,
SELECT column-name-list

from table-name1

CROSS JOIN

table-name2;

INNER Join or EQUI Join


This is a simple JOIN in which the result is based on matched data as per the equality condition specified
in the query.
Inner Join Syntax is,
SELECT column-name-list

from table-name1

INNER JOIN

table-name2

WHERE table-name1.column-name = table-name2.column-name;

Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same datatype
present in both the tables to be joined.
Natural Join Syntax is,
SELECT *

from table-name1

NATURAL JOIN

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 27
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

table-name2;

Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,

Left Outer Join

Right Outer Join

Full Outer Join

Left Outer Join


The left outer join returns a result table with the matched data of two tables then remaining rows of
the left table and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list

from table-name1

LEFT OUTER JOIN

table-name2

on table-name1.column-name = table-name2.column-name;

Left outer Join Syntax for Oracle is,


select column-name-list

from table-name1,

table-name2

on table-name1.column-name = table-name2.column-name(+);

Right Outer Join


The right outer join returns a result table with the matched data of two tables then remaining rows of
the right table and null for the left table's columns.
Right Outer Join Syntax is,
select column-name-list

from table-name1

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 28
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

RIGHT OUTER JOIN table-name2 on table-name1.column-name = table-name2.column-


name;

Full Outer Join


The full outer join returns a result table with the matched data of two table then remaining rows of
both left table and then the right table.
Full Outer Join Syntax is,
select column-name-list

from table-name1

FULL OUTER JOIN

table-name2

on table-name1.column-name = table-name2.column-name;
SQL Alias

Alias is used to give an alias name to a table or a column. This is quite useful in case of large or complex
queries. Alias is mainly used for giving a short alias name for a column or a table with complex names.
Syntax of Alias for table names,
SELECT column-name

from table-name

as alias-name

Following is an Example using Alias,


SELECT * from Employee_detail as ed;

Alias syntax for columns will be like,


SELECT

column-name as alias-name

from

table-name

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 29
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Set Operation in SQL


SQL supports few Set operations to be performed on table data. These are used to get meaningful results
from data, under different special conditions.

Union
UNION is used to combine the results of two or more Select statements. However it will eliminate
duplicate rows from its result set. In case of union, number of columns and datatype must be same in both
the tables.

Union SQL query will be,


select * from First

UNION

select * from second

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 30
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Union All
This operation is similar to Union. But it also shows the duplicate rows.

select * from First

UNION ALL

select * from second

Intersect
Intersect operation is used to combine two SELECT statements, but it only retuns the records which are
common from both SELECT statements. In case of Intersect the number of columns and datatype must
be same. MySQL does not support INTERSECT operator.

Intersect query will be,


select * from First

INTERSECT

select * from second

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 31
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

The result table will look like


ID NAME

2 adam

Minus
Minus operation combines result of two Select statements and return only those result which belongs to
first set of result. MySQL does not support INTERSECT operator.

Minus query will be,


select * from First

MINUS

select * from second

SQL View
A view in SQL is a logical subset of data from one or more tables. View is used to restrict data access.
Syntax for creating a View,
CREATE or REPLACE view view_name AS

SELECT column_name(s)

FROM table_name

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 32
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

WHERE condition

Example of Creating a View


Consider following Sale table,
oid order_name previous_balance customer

11 ord1 2000 Alex

12 ord2 1000 Adam

13 ord3 2000 Abhi

14 ord4 1000 Adam

15 ord5 2000 Alex

SQL Query to Create View


CREATE or REPLACE view sale_view as select * from Sale where customer = 'Alex';

The data fetched from select statement will be stored in another object called sale_view. We can use
create seperately and replace too but using both together works better.

Example of Displaying a View


Syntax of displaying a view is similar to fetching data from table using Select statement.
SELECT * from sale_view;

Force View Creation


force keyword is used while creating a view. This keyword force to create View even if the table does not
exist. After creating a force View if we create the base table and enter values in it, the view will be
automatically updated.
Syntax for forced View is,
CREATE or REPLACE forceviewview_name AS

SELECT column_name(s)

FROM table_name

WHERE condition

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 33
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Update a View
Update command for view is same as for tables.
Syntax to Update a View is,
UPDATE view-name

set value

WHERE condition;

If we update a view it also updates base table data automatically.

Read-Only View
We can create a view with read-only option to restrict access to the view.
Syntax to create a view with Read-Only Access
CREATE or REPLACE forceviewview_name AS

SELECT column_name(s)

FROM table_name

WHERE condition with read-only

The above syntax will create view for read-only purpose, we cannot Update or Insert data into read-only
view. It will throw an error.

Types of View
There are two types of view,

Simple View

Complex View
Simple View Complex View

Created from one table Created from one or more table

Does not contain functions Contain functions

Does not contain groups of data Contains groups of data

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 34
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

WEEK1

1. Create Student table with the following fields and describe the table (id: integer,
firstname: string, lastname :string, email:string ,yearofBirth: integer)

SQL> create table student(id number,firstname varchar(15),lastname varchar(15),Email


varchar(20),yearofbirth number);

QUERY EXPLANATION:To create table named STUDENT with columns


firstname,id,lastname,email,yearofbirth.

Table created.

SQL> desc student

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

ID NUMBER

FIRSTNAME VARCHAR2(15)

LASTNAME VARCHAR2(15)

EMAIL VARCHAR2(20)

YEAROFBIRTH NUMBER

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 35
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

2.INSERT DATA INTO STUDENT TABLE?

SQL> insert into student values(2,'rak','nat','[email protected]',1999);

QUERY EXPLANATION:TO insert data into student table with specified values in columns.

1 row created.

SQL> insert into student values(3,'vikas','to','[email protected]',1998);

1 row created.

SQL> insert into student values(4,'arun','van','[email protected]',1997);

1 row created.

SQL> insert into student values(5,'koushik','ten','[email protected]',1995);

1 row created

SQL> insert into student values(5,'varun','mat','[email protected]',1998);

1 row created.

SQL> insert into student values(8,'raj','tr','[email protected]',1998);

1 row created

3.DISPLAY DATA FROM TABLE?

SQL> select * from student;

QUERY EXPLANATION:SQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.

ID FIRSTNAME LASTNAME EMAIL YEAROFBIRTH

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

2 rak nat [email protected] 1999

3 vikas to [email protected] 1998

4 arun van [email protected] 1997

5 koushik ten [email protected] 1995

5 varun mat [email protected] 1998

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 36
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

8 raj tr [email protected] 1998

6 rows selected.

4,ADD A COLMN PHONE NUMBER TO STUDENT TABLE?

SQL> alter table student add(phone number(10));

QUERY EXPLANATION:ALTER keyword is used here to add PHONENUMBER column to


existing student table.

Table altered.

SQL> desc student;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

ID NUMBER

FIRSTNAME VARCHAR2(15)

LASTNAME VARCHAR2(15)

EMAIL VARCHAR2(20)

YEAROFBIRTH NUMBER

PHONE NUMBER(10)

6.REMOVE ID COLUMN FROM STUDENT TABLE?

SQL> alter table student drop(ID);

QUERY EXPLANATION:ALTER keyword is used here to delete ID column to existing student


table.

Table altered.

SQL> desc student;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 37
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Name Null? Type

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

FIRSTNAME VARCHAR2(15)

LASTNAME VARCHAR2(15)

EMAIL VARCHAR2(20)

YEAROFBIRTH NUMBER

PHONE NUMBER(10)

7.MODIFY THE ID COLUMN DATATYPE TO VARCHAR?

SQL> alter table student add(ID varchar(2));

QUERY EXPLANATION:ALTER keyword is used here to add ID column with VARCHAR to


existing student table.

Table altered.

SQL> desc student;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

FIRSTNAME VARCHAR2(15)

LASTNAME VARCHAR2(15)

EMAIL VARCHAR2(20)

YEAROFBIRTH NUMBER

PHONE NUMBER(10)

ID VARCHAR2(2)

8.DROP YEAROFBIRTH COLUMN FROM STUDENT?

SQL> alter table student drop(YEAROFBIRTH);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 38
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

QUERY EXPLANATION:ALTER keyword is used here toDELETE YEAROFBIRTH column


to existing student table.

Table altered.

SQL> desc student;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

FIRSTNAME VARCHAR2(15)

LASTNAME VARCHAR2(15)

EMAIL VARCHAR2(20)

PHONE NUMBER(10)

ID VARCHAR2(2)

9.ADD A dateofbirth column and adress columns to student table.

SQL> alter table student add(DOB DATE,address varchar(19));

QUERY EXPLANATION:ALTER keyword is used here to add ID column with VARCHAR to


existing student table.

Table altered.

SQL> desc student;

Name Null? Type

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

FIRSTNAME
VARCHAR2(15)

LASTNAME

VARCHAR2(15)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 39
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

EMAIL
VARCHAR2(20)

PHONE NUMBER(10)

ID VARCHAR2(2)

DOB DATE

ADDRESS
VARCHAR2(19)

10.CHANGE NAME OF COLUMN ADDRESS TO LOCATION

SQL> alter table student rename column ADDRESS to Location;

QUERY EXPLANATION:ALTER keyword is used here to RENAME ADDRESS to Location


IN student table.

Table altered.

SQL> desc student;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

FIRSTNAME
VARCHAR2(15)

LASTNAME
VARCHAR2(15)

EMAIL
VARCHAR2(20)

PHONE NUMBER(10)

ID VARCHAR2(2)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 40
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

DOB DATE

LOCATION
VARCHAR2(19

11.INSERT DATA INTO TABLES

SQL> insert into student(ID,PHONE) values(2,5566544);

QUERY EXPLANATION:TO insert data into student table with specified values in columns.

1 row created

SQL> insert into student(ID,PHONE) values(3,5966544);

1 row created.

SQL> insert into student(ID,PHONE) values(4,6966544);

1 row created.

SQL> insert intostudent(ID,PHONE) values(5,686544);

1 row created.

11.1 ADD A DEFAULT VALUE TO COLUMN

SQL> alter table student add(branch varchar(20) default 'cse');

QUERY EXPLANATION:To add a column with default value ‘cse’.

SQL> select * from student;

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

PHONE ID DOB LOCATION BRANCH

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

5566544 1 CSE

55966544 6 CSE

5566544 2 CSE

5966544 3 CSE

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 41
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

PHONE ID DOB LOCATION BRANCH

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

6966544 4 CSE

686544 5 CSE

13 rows selected.

12.UPDATE STUDENT TABLE VALUE?

SQL> update student set DOB='03-feb-98' where ID=4;

QUERY EXPLANATION:The SQL UPDATE Query is used to modify the existing record
column:DOB=03-feb-98 whose id valueis 4.

1 row updated.

SQL> select * from student;

QUERY EXPLANATION:SQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.

PHONE ID DOB LOCATION BRANCH

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

5566544 1 CSE

55966544 6 CSE

5566544 2 CSE

5966544 3 CSE

6966544 4 03-FEB-98 CSE

686544 5 CSE

13 rows selected.

SQL> alter table student add(Name varchar(6));

Table altered.

SQL> select * from student;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 42
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

QUERY EXPLANATION:SQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.

PHONE ID DOB LOCATION BRANCH NAME

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

5566544 1 CSE

55966544 6 CSE

5566544 2 CSE

5966544 3 CSE

6966544 4 03-FEB-98 CSE

686544 5 CSE

13 rows selected

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 43
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

List-1

1. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer)

SQL> create table sailors(sid number,sname varchar(7),Rating number);

QUERY EXPLANATION:It creates a table sailors with columns:sid,sname,Rating with their


datatypes.

Table created.

SQL> desc sailors;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

2. Add field (age: integer) to the Sailors table and describe the table

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 44
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> alter table sailors add(age integer);

QUERY EXPLANATION:ALTER keyword is used here to add SGE column with


datatype:INTEGER to existing table.

Table altered.

SQL> desc sailors;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions for
specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

AGE NUMBER(38).

3. Modify field age to real in the Sailors table and describe the table

SQL> alter table sailors modify(age float);

QUERY EXPLANATION:ALTER keyword is used here to modify AGE column with float
datatype to existing table.

Table altered.

SQL> desc sailors;

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

AGE FLOAT(126)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 45
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

4. Delete age field from the Sailors table and describe the table

SQL> ALTER TABLE sailors DROP COLUMN age;

QUERY EXPLANATION:ALTER keyword is used here to remove age column to existing


table.

Table altered.

SQL> desc sailors;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

5. Add field (age: real) to the Sailors table and describe the table

SQL> alter table sailors add(age real);

QUERY EXPLANATION:ALTER keyword is used here to add AGE column with FLOAT
datatype to existing student table.

Table altered.

SQL> desc sailors;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 46
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

RATING NUMBER

AGE FLOAT(63)

6. Delete Sailors table

SQL> drop table sailors;

QUERY EXPLANATION:The SQL DROP TABLE statement is used to remove a table definition
and all the data, indexes, triggers, constraints and permission specifications for that table.

Table dropped.

7. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

SQL> create table sailors(sid number,sname varchar(7),Rating number,age real);

QUERY EXPLANATION:It creates a table sailors with columns:sid,sname,Rating with their


datatypes.

Table created.

SQL> desc sailors;

QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions for
specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

AGE FLOAT(63)

8. Insert the following records in to the Sailors table

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 47
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into sailors values(22,'dustin',7,45.0);

QUERY EXPLANATION:it entries data into sid:22,sname:dustin,rating:7,age:45

1 row created.

SQL> insert into sailors values(29,'brutus',1,33.0);

1 row created

SQL> insert into sailors values(31,'lubber',8,55.5);

1 row created.

SQL> insert into sailors values(31,'lubber',8,55.5);

1 row created.

SQL> insert into sailors values(31,'lubber',8,55.5);

1 row created.

SQL> insert into sailors values(32,'andy',8,25.5);

1 row created.

SQL> insert into sailors values(32,'andy',8,25.5);

1 row created.

SQL> insert into sailors values(58,'rusty',10,35.0);

1 row created.

SQL> insert into sailors values(71,'zorba',10,16.0);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 48
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

SQL> insert into sailors values(74,'Horatio',9,35.0);

1 row created.

SQL> insert into sailors values(85,'art',3,25.5);

1 row created.

SQL> insert into sailors values(95,'bob',3,63.5);

1 row created.

9. Retrieve all Records from the Sailors table

SQL> select * from sailors;

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

SID SNAME RATING AGE

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

22 dustin 7 45

29 brutus 1 33

32 andy 8 25.5

32 andy 8 25.5

58 rusty 10 35

71 zorba 10 16

74 Horatio 9 35

85 art 3 25.5

95 bob 3 63.5

31 lubber 8 55.5

10 rows selected.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 49
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

10.Retrieve all Records (unique) from the Sailors table .

SQL> SELECT DISTINCT * FROM sailors;

QUERY EXPLANATION:Distinct keyword is used with Select keyword to diplay unique records.

SID SNAME RATING AGE

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

22 dustin 7 45

29 brutus 1 33

32 andy 8 25.5

58 rusty 10 35

71 zorba 10 16

74 Horatio 9 35

85 art 3 25.5

95 bob 3 63.5

31 lubber 8 55.5

9 rows selected.

11. Retrieve the names and ages of all sailors from the Sailors table

SQL> select * from sailors;

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

SNAME AGE

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

dustin 45

brutus 33

andy 25.5

andy 25.5

rusty 35
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 50
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

zorba 16

Horatio 35

art 25.5

bob 63.5

lubber 55.5

12. Create Sailors1 table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

SQL> create table sailors1 as (select * from sailors);

QUERY EXPLANATION:The keyword is also used with a different meaning to create aliases
for tables and fields in a query.

SQL> desc sailors;

DESC statement use for describe the List of column definitions for specified TABLE.

Name Null? Type

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

SID NUMBER

SNAME VARCHAR2(7)

RATING NUMBER

AGE FLOAT(63)

13. Create Boats table with the following fields and describe the table (bid: integer, bname:
string, color: string)

SQL> create table boats (bid number,bname varchar(9),color varchar(5));

Table created.

14. Insert the following records in to the Boats table

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 51
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into boats values(101,'Interlake','blue');

1 row created.

SQL> insert into boats values(102,'Interlake','red');

1 row created.

SQL> insert into boats values(103,'Clipper','green');

1 row created.

SQL> insert into boats values(104,'Marine','red');

1 row created.

15. Create Reserves table with the following fields and describe the table (sid: integer, bid:
integer, day: date)

SQL> create table reserves(sid int,bid int,day date);

Table created.

15. Insert the following records in to the Reserves table

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 52
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into reserves values(22,101,'10-oct-98');

QUERY EXPLANATION:to insert data into tables as sid

:22,bid:101,day:10/10/98.

1 row created.

SQL> insert into reserves values(22,102,'10-oct-98');

1 row created.

SQL> insert into reserves values(22,103,'10-aug-98');

1 row created.

SQL> insert into reserves values(22,104,'10-jul-98');

1 row created.

SQL> insert into reserves values(31,102,'11-oct-98');

1 row created.

SQL> insert into reserves values(31,103,'11-jun-98');

1 row created.

SQL> insert into reserves values(31,104,'11-dec-98');

1 row created.

SQL> insert into reserves values(64,101,'9-may-98');

1 row created.

SQL> insert into reserves values(64,102,'9-aug-98');

1 row created.

SQL> insert into reserves values(74,103,'9-aug-98');

1 row created.

16. Retrieve all Records from the Reserves table .

SQL> select * from reserves;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 53
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

SID BID DAY

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

22 101 10-OCT-98

22 102 10-OCT-98

22 103 10-AUG-98

22 104 10-JUL-98

31 102 11-OCT-98

31 103 11-JUN-98

31 104 11-DEC-98

64 101 09-MAY-98

64 102 09-AUG-98

74 103 09-AUG-98

10 rows selected.

17.Find all sailors with a rating above 7

SQL> select * from sailors where (rating>7);

To display the record of a sailor whose rating is greater than 7.

SID SNAME RATING AGE

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

31 lubber 8 55.5

32 andy 8 25.5

58 rusty 10 35

71 zorba 10 16

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 54
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

74 Horatio 9 35

18.Delete sailor from the Sailors1 table whose sid is 58

SQL> delete from sailors1 where(sid=58);

DELETE:To delete a record from table whose sid=58

1 row deleted.

19.Retrieve all Records from the Sailors1 table

SQL> select * from sailors1;

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

SID SNAME RATING AGE

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

22 dustin 7 45

29 brutus 1 33

31 lubber 8 55.5

32 andy 8 25.5

64 Horatio 7 35

71 zorba 10 16

74 Horatio 9 35

85 art 3 25.5

95 bob 3 63.5

20.Retrieve all Records from the Boats table

SQL> select * from boats;

SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.

BID BNAME COLOR

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

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 55
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

101 Interlake blue

102 Interlake red

103 Clipper green

104 Marine red

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 56
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

List-2
1. Find the names of sailors who have reserved boat number 103 (using AND operator)

SQL> select s.sname from sailors s,reserves r where s.sid=r.sid and r.bid=103;

QUERY EXPLANATION:we created an objects s,r for sailors,reserves and referring with their
sid’s and finding the name of sailor whose bid=103

SNAME

-------

dustin

Horatio

lubber

2. Find all sailors from sailors and sailors1 table by using INTERSECT operator

The INTERSECT query will return the records in the blue shaded area. These are the records
that exist in both Dataset1 and Dataset2.

SQL> select * from sailors intersect select * from sailors1;

SID SNAME RATING AGE

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

22 dustin 7 45

29 brutus 1 33

31 lubber 8 55.5

32 andy 8 25.5

58 rusty 10 35

71 zorba 10 16

74 Horatio 9 35

85 art 3 25.5

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 57
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

95 bob 3 63.5

9 rows selected.

3. Find all sailors from sailors and sailors1 table by using MINUS operator

SQL> select * from sailors minus select * from sailors1;

no rows selected.

4.Find the names of sailors who have reserved boat 103 (using nested query)

SQL> select s.sname from sailors s where s.sid in(select r.sid from reserves r where r.bid=103 );

WHERE IN returns values that matches values in a list or subquery as name of sailor who
reserved boat 103.

SNAME

-------

dustin

lubber

Horatio

5.Find the names of sailors who have reserved a blue boat (using nested query )

SQL> select s.sname from sailors s where s.sid in (select r.sid from reserves r where r.bid
in(select b.bid from boats b where b.color='blue'));

SNAME

-------

dustin

6.Find the names of sailors who have not reserved a red boat (using nested query)

SQL> select s.sname from sailors s where s.sid in (select r.sid from reserves r where r.bid
in(select b.bid from boats b where b.color!='red'));

The WHERE IN returns values that matches values in a list or subquery that is who reserved a
red boat.

SNAME

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 58
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

-------

dustin

Horatio

lubber

7.Find sailors whose rating is better than some sailor called Horatio (using ANY operator)

SQL> select sname from sailors where rating>any(select rating from sailors where
sname='Horatio');

ANY operator returns true if any of the subquery values meet the condition and select command
displays the desired values.

SNAME

-------

rusty

zorba

8. Find sailors whose rating is better than some sailor called Horatio (using ALL operator)

SQL> select sname from sailors where rating>all(select rating from sailors where
sname='andy');

The select command displays the desired values ,ALL operator returns true if any of the
subquery values meet the condition.

SNAME

-------

rusty

zorba

Horatio

9.Find the average age of all sailors

SQL> select avg(age) from sailors;

It displays the average of column values of specified age column.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 59
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

AVG(AGE)

----------

35.95

10.Find the minimum age of the sailor

SQL> select min(age) from sailors;

The MIN() function returns the smallest value of the selected column.

MIN(AGE)

----------

16

11. Find the maximum age of the sailor

SQL> select max(age) from sailors;

The MAX() function returns the smallest value of the selected column.

MAX(AGE)

---------

63.5

12. Find the sailors with the highest rating

SQL> select max(rating) from sailors;

The MAX() function returns the smallest value of the selected column.

MAX(RATING)

-----------

10

13. Count the number of sailors

SQL> select count(sname) from sailors;

The COUNT() function returns the number of rows that matches a specified criteria.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 60
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

COUNT(SNAME)

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

10

14.Count the number of different sailor names

SQL> select count( distinct sname) from sailors;

COUNT(DISTINCTSNAME)

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

15. Find all sailors whose age is in between 45.0 and 63.5 (using BETWEEN operator).

SQL> select * from sailors where age between 45 and 63.5;

SID SNAME RATING AGE

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

22 dustin 7 45

31 lubber 8 55.5

95 bob 3 63.5

16. Find all sailors whose age is in the list of values(15.0,33.2,45.7,63.5) (using IN operator)

SQL> select * from sailors where age in (15,33.2,45.7,63.5);

The IN operator allows you to specify multiple values in a WHERE clause.

SID SNAME RATING AGE

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

95 bob 3 63.5

17. Find all sailors whose second letter in the names contain ‘u’

SQL> select * from sailors where sname like '_u%';

The SQL LIKE clause is used to compare a value to similar values whose second letter is u.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 61
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SID SNAME RATING AGE

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

22 dustin 7 45

31 lubber 8 55.5

58 rusty 10 35

18. Find all sailors whose first letter and third letter in the names are ‘A’ and ‘d’

SQL> select * from sailors where sname like 'a_d%';

The SQL LIKE clause is used to compare a value to similar values in query specified.

SID SNAME RATING AGE

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

32 andy 8 25.5

19. Find all sailors from sailors and sailors1 table by using UNION operator

SQL> select * from sailors union select * from sailors1;

When multiple SELECT queries are joined using UNION operator, It displays the combined
result from all the compounded SELECT queries.

SID SNAME RATING AGE

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

22 dustin 7 45

29 brutus 1 33

31 lubber 8 55.5

32 andy 8 25.5

58 rusty 10 35

71 zorba 10 16

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 62
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

74 Horatio 9 35

85 art 3 25.5

95 bob 3 63.5

9 rows selected.

Week-4

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 63
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

CREATE AN ER DIGRAM WITH GIVEN ENTITIES

1. CREATE AN ER DIGRAM WITH GIVEN ENTITIES

2.create Tables according to er diagram.

SQL> create table course(course_id number,course_name varchar(9),primary key(course_id));

It creates a table “course” with course_name as primary key and with columns course_id,course_name.

Table created.

SQL> create table lecturer(lecturer_id number,lecturer_name varchar(8),course_id number,primary


key(lecturer_id),foreign key(course_id) references course(course_id));

It creates a table “Lecturer” with course_name as primary key and with columns
lecturer_id,lecturer_name.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 64
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Table created.

SQL> create table subjects(subject_id number,subject_name varchar(8),lecturer_id number,primary


key(subject_id),foreign key(lecturer_id) references lecturer(lecturer_id));

It creates a table subjects with primary key as subject_id,foreign key as lecturer id.

Table created.

SQL> create table student(student_id number,student_name varchar(6),dob date,door_no number,street


varchar(3),city varchar(3),pin varchar(4),course_id number,primary key(student_id),foreign
key(course_id) references course(course_id));

t creates a table “student” with student_id as primary key,foreign key as course_id and with columns
course_id,student_id,student_name,street,city,pin.

Table created.

SQL> create table hobby(student_id number,hobby varchar(8),primary key(student_id));

It creates a table “Hobby” with Student_id as primary key and with columns student_id,hobby.

Table created.

2.insert values into course table.

SQL> insert into course values(56,'maths');

To insert values into specified course table with columns specified to be inserted.

1 row created.

SQL> insert into course values(59,'cse');

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 65
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

SQL> insert into course values(23,’it’);

1 row created.

SQL> insert into course values(54,'mech');

1 row created.

SQL> insert into course values(32,'cse');

1 row created..

3.Insert into Lecturer values.

SQL> insert into lecturer values(1,'john',56);

To insert values into specified course table with columns specified to be inserted.

1 row created.

SQL> insert into lecturer values(1,'james',59);

1 row created.

SQL> insert into lecturer values(1,'ron',23);

1 row created.

SQL> insert into lecturer values(1,'kat',54);

1 row created.

SQL> insert into lecturer values(1,'sam',32);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 66
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> select * from course;

To display all the rows and column values in specified table.

COURSE_ID COURSE_NAME

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

56 it

59 eee

23 ece

54 cse

32 mech

SQL> select * from lecturer;

To display all the rows and column values in specified table.

LECTURER_ID LECTURER COURSE_ID

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

1 john 56

2 james 59

3 ron 23

4 kat 54

5 sam 32

SQL> desc subjects;

It is used to display the structure of the tables and views.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 67
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Name Null? Type

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

SUBJECT_ID NOT NULL NUMBER

SUBJECT_NAME VARCHAR2(8)

LECTURER_ID NUMBER

SQL> insert into lecturer values(1,'john',56);

1 row created.

SQL> insert into lecturer values(2,'james',59);

1 row created.

SQL> insert into lecturer values(3,'ron',23);

1 row created.

SQL> insert into lecturer values(4,'kat',54);

1 row created.

SQL> insert into lecturer values(5,'sam',32);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 68
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

INSERT INTO SUBJECT TABLE.

SQL> insert into subjects values(1,'os',1);

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

1 row created.

SQL> insert into subjects values(2,'java',2);

1 row created.

SQL> insertI nto subjects values(3,'dbms',3);

1 row created.

SQL> insert into subjects values(4,'c',4);

1 row created.

SQL> insert into subjects values(5,'cpp',5);

1 row created.

SQL> insert into student values(1,'arun','16-jan-19',87,'res','hyd','89',59);

1 row created.

SQL> insert into student values(2,'vikas','16-feb-19',86,'r','nzb','80',23);

1 row created.

SQL> insert into student values(3,'sai','16-mar-19',87,'p','sec','877',54);

1 row created.

SQL> insert into student values(4,'man','16-apr-19',32,'v','hyd','077',32);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 69
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

SQL> insert into student values(5,'jane','16-may-19',34,'n','adb','079',56);

1 row created.

SQL> insert into student values(6,'makx','17-may-19',34,'i','mdk','069',56);

1 row created.

SQL> insert into student values(7,'kane','4-may-19',35,'u','hyd','019',32);

1 row created.

SQL> desc student;

It is used to display the structure of the tables and views.

Name Null? Type

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

STUDENT_ID NOT NULL NUMBER

STUDENT_NAME VARCHAR2(6)

DOB DATE

DOOR_NO NUMBER

STREET VARCHAR2(3)

CITY VARCHAR2(3)

PIN VARCHAR2(4)

COURSE_ID NUMBER

SQL> select * from student;

The SQL SELECT statement is used to fetch the data from a student table which returns this data in the form of a result table.

STUDENT_ID STUDENT DOB DOOR_NO STREET CITY PIN COURSE_ID

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 70
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

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

1 arun 16-JAN-19 87 res hyd 89 59

2 vikas 16-FEB-19 86 r nzb 80 23

3 sai 16-MAR-19 87 p sec 877 54

4 man 16-APR-19 32 v hyd 077 32

5 jane 16-MAY-19 34 n adb 079 56

6 makx 17-MAY-19 34 i mdk 069 56

7 kane 04-MAY-19 35 u hyd 019 32

7 rows selected.

7.Insert into Hobby values.

SQL> insert into hobby values(1,'swim');

To insert records into hobby table with given values.

1 row created.

SQL> insert into hobby values(2,'chess');

1 row created.

SQL> insert into hobby values(3,'hockey');

1 row created.

SQL> insert into hobby values(4,'baseball');

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 71
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into hobby values(5,'football');

1 row created.

SQL> insert into hobby values(6,'gaming');

1 row created.

SQL> insert into hobby values(7,'music');

1 row created.

8.DISPLAY STUDENT NAMES, LECTURER NAMES ,SUBJECTS FROM their TABLES


RESPECTIVELY

SQL> select student_name from student;

The SQL SELECT statement is used to fetch Student_name from a student table which returns this data in
the form of a result table.

STUDENT

------

arun

vikas

sai

man

jane

makx

kane

7 rows selected.

SQL> select lecturer_name from lecturer;

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 72
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

The SQL SELECT statement is used to fetch the lecturer_name from a lecturer table which returns this
data in the form of a result table.

LECTURER

--------

john

james

ron

kat

sam

SQL> select subject_name from subjects;

The SQL SELECT statement is used to fetch the subject_name from a database table which returns this
data in the form of a result table.

SUBJECT_

--------

os

java

dbms

cpp

9.Select the lecturer id of the instructor who teaches os subject

SQL> select lecturer_id,course_id from lecturer where lecturer_id in(select lecturer_id from subjects
where subject_name='os');

The SQL SELECT statement is used to fetch the lecturer_id,course_id from a lecturer table where
Condition is given in IN clause who teaches os subject.

LECTURER_ID COURSE_ID

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

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 73
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 56

10.Select the name of lecturer who teaches OS subject.

SQL> select lecturer_name,course_id from lecturer where lecturer_id in(select lecturer_id from subjects
where subject_name='os');

The SQL SELECT statement is used to fetch the lecturer_id,course_id from a lecturer table where
Condition is given in IN clause who teaches os subject.

LECTURER COURSE_ID

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

john 56

11.Select the name of student who is cse department.

SQL> select student_name from student where course_id in(select course_id from course where
course_name='cse');

The SQL SELECT statement is used to fetch the lecturer_id,course_id from a lecturer table where
Condition is given in IN clause who teaches os subject.

STUDENT

------

sai

12.Find lecturer who dealt with ‘it ‘department.

SQL> select course_name from course where course_id in(select course_id from lecturer where
lecturer_name='john');

The SQL SELECT statement is used to fetch the lecturer_id,course_id from a lecturer table where
Condition is given in IN clause who dealt with ‘it ‘department.

COURSE_NAME

---------

it

13.Find lecturer dealt with C subject.

SQL> select lecturer_name from lecturer where lecturer_id in(select lecturer_id from subjects where
subject_name='c');

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 74
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

The SQL SELECT statement is used to fetch the lecturer_id,course_id from a lecturer table where
Condition is given in IN clause who dealt with C subject.

LECTURER

--------

kat

14.fIND SUBJECT NAME WITH LECTURER ID AS 1.

SQL> select subject_name from subjects where lecturer_id in(select lecturer_id from lecturer where
lecturer_id=1);

The SQL SELECT statement is used to fetch the data from a database table which returns this data based
on WHERE clause condition in the form of a result table.

SUBJECT_NAME

--------

os

15.Select student with courseID.

SQL> select student_name from student where course_id=56;

The SQL SELECT statement is used to fetch the data from a database table which returns this data based
on WHERE clause condition in the form of a result table.

STUDENT

------

jane

makx

16.Select adress of student whose lecturer is john and belongs to it department.

SQL> select door_no,street,city,pin from student where course_id in(select course_id from lecturer where
lecturer_name='john' and course_id in (select course_id from course where course_name='it'));

The SQL SELECT statement is used to fetch the data from a database table which returns this data based
on the data presented in IN CLAUSE VALUES in the form of a result table.

DOOR_NO STREET CITY PIN

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

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 75
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

34 n adb 079

34 i mdk 069

CONSTRAINTS

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 76
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1. Create a table for the following relation


Customer(cid:int,name:String,age:int,Address:String,Salary:float).

SQL> create table customer(cid number,name varchar(20),age number,address


varchar(20),salary float);
It creates a customer table with cid, name, age, address, salary as attributes of tables with
number, varchar, number, varchar, float as data types respectively.

Table created.

2. Apply NOT NULL constraint on name, age and salary

SQL> alter table customer modify(name varchar(20) not null,age number not null,salary
float not null);
It modifies customer table i.e it defines name, age, salary as not null constraint.

Table altered.
3. Modify column cid as Primary Key of Customer Table

SQL> alter table customer modify(cid number primary key);


It declares cid as primary key in customer table.

Table altered.

4. Set salary column is set to 5000.00 by default

SQL> alter table customer modify(salary float default 5000.0);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 77
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

It sets the salary attribute to a default value of 5000.0.

Table altered.

5. Drop a DEFAULT constraint on Salary column

SQL> alter table customer modify(salary float null);


We cannot drop a default constraint so we set the salary attribute null constraint.

Table altered.

6. Set age Column is UNIQUE in Customer table

SQL> alter table customer modify(age number unique);


It modifies customer table such that it sets age attribute to unique constraint.

Table altered.
7. Create a table for the following relation
Orders(id:int NOT NULL Primary key , orderdate:Date, Amount:double,cid:
foreignkey)

SQL> create table orders(id number not null primary key,orderdate date,amount float,cid
number,foreign key(cid) references customer(cid));
It creates a orders table with id, orderdate, amount, cid as attributes of table with number,
date, float, number as data types of the attributes respectively. It also sets id to not null and
primary key constraints and cid as primary key constraints refrencing to customer tables cid
attribute.
Table created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 78
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

8. Drop foreign key constraint on Order table.

We cannot drop the foreign key constraint.

9. Add a CHECK with AGE column, so that you cannot have any CUSTOMER who is
below 18 years.

SQL> alter table customer modify(age number check(age>18));


It modifies customer table such that it sets age attribute to check constraint with condition
age>18.

Table altered.

10. Insert the following data into customer table.

Customer Table

----+----------+-----+-----------+----------+

| ID | NAME | AGE | ADDRESS | SALARY |

+----+----------+-----+-----------+----------+

| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |

| 2 | Khilan | 25 | Delhi | 1500.00 |

| 3 | kaushik | 23 | Kota | 2000.00 |

| 4 | Chaitali | 25 | Mumbai | 6500.00 |

| 5 | Hardik | 27 | Bhopal | 8500.00 |

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 79
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

| 6 | Komal | 22 | MP | 4500.00 |

| 7 | Muffy | 24 | Indore | 10000.00 |

SQL> insert into customer values(1,'ramesh',32,'ahmedabad',2000.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(2,'khilan',25,'delhi',1500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(3,'kaushik',23,'kota',2000.0);


It inserts the given data into the specified table.
1 row created.

SQL> insert into customer values(4,'chaitali',26,'mumbai',6500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(5,'hardik',27,'bhopal',8500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(6,'komal',22,'mp',4500.0);


It inserts the given data into the specified table.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 80
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

SQL> insert into customer values(7,'muffy',24,'indore',10000.0);


It inserts the given data into the specified table.

1 row created.

SQL> select * from customer;


It select and displays all the record of customer table.

CID NAME AGE ADDRESS SALARY


---------- -------------------- ---------- -------------------- ----------
1 ramesh 32 ahmedabad 2000
2 khilan 25 delhi 1500
3 kaushik 23 kota 2000
4 chaitali 26 mumbai 6500
5 hardik 27 bhopal 8500
6 komal 22 mp 4500
7 muffy 24 indore 10000

7 rows selected.

11. Insert the following data into orders table.


ORDERS Table

|OID | DATE | CUSTOMER_ID | AMOUNT |

+-----+---------------------+-------------+--------+

| 102 | 2009-10-08 | 3 | 3000 |

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 81
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

| 100 | 2009-10-08 | 3 | 1500 |

| 101 | 2009-11-20 | 2 | 1560 |

| 103 | 2008-05-20 | 4 | 2060 |

SQL> insert into orders values(102,'08/oct/09',3000.0,3);


It inserts the given data into the specified table.

1 row created.

SQL> insert into orders values(100,'08/oct/09',1500.0,3);


It inserts the given data into the specified table.

1 row created.

SQL> insert into orders values(101,'20/nov/09',1560.0,2);


It inserts the given data into the specified table.
1 row created.

SQL> insert into orders values(103,'20/may/08',2060.0,4);


It inserts the given data into the specified table.

1 row created.

SQL> select * from orders;


It select and describes all the records of orders table.

ID ORDERDATE AMOUNT CID


---------- --------- ---------- ---------- --------------
102 08-OCT-09 3000 3

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 82
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

100 08-OCT-09 1500 3


101 20-NOV-09 1560 2
103 20-MAY-08 2060 4

12. Display all customer id, name, age and amount from above tables.

SQL> select cid,name,age from customer;


It select and displays cid, name, age of all the records of customer table.

CID NAME AGE


---------- -------------------- ----------
1 ramesh 32
2 khilan 25
3 kaushik 23
4 chaitali 26
5 hardik 27
6 komal 22
7 muffy 24
7 rows selected.

SQL> select amount from orders;


It select and dispalys the amount of all the records of orders table.
AMOUNT
----------
3000
1500
1560
2060
13. Display all customer details who ordered on ‘08-10-2009’.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 83
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> select * from customer where cid in(select cid from orders where
orderdate='08/oct/09');
It select and display the record of customer who ordered an item on 08/OCT/09

CID NAME AGE ADDRESS SALARY


---------- -------------------- ---------- -------------------- ----------
3 kaushik 23 kota 2000

BANK DATABASE
CREATE TABLE BRANCH(branch_name,branch_city,assets)

SQL> create table branch(branchname varchar(3) primary key,branchcity varchar(20),assets float);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 84
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

It creates a table with” branch” as table name and branchName as primary key,branchcity,assets as
columns.

Table created.

CREATE TABLE ACCOUNT(account_number,branch_name,balance)

SQL> create table account(account_number varchar(20) primary key,branchname varchar(20),amount


varchar(20));

It creates a table with” Account” as table name and account_number as primary key,branchname,amounts
as columns.

Table created.

CREATE TABLE LOAN(loan_number,branch_name,amount)

SQL> create table loan(loan_number number(20),branchname varchar(20) ,amount real,primary


key(loan_number,branchname));

It creates a table with” Loan” as table name with LoanNumber&branch_name as primary key,amount as
columns to it within table Level.

Table created.

CREATE TABLE DEPOSITOR(customer_name,account_number)

SQL> create table depositor(customer_name varchar(20),account_number varchar(20),primary


key(customer_name,account_number));

It creates a table with”Depositer” as table name and Customer Name&account_number as primary key of
columns to table.

Table created.

CREATE TABLE BORROWER(customer_name,loan_number)

SQL> create table borrower(customer_name varchar(20),loan_number varchar(20),primary


key(customer_name,loan_number));

It creates a table with” Borrower” as table name and Customer Name&loan_number as primary key
columns to table.

Table created.

CREATE TABLE EMPLOYEE(employee_name,branch_name,salary)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 85
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> create table employee(employee_name varchar(20),branch_name varchar(20),salary real,primary


key(employee_name,branch_name));

It creates a table with” employee” as table name and employee_Name&branvh_name as primary


key,salary of columns to table.

Table created.

ADDING FOREIGN KEY CONSTRAINTS

SQL> alter table account add foreign key(branchname) references branch(branchname);

It adds a foreign key constraint to account table of column branchname which refers to branchname in
branch Table.

Table altered.

SQL> alter table loan add foreign key(branchname) references branch(branchname);

It adds a foreign key constraint to LOAN table of column branch which refers to branchname in branch
Table.

Table altered.

SQL> alter table depositor add foreign key(customer_name) references customer(customer_name);

It adds a foreign key constraint to depositer table of column customer_name which refers to
customer_name in customer Table.

Table altered.

SQL> alter table depositor add foreign key(account_number) references account(account_number);

It adds a foreign key constraint to depositer table of column account_number which refers to
account_number in account Table.

Table altered.

SQL> alter table borrower add foreign key(customer_name) references customer(customer_name);

It adds a foreign key constraint to borrower table of column customer_name which refers to
customer_name in customer Table.

Table altered.

SQL> alter table employee add foreign key(branchname) references branch(branchname);

It adds a foreign key constraint to employee table of column branchname which refers to branch_name in
branch Table.

Table altered.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 86
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> alter table loan add foreign key(branchname) references branch(branchname);

It adds a foreign key constraint to Loan table of column branchname which refers to branchname in
branch Table.

Table altered.

INSERT RECORDS INTO BRANCH TABLE

SQL> insert into branch values('Brighton', 'Brooklyn', 7100000);

1 row created.

SQL> insert into branch values('Downtown', 'Brooklyn', 9000000);

1 row created.

SQL> insert into branch values('Mianus', 'Horseneck', 400000);

1 row created.

SQL> insert into branch values('North Town', 'Rye', 3700000);

1 row created.

SQL> insert into branch values('Perryridge', 'Horseneck', 1700000);

1 row created.

SQL> insert into branch values('Pownal', 'Bennington', 300000);

1 row created.

SQL> insert into branch values('Redwood', 'Palo Alto', 2100000);

1 row created.

SQL> insert into branch values('Round Hill', 'Horseneck', 8000000);

1 row created.

INSERT RECORDS INTO CUSTOMER TABLE

SQL> insert into customers values('Adams', 'Spring', 'Pittsfield');

1 row created.

SQL> insert into customers values('Brooks', 'Senator', 'Brooklyn');

1 row created.

SQL> insert into customers values('Curry', 'North', 'Rye');

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 87
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

SQL> insert into customers values('Glenn', 'Sand Hill', 'Woodside');

1 row created

SQL> insert into customers values('Green', 'Walnut', 'Stamford');

1 row created.

SQL> insert into customers values('Hayes', 'Main', 'Harrison');

1 row created.

SQL> insert into customers values('Johnson', 'Alma', 'Palo Alto');

1 row created.

SQL> insert into customers values('Jones', 'Main', 'Harrison');

1 row created.

SQL> insert into customers values('Lindsay', 'Park', 'Pittsfield');

1 row created.

SQL> insert into customers values('Smith', 'North', 'Rye');

1 row created.

SQL> insert into customers values('Turner', 'Putnam', 'Stamford');

1 row created.

SQL> insert into customers values('Williams', 'Nassau', 'Princeton');

1 row created.

INSERT RECORDS INTO ACCOUNT TABLE

SQL> insert into account values('A-101', 'Downtown', 500);

1 row created.

SQL> insert into account values('A-102', 'Perryridge', 400);

1 row created.

SQL> insert into account values('A-201', 'Brighton', 900);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 88
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into account values('A-215', 'Mianus', 700);

1 row created.

SQL> insert into account values('A-217', 'Brighton', 750);

1 row created.

SQL> insert into account values('A-222', 'Redwood', 700);

1 row created.

SQL> insert into account values('A-305', 'Round Hill', 350);

1 row created.

INSERT RECORDS INTO DEPOSITOR TABLE

SQL> insert into depositor values('&customer_nae','&account_number');

SQL> insert into depositor values('Hayes', 'A-102');

1 row created.

SQL> insert into depositor values('Johnson', 'A-102');

1 row created.

SQL> insert into depositor values('Johnson', 'A-201');

1 row created.

SQL> insert into depositor values('Jones', 'A-217');

1 row created.

SQL> insert into depositor values('Lindsay', 'A-222');

1 row created.

SQL> insert into depositor values('Smith', 'A-215');

1 row created.

SQL> insert into depositor values('Turner', 'A-305');

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 89
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

1 row created.

INSERT RECORDS INTO LOANTABLE

SQL> insert into loan values('L-14', 'Downtown', 1500);

1 row created.

SQL> insert into loan values('L-15', 'Perryridge', 1500);

1 row created.

SQL> insert into loan values('L-16', 'Perryridge', 1300);

1 row created.

SQL> insert inTo loan values('L-17', 'Downtown', 1000);

1 row created.

SQL> insert into loan values('L-23', 'Redwood', 2000);

1 row created.

SQL> insert into loan values('l-93', 'Mianus', 500);

1 row created.

INSERT VALUES INTO EMPLOYEE TABLE

SQL> insert into employee values('Perryridge','Adams', 1500);

1 row created.

SQL> insert into employee values('Perryridge','Brown', 1300);

1 row created.

SQL> insert into employee values('Perryridge','Gopal', 5300);

1 row created.

SQL> insert into employee values('Downtown','Jhonson', 1500);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 90
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into employee values('Downtown','Loreena', 1300);

1 row created.

SQL> insert into employee values('Downtown','Peterson', 2500);

1 row created.

1.FIND ALL ACCOUNTS WHOSE BALANCE IS SMALLER THAN 500

SQL> select *from account where amount<500;

It displays the data in rows and columns from the given table Account.

ACCOUNT_NUMBER BRANCHNAME AMOUNT

a-102 perryridge 400

a-305 round hill 350

2.FIND ALL NAMES OF CUSTOMERS WHOSE CITY IS BROOKLYN

SQL> select *from customer where customer_city='brooklyn';

CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY

brooks senator brooklyn

3.FIND ALL EMPLOYEES WHOSE SALARY IS GREATER THAN 1400 AND WORKING
BRANCH IS DOWNTOWN

SQL> select *from employee where salary>1400 and branchname!='downtown';

It selects an employee whose salary is greater than 1400 whose branchname is not downtown.

EMPLOYEE_NAME BRANCHNAME SALARY

adams perryridge 1500

gopal perryridge 5300

4.CALCULATE THE AVERAGE SALARY OF ALL EMPLOYEES AND SHOW THE


AVERAGE SALARY AS “avg_salary”

SQL> select avg(salary) as avg_salary from employee;

It displays the average salary of employees

AVG_SALARY

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 91
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

2233.33333

6.SHOW ALL account_number,branch_name and CORRESPONDING branch_city

SQL> select a.account_number,b.branchname,b.branchcity from account a, branch b where


a.branchname=b.branchname;

It displays all the accoun_number,branch_name and branchcity whose branch name is common to the
tables.

ACCOUNT_NUMBER BRANCHNAME BRANCHCITY

a-217 brighton brooklyn

201 brighton brooklyn

a-101 downtown brooklyn

215 mianus horseneck

a-102 perryridge horsetown

a-222 redwood palo alto

a-305 round hill horseneck

7 rows selected.

VIEWS

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 92
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Q1. Create tables for the above entity

SQL> create table student(stdname varchar(20),stdaddresss varchar(20),stdphno number,stdemail


varchar(20),stdfathername varchar(20),stdbranch varchar(20),stdrollno number);

It creates a table named as Student with column names as


stdname,stdadress,stdphno,stdemail,stdfathername,stdbranch,stdrollno with all datatypes as varchar.

Table created.

SQL> create table college(colid number,colname varchar(20),coladdress varchar(20),colphno number);

It creates a table named as college with colid,colname,coladress,colphone as columns.

Table created.

SQL> alter table student modify(stdrollno number primary key);

Alter command is used here to add primary key to column stdrollno to table student.

Table altered.

SQL> alter table college modify(colid number primary key);

Alter command is used here to add primary key to column colid to table college.

Table altered.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 93
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Q2.INSERT RECORDS INTO TABLE.

SQL> insert into student values('prani','hyd',2542,'pra','srinu','cse',52);

Insert command is used here to add tuples to table to be inserted into specified columns in table student.

1 row created.

SQL> insert into student values('b','klpr',25362,'b@gmail','dr','it',1);

1 row created.

SQL> insert into student values('c','ka',25362,'c@gmail','cr','civil',2);

1 row created.

SQL> insert into student values('d','plmr',25473,'d@gmail','devil','mech',3);

1 row created.

SQL> insert into student values('e','plm',33473,'e@gmail','desil','mech',4);

1 row created.

SQL> insert into student values('f','acpt',32573,'f@gmail','pr','it',5);

1 row created.

SQL> insert into student values('g','acpt',33573,'g@gmail','raju','eee',6);

1 row created.

SQL> insert into student values('h','acpt',33575,'h@gmail','craju','ece',7);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 94
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into student values('i','ycpt',33575,'i@gmail','braju','eee',8);

1 row created.

SQL> insert into student values('j','ycpt',453575,'j@gmail','sri','aero',9);

1 row created.

SQL> insert into student values('k','ycpt',4534675,'k@gmail','sriv','aero',10);

1 row created.

SQL> insert into student values('l','ap',45346746,'l@gmail','sriva','chemical',11);

1 row created.

SQL> insert into student values('m','ts',4534367,'m@gmail','srivas','chemical',12);

1 row created.

SQL> insert into student values('n','ts',4534367,'m@gmail','sriv','cing',13);

1 row created.

SQL> insert into student values('o','jk',454677,'n@gmail','sree','cing',14);

1 row created.

SQL> select * from student;

STD STD STD STD STD STD STD

NAME ADDDRESS PHNO EMAIL FATHER BRANCH ROLLNO

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 95
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

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

prani hyd 2542 pra srinu cse 52

b klpr 25362 b@gmail dr it 1

c ka 25362 c@gmail cr civil 2

d plmr 25473 d@gmail devil mech 3

e plm 33473 e@gmail desil mech 4

f acpt 32573 f@gmail pr it 5

g acpt 33573 g@gmail raju eee 6

h acpt 33575 h@gmail craju ece 7

i ycpt 33575 i@gmail braju eee 8

j ycpt 453575 j@gmail sri aero 9

k ycpt 4534675 k@gmail sriv aero 10

l ap 4 5346746 l@gmail sriva chemical 11

m ts 4534367 m@gmail srivas chemical 12

n ts 4534367 m@gmail sriv cing 13

o jk 454677 n@gmail sree cing 14

15 rows selected.

SQL> insert into college values(1,'jntu','hyd',245453);

1 row created.

SQL> insert into college values(2,'ou','rr',245454);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 96
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into college values(3,'cbit','gbl',245455);

1 row created.

SQL> insert into college values(4,'vasv','mbnr',245456);

1 row created.

SQL> insert into college values(5,'vnr','npet',245457);

1 row created.

SQL> insert into college values(6,'snist','ypet',2454568);

1 row created.

SQL> insert into college values(7,'mgit','gbl',24545765);

1 row created.

SQL> insert into College values(8,'gkrj','npet',24545786);

1 row created.

SQL> insert into college values(9,'cvsr','tnk',24545754);

1 row created.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 97
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert intO college values(10,'nmlrit','mdchl',23545754);

1 row created.

SQL> insert into college values(11,'vadman','sbad',23545456);

1 row created.

SQL> insert into college values(12,'vbit','gtksr',23545475);

1 row created.

SQL> insert into college values(13,'anu','gtksr',235454546);

1 row created.

SQL> insert into college values(14,'arora','gtksr',235454546);

1 row created.

SQL> insert into college values(15,'mega','gdnr',23549746);

1 row created.

Q3.create a view, named as Student1 with columns as 1,2,3,4 for student table.

SQL> create view std1 as select stdname,stdaddresss,stdphno,stdemail from student;

It creates a view named std1 with selected columns in the select statement of columns
stdname,stdaddresss,stdphno,stdemail

View created.

Q4.create a view, named as college1 with columns as 1, 2 for college table.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 98
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> create view colg1 as select colid,colname from college;

It creates a view named colg1 with selected columns in the select statement of columns colid,colname.

View created.

Q5. Create a view, named as student2 with column as 5, 6, 7 for student table.

SQL> create view std2 as select stdfathername,stdbranch,stdrollno from student;

It creates a view named std2 with selected columns in the select statement of columns

stdfathername,stdbranch,stdrollno.

View created.

Q6. Create a view, named as College2 with column as 3, 4 for student table.

SQL> create view colg2 as select coladdress,colphno from college;

It creates a view named colg2 with selected columns in the select statement of columns
coladdress,colphno.

View created.

SQL> select * from std1;

It displays a view columns and row data from view std1 .

STDNAME STDADDRESSS STDPHNO STDEMAIL

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

prani hyd 2542 pra

b klpr 25362 b@gmail

c ka 25362 c@gmail

d plmr 25473 d@gmail

e plm 33473 e@gmail

f acpt 32573 f@gmail

g acpt 33573 g@gmail

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 99
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

h acpt 33575 h@gmail

i ycpt 33575 i@gmail

j ycpt 453575 j@gmail

k ycpt 4534675 k@gmail

l ap 45346746 l@gmail

m ts 4534367 m@gmail

n ts 4534367 m@gmail

o jk 454677 n@gmail

15 rows selected.

SQL> select * from std2;

It displays a view columns and row data from view std1 .

STD

FATHERNAME STDBRANCH STDROLLNO

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

srinu cse 52

dr it 1

cr civil 2

devil mech 3

desil mech 4

pr it 5

raju eee 6

craju ece 7

braju eee 8

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 100


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

sri aero 9

sriv aero 10

sriva chemical 11

srivas chemical 12

sriv cing 13

sree cing 14

15 rows selected.

SQL> select * from colg1;

It displays a view columns and row data from view std1 .

COLID COLNAME

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

1 jntu

2 ou

3 cbit

4 vasv

5 vnr

6 snist

7 mgit

8 gkrj

9 cvsr

10 nmlrit

11 vadman

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 101


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

COLID COLNAME

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

12 vbit

13 anu

14 arora

15 mega

15 rows selected.

SQL> select * from colg2;

It displays a view columns and row data from view std1

COLADDRESS COLPHNO

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

hyd 245453

rr 245454

gbl 245455

mbnr 245456

npet 245457

ypet 2454568

gbl 24545765

npet 24545786

tnk 24545754

mdchl 23545754

sbad 23545456

gtksr 23545475

gtksr 235454546

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 102


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

gtksr 235454546

gdnr 23549746

15 rows selected.

Q7.Create a view, named as studentcollege with columns 1,3,4 from student and 1,4 from college.

SQL> create view studentcollege as select s.stdname,s.stdphno,s.stdemail,c.colid,c.colphno from student


s,college c;

View created.

SQL> select * from studentcollege;

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

prani 2542 pra 1 245453

prani 2542 pra 2 245454

prani 2542 pra 3 245455

prani 2542 pra 4 245456

prani 2542 pra 5 245457

prani 2542 pra 6 2454568

prani 2542 pra 7 24545765

prani 2542 pra 8 24545786

prani 2542 pra 9 24545754

prani 2542 pra 10 23545754

prani 2542 pra 11 23545456

prani 2542 pra 12 23545475

prani 2542 pra 13 235454546

prani 2542 pra 14 235454546

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 103


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

prani 2542 pra 15 23549746

b 25362 b@gmail 1 245453

b 25362 b@gmail 2 245454

b 25362 b@gmail 3 245455

b 25362 b@gmail 4 245456

b 25362 b@gmail 5 245457

b 25362 b@gmail 6 2454568

b 25362 b@gmail 7 24545765

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

b 25362 b@gmail 8 24545786

b 25362 b@gmail 9 24545754

b 25362 b@gmail 10 23545754

b 25362 b@gmail 11 23545456

b 25362 b@gmail 12 23545475

b 25362 b@gmail 13 235454546

b 25362 b@gmail 14 235454546

b 25362 b@gmail 15 23549746

c 25362 c@gmail 1 245453

c 25362 c@gmail 2 245454

c 25362 c@gmail 3 245455

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

c 25362 c@gmail 4 245456

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 104


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

c 25362 c@gmail 5 245457

c 25362 c@gmail 6 2454568

c 25362 c@gmail 7 24545765

c 25362 c@gmail 8 24545786

c 25362 c@gmail 9 24545754

c 25362 c@gmail 10 23545754

c 25362 c@gmail 11 23545456

c 25362 c@gmail 12 23545475

c 25362 c@gmail 13 235454546

c 25362 c@gmail 14 235454546

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

c 25362 c@gmail 15 23549746

d 25473 d@gmail 1 245453

d 25473 d@gmail 2 245454

d 25473 d@gmail 3 245455

d 25473 d@gmail 4 245456

d 25473 d@gmail 5 245457

d 25473 d@gmail 6 2454568

d 25473 d@gmail 7 24545765

d 25473 d@gmail 8 24545786

d 25473 d@gmail 9 24545754

d 25473 d@gmail 10 23545754

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 105


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

d 25473 d@gmail 11 23545456

d 25473 d@gmail 12 23545475

d 25473 d@gmail 13 235454546

d 25473 d@gmail 14 235454546

d 25473 d@gmail 15 23549746

e 33473 e@gmail 1 245453

e 33473 e@gmail 2 245454

e 33473 e@gmail 3 245455

e 33473 e@gmail 4 245456

e 33473 e@gmail 5 245457

e 33473 e@gmail 6 2454568

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

e 33473 e@gmail 7 24545765

e 33473 e@gmail 8 24545786

e 33473 e@gmail 9 24545754

e 33473 e@gmail 10 23545754

e 33473 e@gmail 11 23545456

e 33473 e@gmail 12 23545475

e 33473 e@gmail 13 235454546

e 33473 e@gmail 14 235454546

e 33473 e@gmail 15 23549746

f 32573 f@gmail 1 245453

f 32573 f@gmail 2 245454

f 32573 f@gmail 3 245455

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 106


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

f 32573 f@gmail 4 245456

f 32573 f@gmail 5 245457

f 32573 f@gmail 6 2454568

f 32573 f@gmail 7 24545765

f 32573 f@gmail 8 24545786

f 32573 f@gmail 9 24545754

f 32573 f@gmail 10 23545754

f 32573 f@gmail 11 23545456

f 32573 f@gmail 12 23545475

f 32573 f@gmail 13 235454546

f 32573 f@gmail 14 235454546

f 32573 f@gmail 15 23549746

g 33573 g@gmail 1 245453

g 33573 g@gmail 2 245454

g 33573 g@gmail 3 245455

g 33573 g@gmail 4 245456

g 33573 g@gmail 5 245457

g 33573 g@gmail 6 2454568

g 33573 g@gmail 7 24545765

g 33573 g@gmail 8 24545786

g 33573 g@gmail 9 24545754

g 33573 g@gmail 10 23545754

g 33573 g@gmail 11 23545456

g 33573 g@gmail 12 23545475

g 33573 g@gmail 13 235454546

g 33573 g@gmail 14 235454546

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 107


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

g 33573 g@gmail 15 23549746

h 33575 h@gmail 1 245453

h 33575 h@gmail 2 245454

h 33575 h@gmail 3 245455

h 33575 h@gmail 4 245456

h 33575 h@gmail 5 245457

h 33575 h@gmail 6 2454568

h 33575 h@gmail 7 24545765

h 33575 h@gmail 8 24545786

h 33575 h@gmail 9 24545754

h 33575 h@gmail 10 23545754

h 33575 h@gmail 11 23545456

h 33575 h@gmail 12 23545475

h 33575 h@gmail 13 235454546

h 33575 h@gmail 14 235454546

h 33575 h@gmail 15 23549746

i 33575 i@gmail 1 245453

i 33575 i@gmail 2 245454

i 33575 i@gmail 3 245455

i 33575 i@gmail 4 245456

i 33575 i@gmail 5 245457

i 33575 i@gmail 6 2454568

i 33575 i@gmail 7 24545765

i 33575 i@gmail 8 24545786

i 33575 i@gmail 9 24545754

i 33575 i@gmail 10 23545754

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 108


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

i 33575 i@gmail 11 23545456

i 33575 i@gmail 12 23545475

i 33575 i@gmail 13 235454546

i 33575 i@gmail 14 235454546

i 33575 i@gmail 15 23549746

j 453575 j@gmail 1 245453

j 453575 j@gmail 2 245454

j 453575 j@gmail 3 245455

j 453575 j@gmail 4 245456

j 453575 j@gmail 5 245457

j 453575 j@gmail 6 2454568

j 453575 j@gmail 7 24545765

j 453575 j@gmail 8 24545786

j 453575 j@gmail 9 24545754

j 453575 j@gmail 10 23545754

j 453575 j@gmail 11 23545456

j 453575 j@gmail 12 23545475

j 453575 j@gmail 13 235454546

j 453575 j@gmail 14 235454546

j 453575 j@gmail 15 23549746

k 4534675 k@gmail 1 245453

k 4534675 k@gmail 2 245454

k 4534675 k@gmail 3 245455

k 4534675 k@gmail 4 245456

k 4534675 k@gmail 5 245457

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 109


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

k 4534675 k@gmail 6 2454568

k 4534675 k@gmail 7 24545765

k 4534675 k@gmail 8 24545786

k 4534675 k@gmail 9 24545754

k 4534675 k@gmail 10 23545754

k 4534675 k@gmail 11 23545456

k 4534675 k@gmail 12 23545475

k 4534675 k@gmail 13 235454546

k 4534675 k@gmail 14 235454546

k 4534675 k@gmail 15 23549746

l 45346746 l@gmail 1 245453

l 45346746 l@gmail 2 245454

l 45346746 l@gmail 3 245455

l 45346746 l@gmail 4 245456

l 45346746 l@gmail 5 245457

l 45346746 l@gmail 6 2454568

l 45346746 l@gmail 7 24545765

l 45346746 l@gmail 8 24545786

l 45346746 l@gmail 9 24545754

l 45346746 l@gmail 10 23545754

l 45346746 l@gmail 11 23545456

l 45346746 l@gmail 12 23545475

l 45346746 l@gmail 13 235454546

l 45346746 l@gmail 14 235454546

l 45346746 l@gmail 15 23549746

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 110


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

m 4534367 m@gmail 1 245453

m 4534367 m@gmail 2 245454

m 4534367 m@gmail 3 245455

m 4534367 m@gmail 4 245456

m 4534367 m@gmail 5 245457

m 4534367 m@gmail 6 2454568

m 4534367 m@gmail 7 24545765

m 4534367 m@gmail 8 24545786

m 4534367 m@gmail 9 24545754

m 4534367 m@gmail 10 23545754

m 4534367 m@gmail 11 23545456

m 4534367 m@gmail 12 23545475

m 4534367 m@gmail 13 235454546

m 4534367 m@gmail 14 235454546

m 4534367 m@gmail 15 23549746

n 4534367 m@gmail 1 245453

n 4534367 m@gmail 2 245454

n 4534367 m@gmail 3 245455

n 4534367 m@gmail 4 245456

n 4534367 m@gmail 5 245457

n 4534367 m@gmail 6 2454568

n 4534367 m@gmail 7 24545765

n 4534367 m@gmail 8 24545786

n 4534367 m@gmail 9 24545754

n 4534367 m@gmail 10 23545754

n 4534367 m@gmail 11 23545456

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 111


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

n 4534367 m@gmail 12 23545475

n 4534367 m@gmail 13 235454546

n 4534367 m@gmail 14 235454546

STDNAME STDPHNO STDEMAIL COLID COLPHNO

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

n 4534367 m@gmail 15 23549746

o 454677 n@gmail 1 245453

o 454677 n@gmail 2 245454

o 454677 n@gmail 3 245455

o 454677 n@gmail 4 245456

o 454677 n@gmail 5 245457

o 454677 n@gmail 6 2454568

o 454677 n@gmail 7 24545765

o 454677 n@gmail 8 24545786

o 454677 n@gmail 9 24545754

o 454677 n@gmail 10 23545754

o 454677 n@gmail 11 23545456

o 454677 n@gmail 12 23545475

o 454677 n@gmail 13 235454546

o 454677 n@gmail 14 235454546

o 454677 n@gmail 15 23549746

225 rows selected.

Q8. Apply all DML, DQL, DDL, TCL commands on the above views.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 112


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> insert into std2 values('k','cse',53);

The insert is used to insert values into specified columns.

1 row created.

SQL> select * from std2;

To display all the rows and column values in specified table.

STD

FATHERNAME STDBRANCH STDROLLNO

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

srinu cse 52

dr it 1

cr civil 2

devil mech 3

desil mech 4

pr it 5

raju eee 6

craju ece 7

braju eee 8

sri aero 9

sriv aero 10

sriva chemical 11

srivas chemical 12

sriv cing 13

sree cing 14

k cse 53

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 113


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

16 rows selected.

SQL> desc std2;

It is used to display the structure of the tables and views.

Name Null? Type

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

STDFATHERNAME VARCHAR2(20)

STDBRANCH VARCHAR2(20)

STDROLLNO NOT NULL NUMBER

SQL> delete from std2 where stdfathername='k';

The DELETE statement is used to delete records in a table. The WHERE clause specifies which record or
records that should be deleted.

1 row deleted.

SQL> select * from std2;

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the
form of a result table.

STD

FATHERNAME STDBRANCH STDROLLNO

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

srinu cse 52

dr it 1

cr civil 2

devil mech 3

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 114


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

desil mech 4

pr it 5

raju eee 6

craju ece 7

braju eee 8

sri aero 9

sriv aero 10

sriva chemical 11

srivas chemical 12

sriv cing 13

sree cing 14

15 rows selected.

SQL> desc std2;

It is used to display the structure of the tables and views.

Name Null? Type

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

STDFATHERNAME VARCHAR2(20)

STDBRANCH VARCHAR2(20)

STDROLLNO NOT NULL NUMBER

SQL> select * from std1;

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the
form of a result table.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 115


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

STDNAME STDADDRESSS STDPHNO STDEMAIL

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

prani hyd 2542 pra

b klpr 25362 b@gmail

c ka 25362 c@gmail

d plmr 25473 d@gmail

e plm 33473 e@gmail

f acpt 32573 f@gmail

g acpt 33573 g@gmail

h acpt 33575 h@gmail

i ycpt 33575 i@gmail

j ycpt 453575 j@gmail

k ycpt 4534675 k@gmail

l ap 45346746 l@gmail

m ts 4534367 m@gmail

n ts 4534367 m@gmail

o jk 454677 n@gmail

15 rows selected.

SQL> update std1 set stdname='s' where stdemail='pra';

The SQL UPDATE Query is used to modify the existing records in a table specified in where clause.

1 row updated.

SQL> select * from std1;

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the
form of a result table.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 116


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

STDNAME STDADDRESSS STDPHNO STDEMAIL

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

s hyd 2542 pra

b klpr 25362 b@gmail

c ka 25362 c@gmail

d plmr 25473 d@gmail

e plm 33473 e@gmail

f acpt 32573 f@gmail

g acpt 33573 g@gmail

h acpt 33575 h@gmail

i ycpt 33575 i@gmail

j ycpt 453575 j@gmail

k ycpt 4534675 k@gmail

l ap 45346746 l@gmail

m ts 4534367 m@gmail

n ts 4534367 m@gmail

o jk 454677 n@gmail

15 rows selected.

SQL> desc std1;

It is used to display the structure of the tables and views.

Name Null? Type

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

STDNAME VARCHAR2(20)

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 117


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

STDADDRESSS VARCHAR2(20)

STDPHNO NUMBER

STDEMAIL VARCHAR2(20)

SQL> select * from std1;

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the
form of a result table.

STDNAME STDADDRESSS STDPHNO STDEMAIL

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

prani hyd 2542 pra

b klpr 25362 b@gmail

c ka 25362 c@gmail

d plmr 25473 d@gmail

e plm 33473 e@gmail

f acpt 32573 f@gmail

g acpt 33573 g@gmail

h acpt 33575 h@gmail

i ycpt 33575 i@gmail

j ycpt 453575 j@gmail

k ycpt 4534675 k@gmail

l ap 45346746 l@gmail

m ts 4534367 m@gmail

n ts 4534367 m@gmail

o jk 454677 n@gmail

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 118


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

Assignment-6
Consider the following Emp table.
sid name age Salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 9000
404 Scott 44 10000
405 Tiger 35 8000

1. Create a table for the above data.


SQL> create table employees(eid number,name varchar(20),age number,salary number);
It creates an employees table with eid, name, age, salary as attributes of the table.

Table created.

2. Insert the above data in the employees table.


SQL> insert into employees values(401,'anu',22,5000);
It inserts the given value in employees table.

1 row created.

SQL> insert into employees values(402,'shane',29,8000);


It inserts the given value in employees table.

1 row created.

SQL> insert into employees values(403,'rohan',34,9000);


It inserts the given value in employees table.

1 row created.

SQL> insert into employees values(404,'scott',44,10000);


It inserts the given value in employees table.

1 row created.

SQL> insert into employees values(405,'tiger',35,8000);


It inserts the
1 row created.

1. Select distinct salary from Employee table

SQL> select distinct salary from emp;


To display the columns from table which are having different values .

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 119


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SALARY
----------
5000
9000
10000
8000
2. Display all records where salary is less than 10000 and age greater than 25.

SQL> select *from emp where salary<10000 and age>25;


To display based on the condition given in where clause that is 25<salary<10000

EID NAME AGE SALARY


---------- -------------------- ---------- ----------
402 shane 29 8000
403 rohan 34 9000
405 tiger 35 8000
3. Display records where either salary is greater than 8000 or age greater than 25.
SQL> select *from emp where salary>8000 or age>25;

EID NAME AGE SALARY


---------- -------------------- ---------- ----------
402 shane 29 8000
403 rohan 34 9000
404 scott 44 10000
405 tiger 35 8000

4. Find average of salary of Employee


SQL> select avg(salary) as avg_salary from emp;
To display average of columns for specified column.

AVG_SALARY
----------
8000

5. Find the names of employees whose salary greater than average salary
SQL> select name from emp where salary>(select avg(salary) from emp);

To select the names of employess whose salary greater than average salary

NAME
--------------------
rohan
scott
6. Find highest salary of Employee.
SQL> select max(salary) from emp;

To find the maximum of salary column is max function.

MAX(SALARY)
-----------

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 120


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

10000
7. Find Second highest salary of employee

SQL> select max(salary) as second_max from emp where not salary = (select max(salary) from emp);
It finds the maximum of given values by max() from values which there is no maximum element in it.

SECOND_MAX
----------
9000
(or)
SQL> select max(salary) from emp where salary<(select max(salary) from emp);

MAX(SALARY)
-----------
9000
8. Display employee records in descending order based on salary of employee.
SQL> select *from emp order by salary desc;
The order by command is used here when we want data of columns in sorted order based on any column.

EID NAME AGE SALARY


---------- -------------------- ---------- ----------
404 scott 44 10000
403 rohan 34 9000
405 tiger 35 8000
402 shane 29 8000
401 anu 22 5000

9. Display number of unique record from Employee.


SQL> select count(distinct eid) from employee;

It returns the no.of distinct values in specified column.


COUNT(EID)
----------
5
10.Display all employees whose salary is greater than 6000 and less than highest salary of
employee

SQL> select * from emp where salary>6000 and salary<(select max(salary) from emp);
It selects the range of columns between the given interval of the specified range.

EID NAME AGE SALARY


---------- -------------------- ---------- ----------
402 shane 29 8000
403 rohan 34 9000
405 tiger 35 8000

11. Count the number of employees whose salary more than average salary.
SQL> select count(eid) from emp where salary>(select avg(salary) from emp);
It returns the number of rows based on given condition to count on a column.

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 121


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

COUNT(EID)
----------
2

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 122


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

ASSIGNMENT 7

Customer Table:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Orders Table

+-----+---------------------+-------------+--------+

|OID | DATE | CUSTOMER_ID | AMOUNT |

+-----+---------------------+-------------+--------+

| 102 | 2009-10-08 00:00:00 | 3 | 3000 |

| 100 | 2009-10-08 00:00:00 | 3 | 1500 |

| 101 | 2009-11-20 00:00:00 | 2 | 1560 |

| 103 | 2008-05-20 00:00:00 | 4 | 2060 |

+-----+---------------------+-------------+--------+

CREATE CUSTOMERS TABLE


SQL> create table cus(id number(10) primary key,name varchar(20),age number(10),address
varchar(20),salary number(10));

Table created.
)
SQL> insert into customer values(1,'ramesh',32,'ahmedabad',2000.0);
It inserts the given data into the specified table.

1 row created.
INSERT INTO CUSTOMER TABLE.
SQL> insert into customer values(2,'khilan',25,'delhi',1500.0);

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 123


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(3,'kaushik',23,'kota',2000.0);


It inserts the given data into the specified table.
1 row created.

SQL> insert into customer values(4,'chaitali',26,'mumbai',6500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(5,'hardik',27,'bhopal',8500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(6,'komal',22,'mp',4500.0);


It inserts the given data into the specified table.

1 row created.

SQL> insert into customer values(7,'muffy',24,'indore',10000.0);


It inserts the given data into the specified table.

1 row created.

1.DISPLAY ID,NAME,ADDRESS,DATE AND AMOUNT FROM CUSTOMER,ORDERS RELATION

SQL> select c.id,c.name,c.address,o.odate,o.amount from customerc,orde o;


It displays the specified columns listed from given specified tables
As a cartesian product relations.

ID NAME ADDRESS ODATE AMOUNT


---------- -------------------- -------------------- --------- ----------
1 ramesh ahmedabad 08-OCT-09 3000
2 khilan delhi 08-OCT-09 3000
3 kaushik kota 08-OCT-09 3000
4 chaitali mumbai 08-OCT-09 3000
5 hardik bhopal 08-OCT-09 3000
6 komal mp 08-OCT-09 3000
7 muffy indore 08-OCT-09 3000
1 ramesh ahmedabad 08-OCT-09 1500

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 124


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

2 khilan delhi 08-OCT-09 1500


3 kaushik kota 08-OCT-09 1500
4 chaitali mumbai 08-OCT-09 1500

ID NAME ADDRESS ODATE AMOUNT


---------- -------------------- -------------------- --------- ----------
5 hardik bhopal 08-OCT-09 1500
6 komal mp 08-OCT-09 1500
7 muffy indore 08-OCT-09 1500
1 ramesh ahmedabad 20-NOV-09 1560
2 khilan delhi 20-NOV-09 1560
3 kaushik kota 20-NOV-09 1560
4 chaitali mumbai 20-NOV-09 1560
5 hardik bhopal 20-NOV-09 1560
6 komal mp 20-NOV-09 1560
7 muffy indore 20-NOV-09 1560
1 ramesh ahmedabad 20-MAY-09 2060
2 khilan delhi 20-MAY-09 2060
3 kaushik kota 20-MAY-09 2060
4 chaitali mumbai 20-MAY-09 2060
5 hardik bhopal 20-MAY-09 2060
6 komal mp 20-MAY-09 2060
7 muffy indore 20-MAY-09 2060

28 rows selected.
2.DISPLAY ID,NAME, AMOUNT AND DATE FROM THE ABOVE RELATIONS USING INNER
JOIN

SQL> select c.id,c.name,o.amount,o.odate from customerc inner join orde o on c.id=o.customer_id;


The INNER JOIN keyword selects records that have matching values in both tables.

ID NAME AMOUNT ODATE


---------- -------------------- ---------- ---------
2 khilan 1560 20-NOV-09
3 kaushik 1500 08-OCT-09
3 kaushik 3000 08-OCT-09
4 chaitali 2060 20-MAY-09
3.DISPLAY ID,NAME,AMOUNT AND DATE FROM THE ABOVE REALATIONS USING CROSS
JOIN

SQL> select c.id,c.name,o.amount,o.odate from customerc cross join orde o where c.id=o.customer_id;
The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of the sets of records from two joined
tables

ID NAME AMOUNT ODATE


---------- -------------------- ---------- ---------
2 khilan 1560 20-NOV-09
3 kaushik 1500 08-OCT-09
3 kaushik 3000 08-OCT-09
4 chaitali 2060 20-MAY-09
4.DISPLAY CUSTOMER DETAILS WHERE ORDERED AMOUNT IS GREATER THAN 1500

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 125


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

SQL> select c.id,c.name,c.age,c.address,c.salary from customerc,orde o where c.id=o.customer_id and


o.amount>1500;
It displays the specified columns listed from given specified tables
As a cartesian product relations.

ID NAME AGE ADDRESS SALARY


---------- -------------------- ---------- -------------------- ----------
2 khilan 25 delhi 1500
3 kaushik 23 kota 2000
4 chaitali 25 mumbai 6500

6.PERFORM LEFT OUTER JOIN ON ABOVE RELATIONS

SQL> select *from customer left outer join orde on customer.id=orde.customer_id;


The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the
right table (table2).

ID NAME AGE ADDRESS SALARY OID ODATE CUSTOMER_ID AMOUNT


---------- --------- ----------- ---------- --------- ---- -------- ------------ --------
3 kaushik 23 kota 2000 102 08-OCT-09 3 3000
3 kaushik 23 kota 2000 100 08-OCT-09 3 1500
2 khilan 25 delhi 1500 101 20-NOV-09 2 1560
4 chaitali 25 mumbai 6500 103 20-MAY-09 4 2060
5 hardik 27 bhopal 8500
1 ramesh 32 ahmedabad 2000
6 komal 22 mp 4500
7 muffy 24 indore 10000
8 rows selected.

7.PERFORM RIGHT OUTER JOIN ON ABOVE RELATIONS

SQL> select *from customerright outer join orde on cus.id=orde.customer_id;


The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the
left table (table1).

ID NAME AGE ADDRESS SALARY OID ODATE CUSTOMER_ID AMOUNT


--- ------ -------------- ------------- ------------- ---- -------------
2 khilan 25 delhi 1500 101 20-NOV-09 2 1560

3 kaushik 23 kota 2000 100 08-OCT-09 3 1500


3 kaushik 23 kota 2000 102 08-OCT-09 3 3000

4 chaitali 25 mumbai 6500 103 20-MAY-09 4 2060

8.PERFORM FULL OUTER JOIN ON ABOVE RELATIONS

SQL> select *from customer full outer join orde on cus.id=order.customer_id;


The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right
(table2) table records.

ID NAME AGE ADDRESS SALARY OID ODATE CUSTOMER_ID AMOUNT

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 126


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

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


3 kaushik 23 kota 2000 102 08-OCT-09 3 3000

3 kaush 23 kota 2000 100 08-OCT-09 3 1500

2 khilan 25 delhi 1500 101 20-NOV-09 2 1560


4 chaitali 25 mumbai 6500 103 20-MAY-09 4 2060
5 hardik 27 bhopal 8500
1 ramesh 32 ahmedabad 2000
6 komal 22 mp 4500
7 muffy 24 indore 10000

8 rows selected.

9.DISPLAY ALL CUSTOMERS WHO DO NOT ORDER ANY ITEM

SQL> select *from customer left outer join order on cus.id=orde.customer_id minus(select *from customer
inner join order on cus.id=order.customer_id);
It selects customer record from left table which are not in right table.

ID NAME AGE ADDRESS SALARY


---------- -------------------- ---------- -------------------- ----------
OID ODATE CUSTOMER_ID AMOUNT
---------- --------- ----------- ----------
1 ramesh 32 ahmedabad 2000
5 hardik 27 bhopal 8500
6 komal 22 mp 4500
7 muffy 24 indore 10000

10. Display ID,Name,Address,Date , Amount ,Discount(10%) Amount from Customer ,Order s


relations

SQL>SELECTdistinct
c.id,c.name,c.address,o.odate,o.amount,((amount*10)/100) as discount_amount from customer c,order o where
c.id=o.customer_id;
The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form
of a result table.

ID NAME ADDRESS ODATE AMOUNT


---------- ------------------ -------------- DISCOUNT_AMOUNT
---------------
3 kaushik kota 08-OCT-09 1500 150
2 khilan delhi 20-NOV-09 1560 156

3 kaushik kota 08-OCT-09 3000 300

4 chaitali mumbai 20-MAY-09 060 667

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 127


B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB

ROLLNUMBER:17311A05L1 NAME:D.vikasPage 128

You might also like