0% found this document useful (0 votes)
12 views

DBMS Lab File

Dbms

Uploaded by

joykundu2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DBMS Lab File

Dbms

Uploaded by

joykundu2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

EXPERIMENT 1

1. Create a table in SQL with table name in departments and attributes are deptno, name,
location.

create table departments (

deptno number,

name varchar2(20),

location varchar2(20)

);

2. Write a SQL statement to display the schema of departments.

desc departments;

3. Write a SQL statement to insert these rows into departments table.

insert into departments(deptno, name, location)

values(1,'CSE','Kalyani');

insert into departments(deptno, name, location)

values(2,'CST','Kalyani');

insert into departments(deptno, name, location)

values(3,'EE','Kolkata');

insert into departments(deptno, name, location)

values(4,'ECE','Agarpara');
4. Write a SQL statement to display all the records in the departments table.

select * from departments;

5. Write a SQL statement to display only deptno from departments table.

select distinct deptno from departments;

6. Write a SQL statement to display only deptno from departments table.

select name, location from departments;

7. Write a SQL statement to add new column ‘no_of_emp’ in existing departments table.

alter table departments add no_of_emp number;


8. Write a SQL statement to update departments table to add value in no_of_emp = 11,
column where deptno=1.

update departments set no_of_emp = 11 WHERE deptno = 1;

9. Write a SQL to display the departments table.

select * from departments;


EXPERIMENT 2
1. Create a empl103 table with column (eid, ename, eaddress, esal).

create table empl103 (

eid number,

ename varchar2(20),

eaddress varchar2(20),

esal number

);

2. Insert values.

insert into empl103(eid, ename, eaddress, esal)

values(100, 'Alex', 'Kolkata', 10001);

insert into empl103(eid, ename, eaddress, esal)

values(200, 'William', 'Kolkata', 10010);

insert into empl103(eid, ename, eaddress, esal)

values(249, 'Robin', 'Karnataka', 20002);

insert into empl103(eid, ename, eaddress, esal)

values(210, 'Antonio', 'Kerala', 30003);

insert into empl103(eid, ename, eaddress, esal)

values(209, 'Mark', 'Mumbai', 51000);

insert into empl103(eid, ename, eaddress, esal)

values(1, 'A K Ghosh', 'Tamil Nadu', 20001);


3. Select the name of the employee whose salary is 10001 and address in Kolkata.

select ename from empl103 WHERE eaddress='Kolkata' and esal=10001;

4. Select the name of the employee whose salary is greater 10000 and address in Kolkata.

select ename from empl103 WHERE eaddress='Kolkata' and esal>10000;

5. Select the name of the employee whose salary is greater than 10000.

select ename from empl103 WHERE esal>10000;

6. Find the salary of the employee id is 209.

select ename, esal, eaddress from empl103 WHERE eid='209';

7. Find the address and name of the employee whose salary is greater than 30000.

select ename, eaddress from empl103 WHERE esal>30000;

8. Find the employee id and salary who belongs to Mumbai.

select eid, ename, esal from empl103 WHERE eaddress='Mumbai';


9. Update the address of Mr. AK Ghosh with new address will be Kolkata.

update empl103 set eaddress='Kolkata' WHERE eid='1' and ename='Mr AK Ghosh';

10. Find all the employee names.

select * from empl103;

11. Find the employee id and name ,salary is greater than 30000 and less than 50000.

select eid,ename from empl103 WHERE esal>30000 and esal<50000;

12. Find the address and salary of employee id 249.

select eaddress,esal from empl103 WHERE eid=249;


EXPERIMENT 3

1. Create a table student103 with attributes first name, middle name, last name, subject, age,
game and insert some values.

create table student103 (

first_name varchar(20),

middle_name varchar(20),

last_name varchar(20),

subject varchar(20),

age number,

game varchar(20)

);

2. Insert into student103.

insert into student103 (first_name, middle_name, last_name, subject, age, game)

values('Ram','Kumar','Verma','Maths',12,'Football'),

('Shyam','Lal','Gupta','Physics',14,'Cricket'),

('Geeta','Devi','Patel','Chemistry',13,'Hockey'),

('Ramesh','Kumar','Singh','Maths',11,'Football'),

('Serama','Devi','Patel','English',29,'Cricket'),

('Sita','Devi','Sharma','CS',15,NULL),

('Aryan','Kumar','Mishra','English',27, NULL);
3. Find the first name and last name of the student whose subject is maths or physics.

select first_name, last_name from student103

where subject in ('Maths', 'Physics');

4. Find the first name and age of the student whose age is between 10 to 15.

select first_name, last_name, age from student103

where age between 10 and 15;

5. Find the first name, last name and game of the student whose game is football.

