0% found this document useful (0 votes)
10 views66 pages

DBMS

This document is a practical lab record for the Database Management System course at Tribhuvan University, detailing various SQL commands and their outputs. It includes tasks such as creating and dropping databases and tables, inserting and updating records, and modifying table structures. The record is submitted by a student and evaluated by faculty members as part of the course requirements.

Uploaded by

safilnapit8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views66 pages

DBMS

This document is a practical lab record for the Database Management System course at Tribhuvan University, detailing various SQL commands and their outputs. It includes tasks such as creating and dropping databases and tables, inserting and updating records, and modifying table structures. The record is submitted by a student and evaluated by faculty members as part of the course requirements.

Uploaded by

safilnapit8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

TRIBHUVAN UNIVERSITY

BHAKTAPUR MULTIPLE CAMPUS


DUDHPATI, BHAKTAPUR

A PRACTICAL LAB RECORD


OF
DATABASE MANAGEMENT SYSTEM

Submitted By:

Section: Bheri
Symbol No.: 379/23

Submitted To:
Milan Chikanbanjar
Bhaktapur Multiple Campus

(In the partial fulfillment of the requirement for the course


Database Management System 2024
CERTIFICATE

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.

Name: Course: BBA


Symbol No: 379/23 Semester: 2nd

_______________________________
(MILAN CHIKANBANJAR)
BBA Faculty

Submitted for the practical examination held on________________________________

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

1. Show all databases.


Syntax:
 Show databases;
Output:

2. Create a database staff, colz and departt.


Syntax:
 Create database staff;
 Create database colz;
 Create database departt;
Output:

3. Drop database staff and colz.


Syntax:
 Drop database staff;
 Drop database colz;
Output:

4
N

4. Create a table employee in depart database with following columns:


EmpID Integer Not Null
Lastname Varchar Not Null
FirstName Varchar Not Null
Gender Varchar Not Null
Title Varchar Not Null
Syntax:
 Use Departt;
 Create Table Employee
(
EmpID Int(9) Not Null,
Lastname varchar(30) not null,
FirstName varchar(30) not null,
Gender varchar(20) not null,
Title varchar(15)
);
Output:

5. Create table student (roll int) and post (id int).


Syntax:
 Create table student
(
Roll int
);
 Create table post
(
Id int
);

5
N

Output:

6. Drop table student and post:


Syntax:
 Drop table student;
 Drop table post;
Output:

7. Create database CampusBMC


Syntax:
 Create database CampusBMC;
 Show databases;

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:

9. Create table class(room_no-Primary key, no_bench-not null)


Syntax:
 Create table class
(
Room_no int(20) primary key,
No_bench int(30) not null
);
 Show tables;
 Describe class;

7
N

Output:

10. Drop table class.


Syntax:
 Drop table class;
 Show tables;
Output:

11. Rename table student into studentinfo.


Syntax:
 Show tables;
 Alter table student rename to studentinfo;
 Show tables;
Output:

8
N

12. Create table client(id – primary key, name, address)


Syntax:
 Create table client
(
Id int(20) primary key,
Name char(20),
Address varchar(30)
);
 Show tables;
 Describe client;
Output :

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;

Output for 13 and 14:

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

17. Drop the column phone from the client table.


Syntax:
 Alter table client drop column phone;
 Describe client;
Output:

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:

21. Create table employee (eno-primary key, ename-unique, salary, post).


Syntax:
 Create table employee
(
Eno int(10) primary key,
Ename varchar(30) unique,
Salary int(10),
Post varchar(30)
);
 Show tables;
 Describe employee;
Output:

12
N

23. create table books as described below:


Field Data type Constraint type
BookID Integer PRIMARY KEY
BookName varchar NOT NULL
Author varchar UNIQUE
Price Integer Check price>500

Syntax:
 Create table book
(
BookID int(5) primary key,
BookName varchar(23) not null,
Author varchar(45) unique,
Price int check (price>500)
);
Output:

24. create table customer as described below:


Field Data type Constraint type
CustID integer PRIMARY KEY
CustName varchar NOT NULL
Email varchar UNIQUE
IssueDate date
BookID integer FOREIGN KEY

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:

25.Create table teacher as described below:


Field Data type Constraint type
TID Integer PRIMARY KEY
TName Varchar NOT NULL
Address Varchar
Salary Integer NOT NULL
Syntax:
 Create table teachers
(
TID int(5) primary key,
TName varchar(56) not null,
Address varchar(78),
Salary int(45) not null
);
Output:

26. create table student as described below:


Field Data type Constraint type
SID integer PRIMARY KEY
SName varchar NOT NULL
Age integer
DateOfBirth date
phone integer UNIQUE
TID integer FOREIGN KEY

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:

29. write SQL statement to drop a column ‘address ‘ in student table.


Syntax:
 Alter table students
 Drop column address;
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:

32.write SQL Statement to rename column phone to ContactNo in student table.


Syntax:
 Alter table students
Change phone contact_no varchar(88);
Output:

33.Use departa/departb database. Insert 10 rows into employee table as shown below::

EmpID Lastname FirstName Gender Title


1000 Thapa Yogita Female Programmer
1001 Khanal John Male Programmer
1002 Gautam Lisa Female President
1003 Chhetri Jeni Female Programmer
1005 Shrestha Matina Female Accountant
1006 K.C Chetana Female Designer
1010 Shrestha Ram Male Programmer
1011 Nath Robin Male Programmer
1012 Shrestha Ludan Male Technician
1013 Pradhan Dinesh Male Designer

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

34.Update the title of Jeni to QA.,


Syntax :
 Update employee
Set title =”QA”
Where firstname=”jeni”;
Output:

35.Update the Female gender to ‘F’.


Syntax:
 Update employee
Set gender=”f”
Where gender= “female”;
Output:

36. Update the gender Male to ‘M’.


Syntax:
 Update employee
Set gender=”m”
Where gender=”male”;
Output:

19
N

37.Delete the record of employee whose title is QA.


Syntax:
 Delete from employee
Where title =”QA”;
Output:

38. Delete the record of President title.


Syntax:
 Delete from employee
Where title =”president”;
Output:

39. Use a MultipleA/BMultipleB database.Insert 4 rows into the table studentinfo.


Syntax:
 Insert into studentinfo values (1,’am’,’bkt’,’m’, 20);
 Insert into studentinfo values (2,’raj’,’ktm’,’m, 21);
 Insert into studentinfo values (3,’rita’,’thimi’,’f’, 19);
 Insert into studentinfo values (4,’sita’,’pokhara’,’f’, 21);
Output:

20
N

40. Delete row having age 20.


Syntax:
 Delete from studentinfo where age=20;
Output:

41. Update rows having age 21 to 23.


Syntax:
 Update studentinfo set age=23 where age=21;
Output:

42. Insert three rows in teacher table.


Syntax;
 Insert into teacher values (101,’ram’,’ktm’, 20000);
 Insert into teacher values (103,’raj’,’bkt’, 30000);
 Insert into teacher values (105,’rita’,’manang’, 30000);
Output:

43. Delete all rows of teacher table.


Syntax:
 Delete from teacher;
Output:

21
N

44. Insert 2 rows into client.


Syntax:
 Insert into client values (‘Nepal’, 22,23223,’hitler’,’germany’,’[email protected]’,48);
 Insert into client values (‘bkt’,’12,23223,’ram’,’roneyy’,’[email protected]’,48);
Output:

45. Delete/Truncate all rows of client.


Syntax:
 Delete from client;
Output:

46. Insert 4 rows in the employee table.


Syntax:
 Insert into employee values (1,’ram’,10000,’director’);
 Insert into employee values (2,’raj’,1000,’employee’);
 Insert into employee values (3,’nischal’,10000,’ceo’);
 Insert into employee values (4, ‘sudip’,100000,’president’);
Output:

22
N

47. Update the salary of employee with 20%.


Syntax:
 Update employee set salary=1.2*salary;
Output:

48. Create database bktA and use it.


Syntax:
 Create database bktA;
 Use bktA;
Output:

49. Create the following tables.


Department
Field Data type Constraint type
ID Number(4) Primary key
Name Varchar(25) NOT NULL
BlockNo Number(4)

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:

50. Insert following data in following tables.


Department
ID Name BlockNo
1 Computer 100
2 Mathematics 200
3 Economics 300
4 Account 400

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:

51. Find unique address of student.


Syntax:
 Select Distinct Saddress from Student;
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

55. Delete the record of the student ‘Anuj’.


Syntax:
 Delete From Student
Where Sname = ‘Anju’;
Output:

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:

65. Find name of department in descending order.


Syntax:
 Select Name From Department
Order By Name DESC;
Output:

66. Use sakila.


Syntax:
 Use sakila;
Output:

67. Show tables in sakila database.


Syntax:
 show tables;
Output:

29
N

68. Display all records of country.


Syntax:
 Select * from country;
Output:

30
N

69. Display all 8 records of country table.


Syntax:
 Select * from country limit 8;
Output:

70. Display 5 records having country_id and country from the table of country.
Syntax:
 select country_id, country from country limit 5;
Output:

71. Display 10 records of country table.


Syntax:
 select * from country limit 10;
Output:

72. List out all country whose country_id less than 7.


Syntax:
 select country from country where country_id <7;
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:

76. Use world.


Syntax:
 Use world;
Output:

77. Display all records of country.


Syntax:
 select * from country;
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:

89. Use world;


Syntax:
 Use world;
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

98. Use sakila.


Syntax:
 Use sakila;
 Show tables;
Output:

99. List all the actor whose first_name starts with A, J, or M.


Syntax:
 Select * from actor;

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

111. List all distinct category of film_list.


Syntax:
 Select all category from film_list;
 Select distinct category from film_list;
Output:

112. List all distinct staff_id of payment table.


Syntax:
 Select distinct staff_id from payment;
Output:

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

116. List out the amount of customer_id 420 in payment table.


Syntax:
 Select amount from payment where customer_id =420;
Output:

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

119. Find distinct store_id of customer.


Syntax:
 Select distinct store_id from customer;
Output:

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:

121. Create table Mstud with following values:


Bid boyname

1 Ajay

2 Bijay

3 Chandan

4 Dijen

Syntax:
 Use sakila;
 Create table Mstud
 (
 Bid int(5),

47
N

 Boyname varchar (20)


 );
 Describe Mstud;
Output:

Inserting values
Syntax:
 Insert into Mstud Values (1,"Ajay "),
 (2,"Bijay "),
 (3,"Chandan"),
 (4,"Dijen");
Output:

Syntax:
 Select * from Mstud;
Output:

122. Create Table Fstud with following values:

Gid Girlname
1 Alisha
2 Basanti
5 Emma
6 Shova

Creating Table:
 Syntax:
 Create table Fstud
 (
 Gid int(5),

48
N

 Girlname varchar (20)


 );
Output:

Inserting values:
Syntax:
 Insert into Fstud Values(1, "Alisha"),
 (2,"Basanti "),
 (5,"Emma "),
 (6,"Shova");
Output:

Syntax:
 Select * from Fstud;
Output:

123. Use inner Join for tables Mstud and Fstud.


For Mstud:
Syntax:
 Select * from Mstud
 Inner join fstud on
 Bid=gid;
Output:

For Fstud:
Syntax:
 select * from Fstud
 Inner join Mstud on
 Bid=gid;

49
N

Output:

124. Use Left Join for tables Mstud and Fstud.


For Mstud:
Syntax:
 Select * from Mstud
 left join Fstud on
 Bid=gid;
Output:

For Fstud:
Syntax:
 Select * from Fstud
 Left join Mstud on
 Bid=gid;
Output:

125. Use Right Join for tables Mstud and Fstud.


For Mstud:
Syntax:
 Select * from Mstud
 Right join Fstud on
 Bid=gid;
Output:

50
N

For Fstud:
Syntax:
 Select * from Fstud
 Right join Mstud on
 Bid=gid;
Output:

126. Use Full Join for Mstud and Fstud.


For Mstud:
Syntax:
 Select * from Mstud
 Full join Fstud 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

128. Display average capital from country whose continent is Europe.


Syntax:
 Select avg(capital) from country where continent =“Europe”;
Output :

129. Display name of country which has maximum population.


Syntax:
 Select name from country where population = (select max(population) from country);
Output :

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 :

131. Display name of country which has minimum population.


Syntax:
 Select name of country
 where population = (select min(population)from country);
Output :

52
N

132. Display name of country which has minimum gnp.


Syntax:
 Select name of country
 where gnp = (select min(gnp)from country);
Output :

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

 Select count (population) as number from country


 where population > 200000000;
Output:

135. Find the total surfacearea of Asia.


Syntax:
 Select sum(surfacearea) as number from country
 where continent =“Asia”;
Output:

136. Find total gnp of Africa.


Syntax:
 Select sum(gnp) from country where continent =“Africa”;
Output:

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 :

139. Create database BBABherii and use it.


Syntax:
 Create Database BBABherii;
 Use BBABherii;
Output:

55
N

140. Create following tables in the database.


StudentDetails StudentMarks
I Ag
SID Name Address Name Address Marks
D e
1 Harsh Kathmandu
Kathmand
2 Ashish Dharan 1 Harsh 90 19
u
3 Pratik Dhankuta
2 Ashish Dharan 50 20
4 Dhanraj Bhaktapur
3 Pratik Dhankuta 80 19
5 Ram Pokhara
4 Dhanraj Bhaktapur 95 21
6 Suresh Pokhara 85 22
Syntax :
 Create table StudentDetails
 (
 sid int (9),
 Name varchar (20),
 Address Varchar (20)
 );
 Insert into StudentDetail Values (1,”harsh”,”kathmandu”);
 Insert into StudentDetail Values (2,”ashish”,”dharan”);
 Insert into StudentDetail Values (3,”pratik”,”dhankuta”);
 Insert into StudentDetail Values (4,”dhanraj”,”bhaktapur”);
 Insert into StudentDetail Values (5,”ram”,”pokhara”);
Output:

Syntax:
 Create table StudentMarks
(
ID int (45),
Name char (52),
Address varchar (45),
Age bigint (23),
Marks bigint (43)
);

56
N

 Insert into StudentMarks Values (1,”Harsh’,”kathmandu”,19,90);


 Insert into StudentMarks Values(2,”Ashish”,”Dharan”,20,50);
 Insert into StudentMarks Values (3,”Pratik”,”Dhankuta”19,80);
 Insert into StudentMarks Values (4,”Dhanraj”,”Bhaktapur”,21,95);
 Insert into StudentMarks Values (5,”Ram”,”Pokhara”,22,85);

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:

142.Display all information of DetailsView.


Syntax:
 Select * from DetailsView;
Output:

143.Create a view named StudentNames from the table StudentDetails with SID,
Name order by name.
Syntax:

57
N

 Create View StudentNames as


 Select SID, Name
 from StudentDetails
 Order by Name;
Output:

144.Display all information of StudentNames.


Syntax:
 select * from StudentNames;
Output:

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:

146.Display all information of MarksView.


Syntax:
 Select * from MarksView;
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:

148. Display all information of AgeView.


Syntax:
 Select * from AgeView;
Output:

149. Update the age of Suresh to 18 in AgeView.


Syntax:
 Update StudentMarks
Set Age = 18
where Name = 'Suresh';
Output:

150. Display all information of AgeView and StudentMarks.


Syntax:
 Select * from AgeView;

59
N

Output:

 Select * from StudentMarks;


Output:

151. INSERT a row in viewdetails with VALUES("Suresh","Gurgaon").

Syntax:
 Insert into Viewdetails (name, address)
 values ('suresh', 'gurgaon');

Output:

152. Insert a row in Viewdetails with (“Diksha”, “Biratnagar”).


Syntax:
 Insert into Viewdetails (Name, Address)
 Values (“diksha”,”biratnagar”);
Output:

153. Dis
play all

information of DetailsView and StudentDetails.


Syntax:
 Select * from detailsview , studentdetails;
Output:

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

information of AgeView and StudentMarks.


Syntax:
 Select * from ageview, studentmarks;
Output:

156. D
rop
the
View

MarksView.
Syntax:
 DROP VIEW MarksView;
Output:

61
N

157. Display all information of AgeView.


Syntax:
 DELETE FROM AgeView;
 DROP VIEW AgeView;
Output:

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

160. Insert following in Employee table.


empid name salary Department
100 Jacob A 20000 SALE
101 James T 50000 IT
102 Riya S 30000 IT
Syntax:
 Insert into Employee values (100, ‘Jacob A’, 20000, ‘SALE’);
 Insert into Employee values (101, ‘James T’, 50000, ‘IT’);
 Insert into Employee values (102, ‘Riya S’, 30000, ‘IT’);
Output:

161. Insert following in Departments table:


deptid department
1 IT
2 ACCOUNTS
3 SUPPORT
Syntax:
 Insert into Departments values (1, ‘IT’);
 Insert into Departments values (2, ‘ACCOUNTS’);
 Insert into Departments values (3, ‘SUPPORT’);
Output:

63
N

162. Select all employees from department with the department id as 1.


Syntax:
 Select * from Employee
 Where Department = (Select Department from Departments where deptid = 1);
Output:

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:

168. Select all employees whose department is in departments table.


Syntax:
 Select * from employee
 Where department in (select department from departments);

Output:

65
N

169. Select all employees whose department is not in department table.


Syntax:
 Select * from employee
 Where department not in (select department from departments);
Output:

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

You might also like