B.S Anangpuria Institute of Technology and Management

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

B.

S ANANGPURIA INSTITUTE OF
TECHNOLOGY AND MANAGEMENT

DATABASE MANAGEMENT SYSTEM


Lab File
CSE 5th semester

Submitted by:
Aditya NathTejpal
College Roll No.- 19/cs03
University Roll No.-19012004003
1|Page

INDEX

S DATE Program Page


NO. No.

1. Write an Introduction on SQL 1


2. To create table and insert values into it & show the table using select 3
command
3. To use where clause for showing the table with condition select * table 4
name where anything stored
4. To use Filtering Data from Database Using Employee Table. 5

5. To use Distinct keyword for elimination of duplicate data. 7


6. To Sort data items in DBMS. 9
7. To create table from another table and insert values. 11

8. To Delete values from the table . 14


9. To Update values from the table . 16
10. Write an SQL query to fetch “FIRST_NAME” from worker table using
the alias name as<WORKER_NAME>.
11. Write an SQL query to fetch “FIRST_NAME” from worker table in
upper case.
12. Write an SQL query to print the first three characters of “FIRST_NAME”
from worker table.
13. Write an SQL query that fetches the unique values of DEPARTMENT
from worker table.
14. Write an SQL query to print all worker details from the worker table
order by FIRST_NAME ascending.
15. Write an SQL query to print details for workers with the first name as
“Vipul” and “Satish”from worker table.
16. Write an SQL query to print details of workers with department name as
“admin”.
17. Write an SQL query to print details of the workers whose FIRST_NAME
ends with ‘h’ and contains sic alphabets.
18. Write an SQL query to print details of the workers whose SALARY ;ies
between 100000 and 500000.
19. Write an SQL query to print details of the workers who have joined in
feb’2014
20. Write an SQL query to fetch workers names with salaries >=50000 and
<=100000.
2|Page

21. Write an SQL query to print details of the workers who have joined in
feb’2014
22. Write an SQL query to print details of the workers who are also
Managers.
23. Write an SQL query to fetch duplicate records having matching data in
some field of a table.
24. Write an SQL query to show the current and time.

25. Write an SQL query to show the top n(say 10) records of a table.

26. Write an SQL query to fetch the list of employees with the same salary.

27. Write an SQL query to show one row twice in resuls from a table.

28. Write an SQL query to fetch the first 50% records from a table.

29. Write an SQL query to print name of employees having the highest salary
in each department.
30. Write an SQL query to fetch the names of workers who earn the highest
salary.
3|Page

PRACTICAL – 1

AIM:Write an Introduction on SQL

Structured Query Language (SQL) is a standardized programming language that’s used to


manage relational databases and perform various operations on the data in them. SQL is
a domain-specific language used in programming and designed for managing data held in
a relational database management system (RDBMS), or for stream processing in a relational
data stream management system (RDSMS). It is particularly useful in handling structured
data, i.e. data incorporating relations among entities and variables. The scope of SQL
includes data query, data manipulation (insert, update and delete), data definition
(schema creation and modification), and data access control. Although SQL is essentially
a declarative language (4GL), it also includes procedural elements.

HISTORY
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce after
learning about the relational model from Edgar F. Coddin the early 1970s. This version,
initially called SEQUEL (Structured English Query Language), was designed to manipulate
and retrieve data stored in IBM's original quasirelational database management
system, System R, which a group at IBM San Jose Research Laboratory had developed
during the 1970s.
Chamberlin and Boyce's first attempt at a relational database language was SQUARE
(Specifying Queries in A Relational Environment), but it was difficult to use due to
subscript/superscript notation. After moving to the San Jose Research Laboratory in 1973,
they began work on a sequel to SQUARE.[12] The name SEQUEL was later changed to SQL
(dropping the vowels) because "SEQUEL" was a trademark of the UK-based Hawker
Siddeley Dynamics Engineering Limited company. [14] The label Structured Query Language
later became the acronym for SQL.
DATATYPES
A data type specifies a particular type of data, such as integer, floating-point, Boolean etc.

Following table summarizes Oracle built-in data types.

Types Description Size


4|Page

VARCHAR2(size Variable- From 1 byte to 4KB.


[BYTE | CHAR]) length
character
string.

NVARCHAR2(size) Variable- Maximum size is determined with an upper limit of 4000


length bytes.You must specify size for NVARCHAR2.
Unicode
character
string
having
maximum
length size
characters.