select first_name, last_name, game from student103

where game='Football';

6. Find the first name, last name and game of the student whose age is between 10 to 15 or
game is not football.

select first_name, last_name, game from student103

where (age between 10 and 15) and (game != 'Football');


7. Find the first name and last name of the student whose first name starts with r.

select first_name, last_name from student103

where first_name like 'r%';

8. Find the first name and the last name of the student whose first name's second letter is -n.

select first_name, last_name from student103

where first_name like '-n%';

9. Find the first name, last name and age of the student whose age is between 10 to 15 using
BETWEEN and AND keywords.

select first_name, last_name, age from student103

where age >= 10 AND age <= 15;

10. Find first name, last name of the student whose subject is maths or cs using IN keywords.

select first_name, last_name from student103

where subject IN ('Maths', 'CS');


11. Find the first name, last name of the student whose game is null.

select first_name, last_name from student103

where game is NULL;

12. Find first, last name and subject of the student whose subject is neither maths nor physics
using NOT IN keywords.

select first_name, last_name, subject from student103

where subject NOT IN ('Maths', 'Physics');


EXPERIMENT 4

1. Find the first name, last name, subject of the student in ascending order to their first
name.

select first_name, last_name, subject from student103

order by first_name;

2. Count the number of students whose subject is maths.

select count(*) as total_maths from student103

where subject='Maths';

3. Count the number of records present in the table.

select count(*) as total_records from student103;

4. Select distinct subjects of table.

select distinct subject from student103;


5. Count the number of distinct subjects of the table.

select count(distinct subject) as distinct_subject from student103;

6. Find the total age of students by subjects using group by keyword.

select subject, sum(age) as total_age from student103 group by subject;

7. Find the age of the students which is greater than 25 by subjects using group by and having
keyword.

select subject,sum(age) as total_age from student103 group by subject having sum(age)>25;


EXPERIMENT 5
1. Create a student table with attributes, first name, last name, address, roll no, marks.

CREATE TABLE student (

first_name VARCHAR(50),

last_name VARCHAR(50),

address VARCHAR(100),

roll_no INT,

marks INT

);

2. Insert 5 rows.

INSERT INTO student (first_name, last_name, address, roll_no, marks)

VALUES ('Alice', 'Johnson', '123 Main St', 1, 85),

('Bob', 'Smith', '456 Oak St', 2, 78),

('Charlie', 'Brown', '789 Elm St', 3, 92),

('David', 'Lee', '101 Pine St', 4, 65),

('Emily', 'Davis', '222 Cedar St', 5, 88);

3. Find the total number of students.

SELECT COUNT(*) AS total_students FROM student;


4. find the maximum marks of the students

SELECT MAX(marks) AS max_marks FROM student;

5. Find minimum marks obtained by the students.

SELECT MIN(marks) AS min_marks FROM student;

6. Find the average marks of the students.

SELECT AVG(marks) AS avg_marks FROM student;

7. Find total of the marks obtained by all student.

SELECT SUM(marks) AS total_marks FROM student;

8. List the students according to their marks in ascending order.

SELECT * FROM student ORDER BY marks ASC;

9. Arrange the student name and roll no and marks according to their marks in descending
order.

SELECT first_name, last_name, roll_no, marks FROM student ORDER BY marks DESC;
EXPERIMENT 6
1. Create a table with roll number, name, address, phone and age given as below:

ROLL_NO NAME ADDRESS PHONE AGE


1 Harsh Delhi 1234567890 18
2 Pratik Bihar 2134567890 19
3 Riyanka Siliguri 3124567890 20
4 Deep Ramnagar 4123567890 18
5 Saptarhi Kolkata 5123467890 19
6 Dhanraj Barabajar 6123457890 20
7 Rohit Balurghat 7123456890 18
8 Niraj Alipur 8123456790 19

create table student2004(

roll_no number,

name varchar(20),

address varchar(20),

phone number,

age number

);

2. Insert 8 rows as per the above data.

insert into student2004(roll_no,name,address,phone,age)

values(1,'Harsh','Delhi',1234567890,18),

(2,'Pratik','Bihar',2134567890,19),

(3,'Riyanka','Siliguri',3124567890,20),

(4,'Deep','Ramnagar',4123567890,18),

(5,'Saptarhi','Kolkata',5123467890,19),

(6,'Dhanraj','Barabajar',6123457890,20),

(7,'Rohit','Balurghat',7123456890,18),

(8,'Niraj','Alipur',8123456790,19);
3. Create a table with student course id and roll number given as below:

COURSE_ID ROLL_NO
1 1
2 2
2 3
3 4
1 5
4 9
5 10
4 11

create table studentcourse2004 (

course_id number,

roll_no number

);

