Record For New
Record For New
Record For New
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
Command Description
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 1
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Command Description
Command Description
Command Description
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,
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 3
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
To Rename a column
Using alter command you can rename an existing column. Following is the Syntax,
To Drop a Column
alter command is also used to drop columns also. Following is the Syntax,
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 4
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
drop command
drop query completely removes a table from database. This command will also destroy the table structure.
Following is its Syntax,
rename query
rename command is used to rename a table. Following is its Syntax,
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,
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
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.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 7
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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.
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.
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.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 9
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
101 Adam 15
102 Alex 18
103 Abhi 17
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
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.
FROM table_name
WHERE condition
GROUP BY column_name
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
Rohan 34
shane 29
anu 22
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 12
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
GROUP BY column_name
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 13
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Suppose we want to find the customer whose previous_balance sum is more than 3000.
We will use the below SQL query,
SELECT *
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.
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
The above query will return only the unique salary from Emp table
salary
5000
8000
10000
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 15
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
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
The above query will return records where either salary is greater than 10000 or age greater than 25.
402 Shane 29 8000
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 16
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 18
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Example of COUNT(distinct)
Consider following Emp table
Eid name age 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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 19
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
SQL query
SELECT FIRST(salary) from Emp;
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
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
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 21
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
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
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
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 24
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 25
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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.
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
2. Outer Join
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 26
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
from table-name1
CROSS JOIN
table-name2;
from table-name1
INNER JOIN
table-name2
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,
from table-name1
table-name2
on table-name1.column-name = table-name2.column-name;
from table-name1,
table-name2
on table-name1.column-name = table-name2.column-name(+);
from table-name1
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 28
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
from table-name1
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
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
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
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.
UNION ALL
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 31
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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
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
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.
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;
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
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
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)
Table created.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
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
QUERY EXPLANATION:TO insert data into student table with specified values in columns.
1 row created.
1 row created.
1 row created.
1 row created
1 row created.
1 row created
QUERY EXPLANATION:SQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 36
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
6 rows selected.
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
ID NUMBER
FIRSTNAME VARCHAR2(15)
LASTNAME VARCHAR2(15)
EMAIL VARCHAR2(20)
YEAROFBIRTH NUMBER
PHONE NUMBER(10)
Table altered.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 37
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
FIRSTNAME VARCHAR2(15)
LASTNAME VARCHAR2(15)
EMAIL VARCHAR2(20)
YEAROFBIRTH NUMBER
PHONE NUMBER(10)
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
FIRSTNAME VARCHAR2(15)
LASTNAME VARCHAR2(15)
EMAIL VARCHAR2(20)
YEAROFBIRTH NUMBER
PHONE NUMBER(10)
ID VARCHAR2(2)
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 38
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
FIRSTNAME VARCHAR2(15)
LASTNAME VARCHAR2(15)
EMAIL VARCHAR2(20)
PHONE NUMBER(10)
ID VARCHAR2(2)
Table altered.
----------------------------------------------------------------------------------------------------------------- --
------ ----------------------------------------------------------------------------
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)
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
----------------------------------------------------------------------------------------------------------------- --
------ ----------------------------------------------------------------------------
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
QUERY EXPLANATION:TO insert data into student table with specified values in columns.
1 row created
1 row created.
1 row created.
1 row created.
SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.
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
6966544 4 CSE
686544 5 CSE
13 rows selected.
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.
QUERY EXPLANATION:SQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.
5566544 1 CSE
55966544 6 CSE
5566544 2 CSE
5966544 3 CSE
686544 5 CSE
13 rows selected.
Table altered.
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.
5566544 1 CSE
55966544 6 CSE
5566544 2 CSE
5966544 3 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)
Table created.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
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
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions for
specified TABLE.
SID NUMBER
SNAME VARCHAR2(7)
RATING NUMBER
AGE NUMBER(38).
3. Modify field age to real in the Sailors table and describe the table
QUERY EXPLANATION:ALTER keyword is used here to modify AGE column with float
datatype to existing table.
Table altered.
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
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
SID NUMBER
SNAME VARCHAR2(7)
RATING NUMBER
5. Add field (age: real) to the Sailors table and describe the table
QUERY EXPLANATION:ALTER keyword is used here to add AGE column with FLOAT
datatype to existing student table.
Table altered.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions
for specified TABLE.
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)
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)
Table created.
QUERY EXPLANATION:SQL DESC statement use for describe the List of column definitions for
specified TABLE.
SID NUMBER
SNAME VARCHAR2(7)
RATING NUMBER
AGE FLOAT(63)
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 47
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 48
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.
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
QUERY EXPLANATION:Distinct keyword is used with Select keyword to diplay unique records.
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 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)
QUERY EXPLANATION:The keyword is also used with a different meaning to create aliases
for tables and fields in a query.
DESC statement use for describe the List of column definitions for specified TABLE.
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)
Table created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 51
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
15. Create Reserves table with the following fields and describe the table (sid: integer, bid:
integer, day: date)
Table created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 52
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
:22,bid:101,day:10/10/98.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
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.
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.
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
1 row deleted.
SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.
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
SQL SELECT Statement is used to viewing data from the table. This statement return list of
table format table data.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 55
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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.
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
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
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 59
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
AVG(AGE)
----------
35.95
The MIN() function returns the smallest value of the selected column.
MIN(AGE)
----------
16
The MAX() function returns the smallest value of the selected column.
MAX(AGE)
---------
63.5
The MAX() function returns the smallest value of the selected column.
MAX(RATING)
-----------
10
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
COUNT(DISTINCTSNAME)
--------------------
15. Find all sailors whose age is in between 45.0 and 63.5 (using BETWEEN operator).
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)
95 bob 3 63.5
17. Find all sailors whose second letter in the names contain ‘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
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’
The SQL LIKE clause is used to compare a value to similar values in query specified.
32 andy 8 25.5
19. Find all sailors from sailors and sailors1 table by using UNION operator
When multiple SELECT queries are joined using UNION operator, It displays the combined
result from all the compounded SELECT queries.
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
It creates a table “course” with course_name as primary key and with columns course_id,course_name.
Table created.
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.
It creates a table subjects with primary key as subject_id,foreign key as lecturer id.
Table created.
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.
It creates a table “Hobby” with Student_id as primary key and with columns student_id,hobby.
Table created.
To insert values into specified course table with columns specified to be inserted.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 65
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created..
To insert values into specified course table with columns specified to be inserted.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 66
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
COURSE_ID COURSE_NAME
56 it
59 eee
23 ece
54 cse
32 mech
1 john 56
2 james 59
3 ron 23
4 kat 54
5 sam 32
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 67
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
SUBJECT_NAME VARCHAR2(8)
LECTURER_ID NUMBER
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 68
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 69
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
STUDENT_NAME VARCHAR2(6)
DOB DATE
DOOR_NO NUMBER
STREET VARCHAR2(3)
CITY VARCHAR2(3)
PIN VARCHAR2(4)
COURSE_ID NUMBER
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.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 70
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
---------- ------ --------- ---------- --- --- ---- ---- ---- ----- -------- ----- -----------------
7 rows selected.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 71
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
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.
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
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
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
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
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
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
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
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
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
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.
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
Table created.
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
Table altered.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 77
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Table altered.
Table altered.
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
9. Add a CHECK with AGE column, so that you cannot have any CUSTOMER who is
below 18 years.
Table altered.
Customer Table
----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 79
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
| 6 | Komal | 22 | MP | 4500.00 |
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
7 rows selected.
+-----+---------------------+-------------+--------+
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 81
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 82
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
12. Display all customer id, name, age and amount from above tables.
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
BANK DATABASE
CREATE TABLE BRANCH(branch_name,branch_city,assets)
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.
It creates a table with” Account” as table name and account_number as primary key,branchname,amounts
as columns.
Table created.
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.
It creates a table with”Depositer” as table name and Customer Name&account_number as primary key of
columns to table.
Table created.
It creates a table with” Borrower” as table name and Customer Name&loan_number as primary key
columns to table.
Table created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 85
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Table created.
It adds a foreign key constraint to account table of column branchname which refers to branchname in
branch Table.
Table altered.
It adds a foreign key constraint to LOAN table of column branch which refers to branchname in branch
Table.
Table altered.
It adds a foreign key constraint to depositer table of column customer_name which refers to
customer_name in customer Table.
Table altered.
It adds a foreign key constraint to depositer table of column account_number which refers to
account_number in account Table.
Table altered.
It adds a foreign key constraint to borrower table of column customer_name which refers to
customer_name in customer Table.
Table altered.
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
It adds a foreign key constraint to Loan table of column branchname which refers to branchname in
branch Table.
Table altered.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 87
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 88
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 89
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 90
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
It displays the data in rows and columns from the given table Account.
3.FIND ALL EMPLOYEES WHOSE SALARY IS GREATER THAN 1400 AND WORKING
BRANCH IS DOWNTOWN
It selects an employee whose salary is greater than 1400 whose branchname is not downtown.
AVG_SALARY
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 91
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
2233.33333
It displays all the accoun_number,branch_name and branchcity whose branch name is common to the
tables.
7 rows selected.
VIEWS
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 92
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
Table created.
Table created.
Alter command is used here to add primary key to column stdrollno to table student.
Table altered.
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
Insert command is used here to add tuples to table to be inserted into specified columns in table student.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 94
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 95
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
15 rows selected.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 96
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 97
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Q3.create a view, named as Student1 with columns as 1,2,3,4 for student table.
It creates a view named std1 with selected columns in the select statement of columns
stdname,stdaddresss,stdphno,stdemail
View created.
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 98
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
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.
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.
It creates a view named colg2 with selected columns in the select statement of columns
coladdress,colphno.
View created.
c ka 25362 c@gmail
ROLLNUMBER:17311A05L1 NAME:D.vikasPage 99
B.TECH 2ND YEAR CSE-D 2ND SEMESTER DATABASE MANAGEMENT SYSTEM LAB
l ap 45346746 l@gmail
m ts 4534367 m@gmail
n ts 4534367 m@gmail
o jk 454677 n@gmail
15 rows selected.
STD
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
15 rows selected.
COLID COLNAME
---------- --------------------
1 jntu
2 ou
3 cbit
4 vasv
5 vnr
6 snist
7 mgit
8 gkrj
9 cvsr
10 nmlrit
11 vadman
COLID COLNAME
---------- --------------------
12 vbit
13 anu
14 arora
15 mega
15 rows selected.
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
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.
View created.
Q8. Apply all DML, DQL, DDL, TCL commands on the above views.
1 row created.
STD
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
16 rows selected.
STDFATHERNAME VARCHAR2(20)
STDBRANCH VARCHAR2(20)
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.
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
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
15 rows selected.
STDFATHERNAME VARCHAR2(20)
STDBRANCH VARCHAR2(20)
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.
c ka 25362 c@gmail
l ap 45346746 l@gmail
m ts 4534367 m@gmail
n ts 4534367 m@gmail
o jk 454677 n@gmail
15 rows selected.
The SQL UPDATE Query is used to modify the existing records in a table specified in where clause.
1 row updated.
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.
c ka 25362 c@gmail
l ap 45346746 l@gmail
m ts 4534367 m@gmail
n ts 4534367 m@gmail
o jk 454677 n@gmail
15 rows selected.
STDNAME VARCHAR2(20)
STDADDRESSS VARCHAR2(20)
STDPHNO NUMBER
STDEMAIL VARCHAR2(20)
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.
c ka 25362 c@gmail
l ap 45346746 l@gmail
m ts 4534367 m@gmail
n ts 4534367 m@gmail
o jk 454677 n@gmail
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
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SALARY
----------
5000
9000
10000
8000
2. Display all records where salary is less than 10000 and age greater than 25.
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;
MAX(SALARY)
-----------
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.
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.
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.
COUNT(EID)
----------
2
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
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
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);
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
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 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
8 rows selected.
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.
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.