NUMBER [ (p [, s]) Number A NUMBER value requires from 1 to 22 bytes.


] having
precision p
and scale s.
Range of p :
From 1 to
38.
Ranges of s
: From -84
to 127.
Both
precision
and scale
are in
decimal
digits.

FLOAT [(p)] A FLOAT A FLOAT value requires from 1 to 22 bytes.


value is
represented
internally as
NUMBER.
Range of p :
From 1 to
126 binary
digits.
5|Page

PRACTICAL – 2

Aim:To create table and insert values into it & show the table using select command.

 The “CREATE TABLE” statement is used to create a new table in a database.


 The “INSERT INTO” statement is used to insert new records in a table.
 The “SELECT” statement is used to select data from a database.
 The data returned is stored in a result table, called the result-set.

Code:
create table avengers( Sr_No number(20), roll_number number(20), name varchar(20),
department varchar(10), section number(5), phoneno number(10));
insert into avengers values (1, 01, 'IRONMAN', 'cse', 1, 9561510564);
insert into avengers values (2, 02, 'CAPTAIN AMERICA', 'cse', 1, 9561510564);
insert into avengers values (3, 03, 'BLACK WIDDOW', 'cse', 1, 9561510564);
insert into avengers values (4, 04, 'THOR', 'cse', 1, 9561510564);
insert into avengers values (5, 05, 'HAWK EYE', 'cse', 1, 9561510564);
insert into avengers values (6, 06, 'SCARLET WITCH', 'cse', 1, 9561510564);
insert into avengers values (7, 07, 'VISION', 'cse', 1, 9561510564);
insert into avengers values (8, 08, 'HULK', 'cse', 1, 9561510564);
select * from avengers;

Output:
SR_NO ROLL_NUMBER NAME DEPARTMENT PHONENO SECTION
1 1 IRONMAN CSE 9561510564 1
2 2 CAPTAIN AMERICA CSE 9561510564 1
3 3 BLACK WIDOW CSE 9561510564 1
4 4 THOR CSE 9561510564 1
5 5 HAWK EYE CSE 9561510564 1
6 6 SCARLET WITCH CSE 9561510564 1
6|Page

7 7 VISON CSE 9561510564 1


8 8 HULK CSE 9561510564 1

PRACTICAL – 3

Aim :To use where clause for showing the table with condition select * table name
whereanything stored

Code:
create table avengers( Sr_No number(20), roll_number number(20), name varchar(20),
department varchar(10), section number(5), phoneno number(10));
insert into avengers values (1, 01, 'IRONMAN', 'IT', 1, 956470564);
insert into avengers values (2, 04, 'CAPTAIN AMERICA', 'cse', 1, 9561510564);
insert into avengers values (3, 43, 'BLACK WIDDOW', 'cse', 1, 9561510564);
insert into avengers values (4, 54, 'THOR', 'BCA', 1, 9561514254);
insert into avengers values (5, 35, 'HAWK EYE', 'IT', 1, 9561545218);
insert into avengers values (6, 52, 'SCARLET WITCH', 'cse', 1, 9532584564);
insert into avengers values (7, 17, 'VISION', 'IT', 1, 9561510114);
insert into avengers values (8, 28, 'HULK', 'cse', 1, 9561510564);
select * from avengers where department ='IT';

Output:

SR_NO ROLL_NUMBER NAME DEPARTMENT PHONENO SECTION


1 1 IRONMAN IT 9561510564 1
5 5 HAWK EYE IT 9561510564 1
7 7 VISON IT 9561510564 1
7|Page

PRACTICAL – 4

Aim :To use Filtering Data from Database Using Employee Table.
Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

4(A):- Display Selected Column and all Rows


Code : -
select Emp_id , Salary from Employee797897;

Output : -
EMP_ SALARY
001 10000
002 20000
003 15000
8|Page

004 25000
005 30000
006 10000
007 15000
008 20000
009 25000
010 15000

4(B):-Display Selected Rows and all Columns.


Code : -
select * from Employee797897
where (Salary >=1000 AND Department = 'CSE');

Output:-
EMP_ EMP_NAME EMP_ADD SALARY DEPART
002 bbb 2222 block A 20000 CSE
004 ddd 4444 block A 25000 CSE
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

4(C):-Display Selected Column and Selected Rows


Code : -
select Emp_id , Salary
from Employee797897
where Emp_Name = 'ddd';

Output:-
EMP_ SALARY
004 25000
9|Page

