DBMS
DBMS
Submitted By:
Section: Bheri
Symbol No.: 379/23
Submitted To:
Milan Chikanbanjar
Bhaktapur Multiple Campus
This is certified to be the record of work done by Miss in the Database Management
System of BHAKTAPUR MULTIPLE CAMPUS in the Department of Database
Management System during Year 2024 A.D.
_______________________________
(MILAN CHIKANBANJAR)
BBA Faculty
2
EVALUATION SHEET
We have concluded the viva-voice examination of the Lab Record presented by and found
the record to be the original work and according to the prescribe format of Database
Management System.
……………….…… …………………..….
(Internal evaluator) (External Evaluator)
3
N
4
N
5
N
Output:
Output:
6
N
8. Create table Student(roll_no-primary key, name-unique, address, gender-not null, age(>18 and
<35))
Syntax:
Use CampusBMC;
Create table student
(
Roll_no int(5) primary key,
Name varchar(6) unique,
Address varchar(20),
Gender varchar(30) not null,
Age int Check (age>18 and age<35)
);
Show tables;
Describe Student;
Output:
7
N
Output:
8
N
13. Add a column named Age of datatype integer and size 3 in the client table.
Syntax:
Alter table client add column age int(3);
Describe client;
14. Add a column named Phone of datatype integer and size 10 in the client table.
Syntax:
Alter table client add column phone int(10);
Describe client;
9
N
15.Add a column named email of datatype varchar and size 20 in the client table immediately after the
column Name.
Syntax:
Alter table client add column email varchar(30) after name;
Describe client;
15. Add a column named registration no of datatype integer and size 20 in client table immediately
after id.
Syntax:
Alter table client add column registration int(20) after id;
Describe client;
16. Add a column named country of datatype varchar and size 15 in the client table as the first
column.
Syntax:
Alter table client add column country varchar(15) first;
Describe client;
Output for 15, 16 and 17
10
N
18. Modify the size of the column email to 25 in the client table;
Syntax:
Alter table client modify email varchar(25);
Describe client;
Output:
19. Change the column name Age to CntAge in the client table.
Syntax:
Alter table client rename column age to cntage;
Describe client;
Output:
11
N
20. Change the column address to location in the client table and shift it immediate after the column
name.
Syntax:
Alter table client rename column address to location;
Describe client;
Alter table client modify location varchar(30) after name;
Describe client;
Output:
12
N
Syntax:
Create table book
(
BookID int(5) primary key,
BookName varchar(23) not null,
Author varchar(45) unique,
Price int check (price>500)
);
Output:
Syntax:
Create table customers
(
custID int(56) primary key,
custName varchar(67) not null,
email varchar(55) unique,
issueDate date,
BookID int,
Foreign key (BookId) references book (bookID)
);
13
N
Output:
14
N
Syntax:
Create tables students
(
SID int(5) primary key,
SName varchar(99) not null,
Age int(67),
DOB date,
Phone int(15) unique,
TID int,
Foreign key (TID) references teachers (TID)
);
Output:
27. Write SQL statement to add a new column ‘address’ in student table after SName.
Syntax:
Alter table students
Add address varchar(77)
After SName;
Output:
15
N
28. Write SQL statement to add a new column ‘RollNo’ in student table in first and ‘fee’ after phone.
Syntax':
Alter table students
Add rollno int(78) first,
Add Fee int(34) after phone;
Output:
30. Write SQL statement to modify the size of column ‘SName’ in student table to varchar(50).
Syntax:
Alter table students
Modify SName varchar(50) not null;
Output:
16
N
31. SQL statement to change the constraints type of column ‘RollNo’ from NULL to NOT NULL and
modify Fee data type from int to varchar in student table.
Syntax:
Alter table students
Modify rollno int not null,
Modify fee varchar(88);
Output:
33.Use departa/departb database. Insert 10 rows into employee table as shown below::
17
N
Syntax:
Create database departa;
Use departa;
Create table employee
(
Empid int (40 ),
Lastname char (52),
Firstname char (55 ),
Gender char (34 ) ,
Title varchar (56)
);
Insert into employee values (1000,”thapa”,”yogita”,”female”,”programmer”);
Insert into employee values (1001,”khanal”,”john”,”male”,”programmer”);
Insert into employee values (1002,”gautam”,”lisa”,”female”,”president”);
Insert into employee values (1003,”chhetri”,”jeni”,”female”,”programmer”);
Insert into employee values (1005,”shrestha”,”matina”,”female”,”accountant”);
Insert into employee values (1006,”K.C”,”chetana”,”female”,”Desinger”);
Insert into employee values (1010,”shrestha”,”ram”,”male”,”programmer”);
Insert into employee values (1011,”nath”,”robin”,”male”,”programmer”);
Insert into employee values (1012,”shrestha”,”ludan”,”male”,”technician”);
Insert into employee values (1013,”pradhan”,”dinesh”,”male”,”designer”);
Output:
18
N
19
N
20
N
21
N
22
N
Syntax:
Create table Department
(
ID Numeric (4) Primary Key,
Name Varchar (25) NOT NULL,
BlockNo Numeric (4)
);
Output:
23
N
Student
Field Data Type Constraint Type
Sid Number(4) Primary Key
Sname Varchar(25) NOT NULL
Saddress Varchar(25)
Age Number(4)
DID Number(4) Foreign Key
Syntax:
Create table Student
(
Sid Numeric (4) Primary Key,
Sname Varchar (25) NOT NULL,
Saddress Varchar (25),
Age Numeric (4),
DID Numeric (4),
Foreign Key (DID) References Department (ID)
);
Output:
Syntax:
Insert into Department values (1, ‘Computer’, 100);
Insert into Department values (2, ‘Mathematics’, 200);
Insert into Department values (3, ‘Economics’, 300);
Insert into Department values (4, ‘Account’, 400);
Output:
24
N
Student
Sid Sname Saddress Age DID
10 Maya Palpa 21 1
11 Abin Ktm 22 2
12 Aarav Ktm 19 1
13 Ashna Palpa 33 3
14 Anju Pokhara 23 4
15 Manish Banepa 22 2
16 Pinky Ktm 43 1
Syntax:
Insert into Student values (10, ‘Maya’, ‘Palpa’, 21, 1);
Insert into Student values (11, ‘Abin’, ‘Ktm’, 22, 2);
Insert into Student values (12, ‘Aarav’, ‘Ktm’, 19, 1);
Insert into Student values (13, ‘Ashna’, ‘Palpa’, 33, 3);
Insert into Student values (14, ‘Anuj’, ‘Pokhara’, 23, 4);
Insert into Student values (15, ‘Manish’, ‘Banepa’, 22, 2);
Insert into Student values (16, ‘Pinky’, ‘Ktm’, 43, 1);
Output:
25
N
52. Find name, department id and age of all student whose address is not ‘ktm’.
Syntax:
Select Sname, DID, Age From Student
Where Saddress <> ‘Ktm’;
Output:
53. Change address of all student whose age is less than 25 to ‘Bhaktapur’.
Syntax:
Update Student Set Saddress = ‘Bkt’
Where Age < 25;
Output:
54. Update name of student to ‘Punty’ and address to ‘Lalitpur’ whose age is greater than 35 and id
also greater than 15.
Syntax:
Update Student Set Sname = ‘Punty’, Saddress = ‘Lalitpur’
Where Age > 35 And Sid > 15;
Output:
26
N
56. Delete the record of the student whose age is greater than 35.
Syntax:
Delete From Student
Where Age > 35;
Output:
57. Delete the student whose address is ‘Palpa’ and age greater than 30.
Syntax:
Delete From Student
Where Saddress = ‘Palpa’ And Age > 30;
Output:
58. Find name, address and department id of those student whose department id is 1.
Syntax:
Select Sname, Saddress, DID From Student
Where DID = 1;
Output:
27
N
59. Find id and name of all student whose name contains the characters ’a’ and ‘n’ in the name.
Syntax:
Select Sid, Sname From Student
Where Sname Like ‘%a%n%’;
Output:
60. Find id and name of the student whose name start with letter ‘a’.
Syntax:
Select Sid, Sname From Student
Where Sname Like ‘A%’;
Output:
61. Find name, address and age of the student whose age is in the range of 20 to 30.
Syntax:
Select Sname, Saddress, Age From Student
Where Age Between 20 And 30;
Output:
62. Find id, name and department id of student whose department id is not between 2 and 4.
Syntax:
Select Sid, Sname, DID From Student
Where DID Not Between 2 And 4;
Output:
63. Find name of those department whose block number is greater than 200.
Syntax:
Select Name From Department
Where BlockNo >200;
Output:
28
N
64. Find name and block number of department of name as ‘Computer’, ‘Account’, ‘Physics’ or
‘English’.
Syntax:
Select Name, BlockNo From Department
Where Name In (‘Computer’, ‘Account’, ‘Physics’, ‘English’);
Output:
29
N
30
N
70. Display 5 records having country_id and country from the table of country.
Syntax:
select country_id, country from country limit 5;
Output:
31
N
73. List out all country whose country_id greater than 105.
Syntax:
Select country from country where country_id >105;
Output:
74. Display records having country_id and country from the table of country whose country_id less
than 9.
Syntax:
Select country,country_id from country where country_id <9;
Output:
75. Display records having country from the table of country whose country_id greater than 102.
Syntax:
Select country,country_id from country where country_id >102;
Output:
32
N
78. Display name, code, continent and surface area top 3 rows of country table.
Syntax:
select name, code, continent, surface area from country limit 3;
Output:
79. Display name, code, continent and surfacearea top 10 rows of country table.
Syntax:
select name,code,continent,surfacearea from country limit 10;
Output:
80. Display name, code, continent and surfacearea from country table whose continent is Asia.
Syntax:
Select name,code,continent,surfacearea from country
where continent="Asia";
Output:
33
N
81. Display name, code, continent and surfacearea from country table whose continent is Asia.
Syntax:
select name,code,continent,surfacearea from country
where surfacearea > 1000000;
Output:
82. List name of country where the second character in its name is 'e'.
Syntax:
select name from country
where name like '_e%';
Output:
34
N
83. List name of country where the third character in its name is 'p'.
Syntax:
select name from country where name like '__p%';
Output:
84. List top 10 country where the last character in its name is not 'an'.
Syntax:
select name from country where name not like '%an' limit 10;
Output:
85. List top 15 country where the third character in its name has either 'e' or 'p' .
Syntax:
select name from country where name like '%an' limit 10;
Output:
86. List name and continent from country whose country name has four character.
Syntax:
select name,continent from country where name like '____';
Output:
35
N
87. List name and continent from country whose country name has four character and the last
character is 'n'.
Syntax:
select name,continent from country where name like '___n';
Output:
88. List name and continent from country whose country name has four character whose first
character starrts withh 'p' and the last character is 'n'.
Syntax:
select name,continent from country where name like 'p__u';
Output:
90. List name and continent from country whose country name has third character ‘p’ having five
character in total;
Syntax:
select name, continent from country where name like '__p__';
Output:
36
N
91. Display name and surfacearea of country whose surfacearea lies between 400 and 600;
Syntax:
select name, surfacearea from country where surfacearea between 400 and 600
Output:
92. Display name and surfacearea of country whose surfacearea lies in 200, 260 and 344;
Syntax:
select name, surfacearea from country where surfacearea in (200,260,344);
Output:
93. Display name and population of country whose population ranges from 100000000 to
10000000000;
Syntax:
select name, population from country where population between 100000000 and 10000000000;
Output:
94. Display name, code, continent and surfacearea of country with surfacearea lies between 2000000
to 5000000;
Syntax:
select name, code, continent, surfacearea from country where surfacearea between 2000000 and
5000000;
37
N
Output:
95. List name and continent from country that lies in Asia, Africa and Antactica displaying 5 rows;
Syntax:
select name, continent from country where continent in ('Asia','Africa','Antarctica') limit 5;
Output:
96. List name and continent from country that does not lie in Asia, Africa, Europe, South America and
Antarctica displaying 10 rows;
Syntax:
select name, continent from country
where continent not in ('Asia','Africa','Europe','South America','Antarctica') limit 10;
Output:
97. List name and continent from country whose continentletter starts with E, S and N characters.
Syntax:
select name, continent from country where continent like 'E%' or continent like 'S%' or continent
like ‘N’;
Output:
38
N
39
N
Select * from actor where first_name like “A%” or first_name like “J%” or first_name like “M
%”;
Output:
100. Find actor_id, first_name and last_name from actor whose actor_id lies between 110 to 125.
Syntax:
Select actor_id, first_name, last_name
from actor
where actor_id between 110 and 125;
Output:
101. Find actor_id, first_name and last_name from actor whose actor_id lies between 2, 45, 79, 157,
160, 179, 190 and 200.
Syntax:
Select actor_id, first_name, last_name from actor where actor_id in (2, 45, 79, 157, 160, 179,
190 and 200);
Output:
40
N
102. Find actor_id, first_name and last_name from actor whose last_name starts with ‘J’ and the
last character ends with ‘a’ or ‘e’.
Syntax:
Select actor_id, first_name, last_name from actor where last_name like “j%” and last_name like
“%a” or last_name like “%e”;
Output:
103. Find actor_id, first_name and last_name from actor whose first_name has third character ‘l’
having five character in total.
Syntax:
Select actor_id, first_name, last_name from actor where first_name like “__l__”;
Output;
104. Find film_id, title and rating of film whose rating is G or PG.
Syntax:
Select film_id, title, rating from film
where rating= “G” or “PG”;
Output:
41
N
105. Find film_id, title and rating of film whose film_id ranges from 900 to 970 displaying 15 rows.
Syntax:
Select film_id, title, rating from film where film_id between 900 and 970 limit 15;
Output:
106. Find film_id, title and rating of film whose film_id is 134, 245, 368, 413, 579, 632, 700, 831.
Syntax:
Select film_id, title, rating from film where film_id in (134, 245,368, 413, 579, 632, 700, 831);
Output:
107. Find FID, title, category and rating of film_list whose price is more than equals to 2.
Syntax:
Select * from film_list;
Select FID, title, category, rating from film_list where price >=2;
Output:
42
N
108. Find FID, title, category and rating of film_list whose category is action or horror or comedy.
Syntax:
Select FID, title, category, rating from film_list where category= “action” or “horror” or
“comedy”;
Output:
109. Find FID, title, category and rating of film_list whose category is sci-fi.
Syntax:
Select FID, title, category, rating from film_list where category= “sci-fi”;
Output:
110. Find FID, title, category and rating of film_list whose rating is R and price is less than 3.
Syntax:
Select FID, title, category, rating from film_list where rating= “R” and price<3;
Output:
43
N
113. Find all payment_id, rental_id, amount and staff_id of payment table whose customer_id is 555.
Syntax:
Select payment_id,rental_id,amount,staff_id from payment where customer_id=555;
44
N
Output:
114. Find all payment_id, rental_id, amount and staff_id of payment table whose customer_id is
123,234,345,456, and 567.
Syntax:
Select payment_id, rental_id, amount, staff_id from payment
where customer_id in(123,234,345,456,567);
Output:
115. Find all payment_id, rental_id, amount and staff_id of payment with payment id in the range
15000 to 15050.
Syntax:
Select payment_id,rental_id,amount,staff_id from payment
where payment_id between 15000 and 15050;
Output:
45
N
117. Find FID, title, category and rating of flim_list whose category has three characters.
Syntax:
Select FID,title,category,rating from film_list where category like “___”;
Output:
118. Find FID, title, category and rating of flim_list whose category starts with T, the third character
starts with third character is A and the last character is L.
Syntax:
Select FID,title,category,rating from film_list where category like “T_A%L”;
Output:
46
N
120. Find the category_id and name of category_id is 6,9,12 and 15.
Syntax:
Select category_id,name from category where category_id in (6,9,12,15);
Output:
1 Ajay
2 Bijay
3 Chandan
4 Dijen
Syntax:
Use sakila;
Create table Mstud
(
Bid int(5),
47
N
Inserting values
Syntax:
Insert into Mstud Values (1,"Ajay "),
(2,"Bijay "),
(3,"Chandan"),
(4,"Dijen");
Output:
Syntax:
Select * from Mstud;
Output:
Gid Girlname
1 Alisha
2 Basanti
5 Emma
6 Shova
Creating Table:
Syntax:
Create table Fstud
(
Gid int(5),
48
N
Inserting values:
Syntax:
Insert into Fstud Values(1, "Alisha"),
(2,"Basanti "),
(5,"Emma "),
(6,"Shova");
Output:
Syntax:
Select * from Fstud;
Output:
For Fstud:
Syntax:
select * from Fstud
Inner join Mstud on
Bid=gid;
49
N
Output:
For Fstud:
Syntax:
Select * from Fstud
Left join Mstud on
Bid=gid;
Output:
50
N
For Fstud:
Syntax:
Select * from Fstud
Right join Mstud on
Bid=gid;
Output:
For Fstud:
Syntax:
Select * from Fstud
Full join Mstud on
Bid=gid;
Output:
Use world
127. Display average gnp from country whose continent is Asia.
Syntax:
Select avg(gnp) from country where continent = “asia”;
Output:
51
N
130. Display maximum capital from country and find name of the country having
maximum capital.
Syntax:
Select name from country
where capital = (select max(capital) from country);
Output :
52
N
133. Count the number of country which has population more than 1,00,00,000.
Syntax:
Select count (population) from country
where population > 10000000;
Output :
134. Count the number of country and use alias as “Number” which has population more than
20,00,00,000.
Syntax:
53
N
137. Display all details from city table whose name starts with N and end with d followed by name,
code, continent from country table whose country name is Nepal.
Syntax:
Select * from city
where name like “n%d”;
Select name, code, continent from country
where name = “Nepal”;
Output :
54
N
138. Display all details from city table whose name starts with “A” and end with ‘d’ followed by
name, code, continent from country table whose country name is Nepal and again followed by all
details from country language whose language is Nepali.
Syntax:
Select * from city
where name like “a%d”;
select name, code, continent from country
where name = “Nepal”;
select * from countrylanguage
where language = “Nepali”;
Output :
55
N
Syntax:
Create table StudentMarks
(
ID int (45),
Name char (52),
Address varchar (45),
Age bigint (23),
Marks bigint (43)
);
56
N
Output:
141.Create a View named DetailsView from the table StudentDetails containing name, address and
SID less than 5.
Syntax:
Create View DetailsView as
Select name, address, SID
from StudentDetails
where SID < 5;
Output:
143.Create a view named StudentNames from the table StudentDetails with SID,
Name order by name.
Syntax:
57
N
145. Create a View named MarksView from the tables StudentDetails and
StudentMarks containing SID, Name, Address, Marks whose names are same in
both tables.
Syntax:
Create View MarksView as
Select sd.SID,sd.Name,sd.Address,sm.Marks
From StudentDetails sd
Join StudentMarks sm ON sd.Name = sm.Name;
Output:
58
N
147. Create a View named AgeView from the table StudentMarks containing Name
and Age.
Syntax:
Create View AgeView AS
select Name, Age
from StudentMarks;
Output:
59
N
Output:
Syntax:
Insert into Viewdetails (name, address)
values ('suresh', 'gurgaon');
Output:
153. Dis
play all
60
N
154. Delet
e the
record of
Suresh
from
AgeView.
Syntax:
D
el
et
e
from ageview where name =”suresh”;
Output:
155. Dis
play all
156. D
rop
the
View
MarksView.
Syntax:
DROP VIEW MarksView;
Output:
61
N
Sub
Queries:
158. Create Employee table with following columns: empid numeric (10), name varchar (20), salary
numeric (10) and department varchar (20).
Syntax:
Create table Employee
(
Empid numeric (10),
Name varchar(20),
Salary numeric(10),
Department varchar(20)
);
Output:
159. Create Departments table with following columns: deptid numeric (10), department varchar (20).
Syntax:
Create table Departments
(
Deptid numeric (10),
Department varchar (20)
);
Output:
62
N
63
N
163. Select those employees whose salary is less than average salary of all employees.
Syntax:
Select * from Employee
Where salary < (select avg(salary) from employee);
Output:
164. Select all records of those employees whose salary is greater than or equal to average salary of
all employees.
Syntax:
Select * from Employee
Where salary >= (select avg(salary) from employee);
Output:
165. Select name of those employees whose salary is maximum of all employees.
Syntax:
Select name from Employee
Where salary = (select max(salary) from employee);
Output:
64
N
166. Select name of those employees whose salary is minimum of all employees.
Syntax:
Select name from Employee
Where salary = (select min(salary) from employee);
Output:
167. Select name and salary of those employees whose salary is less than or equal to total sum of
salary of all employees.
Syntax:
Select name, salary from Employee
Where salary <= (select sum(salary) from employee);
Output:
Output:
65
N
170. Select EmpId and Name of employee from Employee where salary is less than average salary
and department is same in both Employee and Department tables.
Syntax:
Select empId, name from employee
Where salary < (select avg(salary) from employee) and department in (select department from
departments);
Output:
66