4. Insert 8 rows as per the above data.

insert into studentcourse2004(course_id,roll_no)

values(1,1),

(2,2),

(2,3),

(3,4),

(1,5),

(4,9),

(5,10),

(4,11);
5. Using a inner join clause, generate the table course id, name and age from the two tables.

select studentcourse2004.course_id, student2004.name, student2004.age

from student2004

inner join studentcourse2004 on student2004.roll_no = studentcourse2004.roll_no;

6. Generate using SQL left join having value name and course id.

select student2004.name, studentcourse2004.course_id

from student2004

left join studentcourse2004 on studentcourse2004.roll_no = student2004.roll_no;

7. Generate using SQL right join having value name and course id.

select student2004.name, studentcourse2004.course_id

from student2004

right join studentcourse2004 on studentcourse2004.roll_no = student2004.roll_no;


8. Generate the full join with name and course id.

select student2004.name, studentcourse2004.course_id

from student2004

full join studentcourse2004 on studentcourse2004.roll_no = student2004.roll_no;


EXPERIMENT 7
1. Create a table with employee having its employee name, employee location, employee
department name, department number, project name, project number, project location.

CREATE TABLE employee (

emp_name VARCHAR(50),

emp_loc VARCHAR(50),

dept_name VARCHAR(50),

dept_no INT,

pro_name VARCHAR(50),

pro_no INT,

pro_loc VARCHAR(50)

);

2. Insert six rows.

INSERT INTO employee (emp_name, emp_loc, dept_name, dept_no, pro_name, pro_no, pro_loc)

VALUES ('Alice', 'Kolkata', 'HR', 1, 'Project A', 100, 'Mumbai'),

('Bob', 'Mumbai', 'IT', 2, 'Project B', 200, 'Delhi'),

('Charlie', 'Delhi', 'Finance', 3, 'Project C', 300, 'Chennai'),

('David', 'Chennai', 'Marketing', 4, 'Project D', 400, 'Kolkata'),

('Emily', 'Kolkata', 'HR', 1, 'Project E', 500, 'Mumbai'),

('Frank', 'Bangalore', 'Operations', 5, 'Project F', 600, 'Bangalore');

3. Find the 1st name of the employee who belongs to Kolkata.

SELECT emp_name FROM employee WHERE emp_loc = 'Kolkata' LIMIT 1;


4. Find the department name whose location is Mumbai.

SELECT dept_name FROM employee

WHERE pro_loc = 'Mumbai' LIMIT 1;

5. Find the project name and project location for the project number is 100.

SELECT pro_name, pro_loc FROM employee

WHERE pro_no = 100;

6. Find the project number which is executed by department number is 5.

SELECT pro_no FROM employee WHERE dept_no = 5;


EXPERIMENT 8
1. Create a table named customer with id, age, address, and salary. Make id as primary key
and make every other field not null.

CREATE TABLE customer (

id INT PRIMARY KEY,

age INT NOT NULL,

address VARCHAR(100) NOT NULL,

salary INT NOT NULL

);

2. Insert 10 rows with salary ranging from 50000 to 75000.

INSERT INTO customer (id, age, address, salary)

VALUES (1, 20, 'Kolkata', 55000),

(2, 25, 'Delhi', 60000),

(3, 30, 'Mumbai', 65000),

(4, 35, 'Chennai', 70000),

(5, 40, 'Bangalore', 75000),

(6, 22, 'Hyderabad', 52000),

(7, 28, 'Pune', 58000),

(8, 32, 'Ahmedabad', 62000),

(9, 38, 'Jaipur', 68000),

(10, 42, 'Lucknow', 72000);


3. Find all the customers whose salary is greater than 55000.

SELECT * FROM customer

WHERE salary > 55000;

4. Update salary by 0.25 times for all customers whose age is greater than or equal to 25.

UPDATE customer

SET salary = salary + (salary * 0.25)

WHERE age >= 25;

SELECT * FROM customer;

5. Delete records from customers whose age is greater than or equal to 35.

DELETE FROM customer WHERE age >= 35;

SELECT * FROM customer;

6. Select id and salary from customer table where age is same as customer with id 2.

SELECT id, salary FROM customer WHERE age = (SELECT age FROM customer WHERE id = 2);
7. Select id and salary from customer table where salary is equal to the minimum salary.

SELECT id, salary FROM customer

WHERE salary = (SELECT MIN(salary) FROM customer);

8. Select details from customers table where salary is equal to the average salary.

SELECT * FROM customer

WHERE salary = (SELECT AVG(salary) FROM customer);

9. Select details of customers where salary is maximum.

SELECT * FROM customer

WHERE salary = (SELECT MAX(salary) FROM customer);

You might also like