PRACTICAL – 5

Aim :To use Direct keyword for elimination of duplicate data.


Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE
001 aaa 1111 block A 10000 IT

Using Distinct Keyword:-

Code :-
Select Distinct * from Employee797897;

Output:-
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
10 | P a g e

002 bbb 2222 block A 20000 CSE


003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE
PRACTICAL – 6

Aim :To Sort data items in dbms.


Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

6(A).Usingsort of single column

Code :-
select * from Employee797897
order by EMP_NAME desc;

Output:-
EMP_ EMP_NAME EMP_ADD SALARY DEPART
010 jjj 1010 block A 15000 CSE
11 | P a g e

009 iii 9999 block A 25000 CSE


008 hhh 8888 block A 20000 CSE
007 ggg 7777 block A 15000 HR
006 fff 6666 block A 10000 IT
005 eee 5555 block A 30000 IT
004 ddd 4444 block A 25000 CSE
003 ccc 3333 block A 15000 HR
002 bbb 2222 block A 20000 CSE
001 aaa 1111 block A 10000 IT
6(B).Usingsort of Multiple column

Code :-
select * from Employee797897
order by EMP_NAME , DEPARTMENT ;

Output:-
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE
12 | P a g e

PRACTICAL – 7

Aim :To create table from another table and insert values.
Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

7(A). Create table from existing table


Code :-
create table Employee8000
(Emp_id , Department)
as select Emp_id , Department
from Employee797897;
describe Employee8000;
13 | P a g e

Output:
Name Null? Type
EMP_ID VARCHAR2(4)
DEPARTMENT VARCHAR2(6)

7(B). Insert data to new table


Code :-
insert into Employee8000
select Emp_id , Department
from Employee797897;
select * from Employee8000;

Output:
EMP_ DEPART
001 IT
002 CSE
003 HR
004 CSE
005 IT
006 IT
007 HR
008 CSE
009 CSE
010 CSE

7(C). Insert data to new table(specific value)


Code :-
create table Employee9000
(Emp_id , Department)
as select Emp_id , Department
from Employee797897;
describe Employee9000;

insert into Employee9000


14 | P a g e

select Emp_id , Department


from Employee797897 where(Emp_id>= 005);
select * from Employee9000;

Output:

EMP_ DEPART
005 IT
006 IT
007 HR
008 CSE
009 CSE
010 CSE
15 | P a g e

PRACTICAL – 8

Aim :To Delete values from the table .


Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

8(A). Delete all values from table:-


Code :-
Delete from Employee797897;
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
16 | P a g e

8(B). Delete Specific values from table:-


Code :-
Delete from Employee797897
Where ( EMP_NAME = “aaa”);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE
010 jjj 1010 block A 15000 CSE

8(C). Delete all values from table:-


Code :-
Drop Employee797897;
select* from Employee797897;

Output:
No output, Table doesn’t Exist
17 | P a g e

PRACTICAL – 9

Aim :To Update values from the table .


Code:
create table Employee797897(Emp_id varchar2(4), Emp_Namevarchar(15), Emp_add
varchar(20), Salary number(10), Department varchar2(6));
insert into Employee797897values('&Emp_id, &Emp_Name, &Emp_add , &Salary,
&Department);
select* from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 HR
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 IT
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 HR
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE

9(A). Update all rows from table:-


Code :-
UpdateEmployee797897Set Department = ‘CSE’;
select * from Employee797897;

Output:
EMP_ EMP_NAME EMP_ADD SALARY DEPART
001 aaa 1111 block A 10000 CSE
002 bbb 2222 block A 20000 CSE
18 | P a g e

003 ccc 3333 block A 15000 CSE


004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 CSE
006 fff 6666 block A 10000 CSE
007 ggg 7777 block A 15000 CSE
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE

9(B). Update selected (specific rows) rows from table:-


Code :-
Update Employee797897 Set Department = ‘CSE’
Where salary >=15000;
select * from Employee797897;

EMP_ EMP_NAME EMP_ADD SALARY DEPART


001 aaa 1111 block A 10000 IT
002 bbb 2222 block A 20000 CSE
003 ccc 3333 block A 15000 CSE
004 ddd 4444 block A 25000 CSE
005 eee 5555 block A 30000 CSE
006 fff 6666 block A 10000 IT
007 ggg 7777 block A 15000 CSE
008 hhh 8888 block A 20000 CSE
009 iii 9999 block A 25000 CSE

You might also like