0% found this document useful (0 votes)
44 views37 pages

DBMS Lab Report

DBMS lab report for 2nd year 2nd semester

Uploaded by

Taiyeb Reza
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)
44 views37 pages

DBMS Lab Report

DBMS lab report for 2nd year 2nd semester

Uploaded by

Taiyeb Reza
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/ 37

Index

Part-
1
Sl Topic Page
01. University Database

02. University database Schema

03. University database E-R diagram

04. University database Schema diagram

Part-2
Sl Name of experiment
01. Write SQL queries using integrity constrains to create tables
for a database.
02. Write SQL queries to insert values into tables in the
university database.
03 Write SQL query using insert and delete, drop table,
alter table command.
04 Write a query for searching an attribute.

05 Write queries by implementing the ‘distinct’ and ‘all’ keyword.

06 Write queries using arithmetic, logical and relational operator.

07 Write queries on multiple relation and the using of


‘natural join’ keyword.
08 Write queries using renaming (‘as’ clause) operation.

09 Write queries using String operation, attribute specification


and ‘order by’ clause.
10 Write queries using ‘between’ keyword and comparison
operation
11 Write queries using Set operation.

12 Write queries using Aggregate function

13 Write queries using set membership, set comparison and


test for empty relationship.
14 Write queries to delete, insert and update data into
relationship
15 Write queries using different types of join.
University Database
A Database Management System (DBMS) is a tool that organizes and
accesses interconnected data through a set of programs. The University
Database consolidates essential information about departments, courses,
instructors, students, classrooms, and more. User requirements guide its
organization, emphasizing key features:

- Departments are uniquely identified by dept_name, situated in specific


buildings, each with an allocated budget.
- Courses within departments have course_id, title, dept_name, credits,
and optional prerequisites.
- Instructors possess unique IDs, names, associated departments
(dept_name), and salaries.
- Students, identified by unique IDs, have names, associated major
departments (dept_name), and total earned credit hours (tot_cred).
- Classrooms are specified by building name, room_number, and
time_slot_id (class meeting time).
- Classes (sections) are identified by course_id, sec_id, year, and
semester, associated with a building, room_number, and time_slot_id.
- Teaching assignments detail instructors' sections within departments.
- Student course registrations record courses and associated sections.

The University Database example includes a schema, Entity-Relationship


(E-R) diagram, SQL data definition, and inserted values, providing a
comprehensive framework for managing and accessing real-life university
data.

Full Schema

classroom(building, room_number, capacity)


department(dept_name, building, budget)
course(course_id, title, dept name, credits)
instructor(ID, name, dept_name, salary)
section(course_id, sec_id, semester, year,
building, room_number,
time_slot_id)
teaches(ID, course_id, sec_id, semester, year)
student(ID, name, dept_name, tot_cred)
takes(ID, course_id, sec_id, semester, year,

grade) Fig: Schema for a university Database.


ER diagram

Fig: E-R diagram for a university


Schema Diagram for University Database:

Fig: Schema diagram for a university enterprise

Experiment No: 01
Experiment Name: Write SQL queries using integrity constraints to create
tables for a database.

Introduction:
Data integrity is the cornerstone of a reliable database. To ensure
consistency and enforce business rules, we turn to integrity constraints,
acting as watchful guardians ensuring data's accuracy and validity. This
exploration dives into writing SQL queries incorporating such constraints
to create robust and reliable tables, laying the groundwork for a healthy
database.
Program Code:
create table
classroom( buildin
g varchar (15),
room_number varchar (7),
capacity numeric (4,0),
primary key (building, room_number));

create table
department( dept_na
me varchar (20),
building varchar (15),
budget numeric (12,2),
primary key
(dept_name));

create table
course( course_id
varchar (7),
title varchar (50),
dept_name varchar (20),
credits numeric (2,0),
primary key
(course_id));

create table
instructor( ID
varchar (5),
name varchar (20) not
null, dept_name
varchar (20),
salary numeric (8,2),
primary key (ID));

create table
section( course_id
varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
building varchar (15),
room_number varchar (7),
time_slot_id varchar (4),
primary key (course_id, sec_id, semester, year1));

create table
teaches( ID
varchar (5),
course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
primary key (ID, course_id, sec_id, semester, year1));
create table student(
ID varchar (5),
name varchar (20),
dept_name varchar (20),
tot_cred numeric (3,0),
primary key (ID));

create table
takes( ID
varchar (5),
course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
grade varchar (2),
primary key (ID, course_id, sec_id, semester, year1));

create table advisor(


s_ID varchar
(5),
i_ID varchar (5),
primary key
(s_ID));

create table
prereq( course_id
varchar(8),
prereq_id varchar(8),
primary key (course_id, prereq_id));

create table
timeslot( time_slot_id
varchar (4),
day varchar (1),
start_hr numeric (2),
start_min numeric (2),
end_hr numeric (2),
end_min numeric (2),
primary key (time_slot_id, day, start_hr, start_min));

Output:
Integrity constraints are used to create tables for university database.

Discussion:
We have successfully used integrity constraints and created the
necessary tables using SQL with the help of XAMPP software. The create
table keyword is used to create the tables or relations required for the
university database. The integrity constraints specifiy the attributes value.
Experiment No: 02
Experiment Name: Write SQL queries to insert values into tables in the
university database.

Introduction:
One of the most common operations in SQL is inserting data into tables.
We can use the INSERT INTO statement to add new records to a table. We
can specify the column names and the values to be inserted, or we can
omit the column names if we are inserting values for all the columns in
the same order as they are defined in the table.We can also insert
multiple records in one statement by using a comma-separated list of
values, or we can insert data from another table or a subquery by using
the INSERT INTO SELECT statement.

Program Code:
INSERT INTO classroom (building, room_number, capacity)
VALUES
('Packard', 101, 500),
('Painter', 514, 10),
('Taylor', 3128, 70),
('Watson', 100, 30),
('Watson', 120, 50);

INSERT INTO department


VALUES("Biology","Watson",90000); INSERT INTO
department VALUES("Comp.Sci.","Taylor",100000); INSERT
INTO department VALUES("Elec.Eng.","Taylor",85000);
INSERT INTO department
VALUES("Finance","Painter",120000); INSERT INTO
department VALUES("History","Painter",50000); INSERT
INTO department VALUES("Music","Packrad",80000);
INSERT INTO department
VALUES("Physics","Watson",70000);

INSERT INTO course VALUES("BIO-101","Intro. to


Biology","Biology",4); INSERT INTO course VALUES("BIO-
301","Genetics","Biology",4);
INSERT INTO course VALUES("BIO-399","Computational
Biology","Biology",3); INSERT INTO course VALUES("CS-101","Intro. to
Computer Science", "Comp. Sci.",4);
INSERT INTO course VALUES("CS-109","Game Design","Comp.
Sci.", 4); INSERT INTO course VALUES("CS-315","Robotics",
"Comp. Sci.", 3);
INSERT INTO course VALUES("CS-319","Image Processing", "Comp. Sci.",
3); INSERT INTO course VALUES("CS-347","Databse System Concepts",
"Comp. Sci.", 3);
INSERT INTO course VALUES("EE-181","Intro. to Digital Systems","Elec.
Eng",3); INSERT INTO course VALUES("FIN-201","Investment
Banking","Finance", 3); INSERT INTO course VALUES("HIS-351","World
History","History", 3);
INSERT INTO course VALUES("MU-199","Music Video Production", "Music",
3); INSERT INTO course VALUES("PHY-101","Physical Principles", "Physics",
4);

INSERT INTO instructor VALUES(10101,"Srinivasan","Comp.


Sci.",65000); INSERT INTO instructor VALUES(45565,"Katz", "Comp.
Sci.", 75000); INSERT INTO instructor VALUES(83821,"Brandt",
"Comp. Sci.",92000); INSERT INTO instructor
VALUES(12121,"Wu","Finance",90000);
INSERT INTO instructor VALUES(76543,"Singh","Finance",80000);
INSERT INTO instructor VALUES(15151,"Mozart","Music",40000);
INSERT INTO instructor
VALUES(22222,"Einstein","Physics",95000); INSERT INTO
instructor VALUES(33456,"Gold","Physics",87000); INSERT INTO
instructor VALUES(32343,"El Said","History",60000); INSERT
INTO instructor VALUES(58583,"Califieri","History",62000);
INSERT INTO instructor VALUES(76766,"Crick","Biology",72000);
INSERT INTO instructor VALUES(98345,"Kin","Elec. Eng",80000);

INSERT INTO section VALUES ("BIO-101", 1, "Summer", 2017,


"Painter",514, "B"); INSERT INTO section VALUES ("BIO-301", 1,
"Summer", 2018, "Painter",514, "A"); INSERT INTO section VALUES ("CS-
101", 1, "Fall", 2017, "Packard", 101, "H"); INSERT INTO section VALUES
("CS-101", 1, "Spring", 2018, "Packard", 101, "F"); INSERT INTO section
VALUES ("CS-190", 1, "Spring", 2017, "Taylor", 3128, "E"); INSERT INTO
section VALUES ("CS-190", 2, "Spring", 2017, "Taylor", 3128, "A"); INSERT
INTO section VALUES ("CS-315", 1, "Spring", 2018, "Watson", 120, "D");
INSERT INTO section VALUES ("CS-319", 1, "Spring", 2018, "Watson", 100,
"B"); INSERT INTO section VALUES ("CS-319", 2, "Spring", 2018, "Taylor",
3128, "C"); INSERT INTO section VALUES ("CS-347", 1, "Fall", 2017,
"Taylor", 3128, "A"); INSERT INTO section VALUES ("EE-181", 1, "Spring",
2017, "Taylor", 3128, "C"); INSERT INTO section VALUES ("FIN-201", 1,
"Spring", 2018, "Packard",101, "B"); INSERT INTO section VALUES ("HIS-
351", 1, "Spring", 2018, "Painter",514, "C"); INSERT INTO section VALUES
("MU-199", 1, "Spring", 2018, "Packard", 101, "D"); INSERT INTO section
VALUES ("PHY-101", 1, "Fall", 2017, "Watson", 100, "A");

INSERT INTO teaches (ID, course_id, sec_id, semester,


year1) VALUES
(10101, 'CS-101', 1, 'Fall', 2017),
(10101, 'CS-315', 1, 'Spring', 2018),
(10101, 'CS-347', 1, 'Fall', 2017),
(12121, 'FIN-201', 1, 'Spring', 2018),
(15151, 'MU-199', 1, 'Spring', 2018),
(22222, 'PHY-101', 1, 'Fall', 2017),
(32343, 'HIS-351', 1, 'Spring', 2018),
(45565, 'CS-101', 1, 'Spring', 2018),
(45565, 'CS-319', 1, 'Spring', 2018),
(76766, 'BIO-101', 1, 'Summer', 2017),
(76766, 'BIO-301', 1, 'Summer', 2018),
(83821, 'CS-190', 1, 'Spring', 2017),
(83821, 'CS-190', 2, 'Spring', 2017),
(83821, 'CS-319', 2, 'Spring', 2018),
(98345, 'EE-181', 1, 'Spring', 2017);

INSERT INTO student (ID, name, dept_name,


tot_cred) VALUES
('00128', 'Zhang', 'Comp. Sci.', 102),
('12345', 'Shankar', 'Comp. Sci.', 32),
('19991', 'Brandt', 'History', 80),
('23121', 'Chavez', 'Finance', 110),
('44553', 'Peltier', 'Physics', 56),
('45678', 'Levy', 'Physics', 46),
('54321', 'Williams', 'Comp. Sci.', 54),
('55739', 'Sanchez', 'Music', 38),
('70557', 'Snow', 'Physics', 0),
('76543', 'Brown', 'Comp. Sci.', 58),
('76653', 'Aoi', 'Elec. Eng.', 60),
('98765', 'Bourikas', 'Elec. Eng.', 98),
('98988', 'Tanaka', 'Biology', 120);

INSERT INTO takes (ID, course_id, sec_id, semester, year1,


grade) VALUES
('00128', 'CS-101', 1, 'Fall', 2017, 'A'),
('00128', 'CS-347', 1, 'Fall', 2017, 'A-'),
('12345', 'CS-101', 1, 'Fall', 2017, 'C'),
('12345', 'CS-190', 2, 'Spring', 2017, 'A'),
('12345', 'CS-315', 1, 'Spring', 2018, 'A'),
('12345', 'CS-347', 1, 'Fall', 2017, 'A'),
('19991', 'HIS-351', 1, 'Spring', 2018, 'B'),
('23121', 'FIN-201', 1, 'Spring', 2018, 'C+'),
('44553', 'PHY-101', 1, 'Fall', 2017, 'B-'),
('45678', 'CS-101', 1, 'Fall', 2017, 'F'),
('45678', 'CS-101', 1, 'Fall', 2018, 'B+'),
('45678', 'CS-319', 1, 'Spring', 2018, 'B'),
('54321', 'CS-101', 1, 'Fall', 2017, 'A-'),
('54321', 'CS-190', 2, 'Spring', 2017, 'B+'),
('55739', 'MU-199', 1, 'Spring', 2018, 'A-'),
('76543', 'CS-101', 1, 'Fall', 2017, 'A'),
('76543', 'CS-319', 2, 'Spring', 2018, 'A-'),
('76653', 'EE-181', 1, 'Spring', 2017, 'C'),
('98765', 'CS-101', 1, 'Fall', 2017, 'C-'),
('98765', 'CS-315', 1, 'Spring', 2018, 'B'),
('98988', 'BIO-101', 1, 'Summer', 2017, 'A'),
('98988', 'BIO-301', 1, 'Summer', 2018, null);

INSERT INTO advisor (s_id,


i_id) VALUES
('00128', '45565'),
('12345', '10101'),
('23121', '76543'),
('44553', '22222'),
('45678', '22222'),
('76543', '45565'),
('76653', '98345'),
('98765', '98345'),
('98988', '76766');

INSERT INTO prereq (course_id,


prereq_id) VALUES
('BIO-301', 'BIO-101'),
('BIO-399', 'BIO-101'),
('CS-190', 'CS-101'),
('CS-315', 'CS-101'),
('CS-319', 'CS-101'),
('CS-347', 'CS-101'),
('EE-181', 'PHY-101');

INSERT INTO timeslot (time_slot_id, day, start_hr, start_min, end_hr,


end_min) VALUES
('A', 'M', 8, 0, 8, 50),
('A', 'W', 8, 0, 8, 50),
('A', 'F', 8, 0, 8, 50),
('B', 'M', 9, 0, 9, 50),
('B', 'W', 9, 0, 9, 50),
('B', 'F', 9, 0, 9, 50),
('C', 'M', 11, 0, 11, 50),
('C', 'W', 11, 0, 11, 50),
('C', 'F', 11, 0, 11, 50),
('D', 'M', 13, 0, 13, 50),
('D', 'W', 13, 0, 13, 50),
('D', 'F', 13, 0, 13, 50),
('E', 'T', 10, 30, 11, 45),
('E', 'R', 10, 30, 11, 45),
('F', 'T', 14, 30, 15, 45),
('F', 'R', 14, 30, 15, 45),
('G', 'M', 16, 0, 16, 50),
('G', 'W', 16, 0, 16, 50),
('G', 'F', 16, 0, 16, 50),
('H', 'W', 10, 0, 12, 30);

Output:
Classroom:

Instructor:

Section:
Discussion: We have successfully insert values into tables using SQL with
the help of XAMPP software. Here we apply SQL query for insert the value
of the attribute of the table of student database. For this we use INSERT
INTO key ward and write the table name then we use VALUES key ward to
give the value of the field of the table.

Experiment No: 03
Experiment Name: Write SQL query using insert and delete, drop table,
alter table command.

Introduction:

The insert command is used to add new records to a table. We can specify
the column names and the values to be inserted, or we can omit the
column names if we are inserting values for all the columns in the same
order as they are defined in the table.The delete command is used to
remove existing records from a table.We can specify a condition to
delete only the records that match the condition, or we can omit the
condition to delete all the records from the table.The drop table command
is used to delete a table and all its data from the database. We can
specify the name of the table to be deleted. Be careful before using this
command, as it will result in loss of all information stored in the table.The
alter table command is used to add, modify, or delete columns or
constraints in an existing table.We can specify the name of the table and
the column or constraint to be added, modified, or deleted. We can also
use multiple alter table commands in one statement by separating them
with commas.

Program Code:

INSERT INTO instructor VALUES(10211, 'Smith', 'Biology', 66000);


DELETE FROM student;
DROP TABLE teaches;
ALTER TABLE student ADD Height NUMERIC
(2,0); ALTER TABLE student DROP height;
Output:

1 row inserted. (Query took 0.0066 seconds.)


INSERT INTO instructor VALUES(10211, 'Smith', 'Biology',
66000);

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010
seconds.)
DROP TABLE teaches;
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0009
seconds.)
ALTER TABLE student ADD Height NUMERIC (2,0);
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010
seconds.)
ALTER TABLE student DROP height;

Discussion: We have successfully used the insert and delete, drop table,
alter table command using SQL with the help of XAMPP software. Here we
apply SQL query for insert the value, delete the of the attribute of the
table of student database. For this we use INSERT INTO keyword and write
the table name then we use VALUES keyword to give the value of the
field of the table. We use DELETE keyword to delete all the record of the
table. We used DROP keyword to dropped the table from the database.

Experiment No: 04
Experiment Name:
Write a query for searching an attribute.

Introduction:
Compose and execute a structured query (SELECT attribute FROM table;)
to search and retrieve specific attributes from designated table,
contributing to the exploration and analysis of relational database data.

Program
Code: SELECT
name FROM
student;

SELECT
dept_name FROM
instructor;

Output:
Discussion:
The first query fetches names from the student table, and the second
retrieves department names from the instructor table, aiding in data
exploration and analysis.
Experiment No: 05
Experiment Name:
Write queries by implementing the ‘distinct’ and ‘all’ord.

Introduction:
In this context, the focus is on crafting SQL queries with the integration of
the 'DISTINCT' and 'ALL' keywords. These keywords play a crucial role in
fine-tuning query results. 'DISTINCT' ensures unique values, while 'ALL'
includes all values, offering versatility in data retrieval and analysis.

Program Code:
SELECT DISTINCT dept_name
FROM instructor;

SELECT ALL dept_name


FROM instructor;

Output:

Discussion:
Successfully employed SQL queries with 'DISTINCT' and 'ALL' keywords
using XAMPP. 'DISTINCT' ensured unique values, eliminating duplicates for
clarity. 'ALL' facilitated comprehensive data retrieval, showcasing the
versatility of these keywords. This implementation on XAMPP underscores
enhanced query precision and adaptability in tailoring results.

Experiment No: 06
Experiment Name:
Write queries using arithmetic, logical and relational operator.

Instruction:
This exploration delves into crafting SQL queries by leveraging the power
of arithmetic, logical, and relational operators. By incorporating these
operators, users can perform calculations, create logical conditions, and
establish relationships between data, enabling precise and dynamic data
retrieval from relational databases.
Program Code:
SELECT ID, name, dept_name, salary *
1.1 FROM instructor;

SELECT name
FROM
instructor
WHERE dept_name = 'Comp. Sci.' AND salary > 70000;

SELECT name, instructor.dept_name,


building FROM instructor, department
WHERE instructor.dept_name = department.dept_name;

SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID AND instructor.dept_name = 'Comp. Sci.';

Output:
Discussion: We have successfully use arithmetic, logical and relational
operator using SQL with the help of XAMPP software.

Experiment No: 07

Experiment Name:
Write queries on multiple relation and the using of ‘natural join’ keyword..

Introduction:
Compose queries for multiple relations by employing the 'natural join'
keyword to seamlessly combine tables with matching columns. Specify
common attributes to link tables effortlessly, simplifying complex
database retrievals. Enhance data retrieval efficiency by leveraging the
inherent relationships between tables through the natural join operation
in your SQL queries.

Program Code:
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID;

SELECT name, course_id FROM instructor NATURAL JOIN teaches;

SELECT name, title FROM instructor


NATURAL JOIN teaches, course
WHERE teaches.course_id = course.course_id;

SELECT name, title


FROM ( instructor NATURAL JOIN teaches) JOIN course USING(course_id);

Output:
Discussion: We've effectively harnessed the power of SQL and XAMPP
software to implement 'natural join' in multiple relations. NATURAL JOIN
streamlines data integration by focusing on shared attributes in the
schemas of related tables. This synergy enhances precision in tuple
pairing, optimizing our database queries for seamless and efficient data
retrieval.

Experiment No: 08
Experiment Name: Write queries using renaming (‘as’ clause) operation.

Introduction: The 'AS' clause in SQL enables the renaming of columns or


tables within queries, enhancing readability and providing a more intuitive
representation of data. This feature allows users to customize aliases for
tables and fields, making complex queries more concise and
comprehensible.

Program Code:
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID;

SELECT name AS instructor_name,


course_id FROM instructor, teaches
WHERE instructor.ID = teaches.ID;

SELECT T.name, S.course_id


FROM instructor AS T, teaches AS S WHERE T.ID = S.ID;

SELECT DISTINCT T.name FROM instructor AS T, instructor AS S


WHERE T.salary > S.salary AND S.dept_name = 'Biology';
Output:
Discussion: We have effectively utilized the 'AS' clause in SQL, facilitated
by XAMPP software. This SQL feature allows for the renaming of both
database field names and entire tables. By employing the 'AS' clause, we
achieved enhanced query clarity and structure, demonstrating its
versatility in customizing field and table names for improved readability
and understanding.

Experiment No:09
Experiment Name:
Write queries using String operation, attribute specification and ‘order by’ clause.

Instruction:Compose SQL queries incorporating String operations and


attribute specification, optimizing results with the 'ORDER BY' clause.
Utilize functions like CONCAT or SUBSTRING for string manipulations.
Specify desired attributes using SELECT, and enhance result organization
by sorting with 'ORDER BY' based on chosen columns, ensuring tailored
and orderly query outcomes.

Program Code:
SELECT dept_name FROM department
WHERE building LIKE '%Watson%';

SELECT instructor.*
FROM instructor,
teaches
WHERE instructor.ID = teaches.ID;

SELECT name
FROM
instructor
WHERE dept_name =
'Physics' ORDER BY name;
SELECT *
FROM instructor
ORDER BY salary DESC, name ASC;

Output:
Discussion:
We adeptly employed SQL's String operations, attribute specification, and
'ORDER BY' clause via XAMPP, optimizing queries. Leveraging functions
like CONCAT, we manipulated strings, specified attributes, and organized
results for enhanced data presentation, showcasing the versatility of SQL
in conjunction with XAMPP.

Experiment No:10
Experiment Name:Write queries using ‘between’ keyword and comparison
operation.

Instruction:
Leverage SQL's 'BETWEEN' keyword and comparison operations to refine
queries. This dynamic duo enables precise data retrieval by specifying a
range of values or conditions, ensuring tailored results that meet specific
criteria within a defined parameter range.

Program Code:
SELECT name
FROM
instructor
WHERE salary BETWEEN 90000 AND 100000;

SELECT name FROM instructor


WHERE salary <= 100000 AND salary >= 90000;

SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID AND dept_name = 'Biology';

SELECT name,
course_id FROM
instructor, teaches
WHERE (instructor.ID, dept_name) =(teaches.ID, 'Biology');
Output:

Discussion:
We've seamlessly harnessed the power of SQL's 'BETWEEN' keyword and
comparison operations through XAMPP, refining data retrieval. This
dynamic combination allows us to specify ranges and conditions, ensuring
precise results tailored to our criteria. The synergy of SQL and XAMPP
enhances our ability to extract relevant information efficiently.

Experiment No:
11 Experiment
Name:
Write queries using Set operation.

Introduction:
Compose SQL queries employing set operations, such as UNION or
INTERSECT, to combine or compare data from multiple tables. For
instance, use SELECT column FROM table1 UNION SELECT column FROM
table2; to merge distinct values.

Program Code:
SELECT
course_id FROM
section
WHERE semester = 'Fall' AND YEAR = 2017;

SELECT
course_id FROM
section
WHERE semester = 'Spring' AND year = 2018;

Union operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year=
2017) UNION
(SELECT
course_id FROM
section
WHERE semester = "Spring" AND year= 2018);
Intersect operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year= 2009)
INTERSECT
(SELECT
course_id FROM
section
WHERE semester = "Spring" AND year= 2010);

Except operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year=
2009) EXCEPT
(SELECT course_id
FROM section WHERE semester = "Spring" AND year= 2010);

Output:

Discussion:
Set operations in SQL enhance data manipulation. The provided query
combines unique values from 'table1' and 'table2,' showcasing how set
operations enable powerful data synthesis or comparison, crucial for
diverse analytical tasks in a laboratory environment.

Experiment No:12
Experiment Name:Write queries using Aggregate function.

Introduction:
Aggregate functions in SQL empower users to extract valuable insights
from data by performing operations like sum, average, count, and more.
This set of queries focuses on harnessing the power of aggregate
functions to analyze and summarize information, providing a
comprehensive view of the data stored in a relational database.

Program Code:
select avg (salary) from
instructor where dept_name=
"Comp. Sci.";

select avg (salary) as avg_salary from


instructor where dept_name= "Comp. Sci.";

select count(*) from course;

select dept_name, avg (salary) as avg_salary from instructor group by

dept_name; select dept_name, count(distinct ID) as instr_count


from instructor natural join teaches
where semester = "Spring" and year = 2010 group by dept_name;

select dept_name, avg (salary) as avg_salary


from instructor group by dept_name having avg (salary) > 42000;

select course_id, semester, year, sec_id, avg (tot_cred)


from takes natural join student where year = 2009 group by
course_id, semester, year, sec_id having count(ID) >= 2;

Output:

Discussion: We have successfully use Aggregate function. using SQL with


the help of XAMPP software.
Experiment No:13
Experiment Name:Write queries using set membership, set comparison and
test for empty relationship.

Introduction:
This experiment aims to develop SQL queries to implement set
membership, set comparison, and test for empty relationships. By
exploring these operations, participants will gain a deeper understanding
of how SQL handles set-related tasks, enabling them to efficiently manage
and analyze data within relational databases.

Program Code:
select distinct course_id from section
where semester = "Fall" and year= 2017 and course_id
in (select course_id from section where semester = "Spring" and year= 2018);

select distinct course_id from section


where semester = "Fall" and year= 2017 and course_id
not in (select course_id from section where semester = "Spring" and

year= 2018); select distinct name from instructor where name not in

("Mozart", "Einstein");

select distinct T.name from instructor as T, instructor


as S where T.salary > S.salary and S.dept_name =
"Biology";

select name from instructor


where salary > some (select salary from instructor where dept_name = "Biology");

select name from instructor


where salary > all (select salary from instructor where dept_name = "Biology");

select dept_name
from instructor group by dept_name
having avg (salary) >= all (select avg (salary) from instructor group by dept_name);

select course_id from section as S


where semester = "Fall" and year=
2017
and exists (select * from section as T where semester = "Spring" and
year= 2018 and S.course_id= T.course_id);

select distinct S.ID, S.name from student as S


where not exists ((select course_id from course where dept_name =
"Biology") except (select T.course_id from takes as T where S.ID =
T.ID));
Output:

Discussion:
The successful utilization of set membership, set comparison, and testing
for empty relationships in SQL, facilitated by XAMPP software, underscores
the versatility of these operations in database management.

Experiment No:14
Experiment Name: Write queries to delete, insert and update data into relationship.

Introduction:
The primary goal of this experiment is to delve into the practical aspects
of database management, focusing on the crucial operations of deleting,
inserting, and updating data within a relational database. Through these
actions, participants will gain valuable insights into the dynamic nature of
database manipulation and the nuances of executing SQL commands
effectively.

Program Code:
delete from instructor
where dept_name=
"Finance"; delete from
instructor
where salary between 13000 and 15000;

delete from instructor


where dept_name in (select dept_name from department where
building = "Watson");
delete from instructor
where salary< (select avg (salary) from instructor);

insert into course values ("CS-437", "Database Systems", "Comp.

Sci.", 4); insert into instructor


select ID, name, dept_name, 18000 from
student where dept_name = "Music" and
tot_cred > 144;

insert into student values ("3003", "Green", "Finance",


null); update instructor set salary= salary * 1.05; 49

update instructor set salary = salary *


1.05 where salary < 70000;

update instructor set salary = salary * 1.05


where salary < (select avg (salary) from instructor);

update instructor set salary = salary * 1.03 where salary > 100000;
update instructor set salary = salary * 1.05 where salary <= 100000;

Output:

2 rows deleted. (Query took 0.0264 seconds.)


delete from instructor where dept_name= "Finance";

0 rows deleted. (Query took 0.0016 seconds.)


delete from instructor where salary between 13000 and 15000;

4 rows deleted. (Query took 0.0017 seconds.)

delete from instructor where dept_name in (select dept_name from


department where building = "Watson");

4 rows deleted. (Query took 0.0016 seconds.)

delete from instructor where salary< (select avg (salary) from


instructor);

1 row inserted. (Query took 0.0010 seconds.)


insert into course values ("CS-437", "Database Systems", "Comp. Sci.", 4);

0 rows inserted. (Query took 0.0009 seconds.)

insert into instructor select ID, name, dept_name, 18000 from student
where dept_name = "Music" and tot_cred > 144;

1 row inserted. (Query took 0.0010 seconds.)

insert into student values ("3003", "Green", "Finance", null);

3 rows affected. (Query took 0.0074 seconds.)

update instructor set salary= salary * 1.05;

0 rows affected. (Query took 0.0011 seconds.)

update instructor set salary = salary * 1.05 where salary < 70000;

2 rows affected. (Query took 0.0016 seconds.)

update instructor set salary = salary * 1.05 where salary < (select
avg (salary) from instructor);

0 rows affected. (Query took 0.0020 seconds.)

update instructor set salary = salary * 1.03 where salary > 100000;

3 rows affected. (Query took 0.0017 seconds.)

update instructor set salary = salary * 1.05 where salary <= 100000;

Discussion:
We've successfully executed operations to delete, insert, and update data
within a relational database using SQL through XAMPP. Leveraging
commands like INSERT,
DELETE, and UPDATE, we manipulated data effectively. For insertion, we
utilized the FROM clause, employed DELETE with WHERE conditions, and
updated records using the UPDATE statement with specified conditions for
precision.

Experiment No:15

Experiment Name: Write queries using different types of join.

Introduction:
In this experiment, the focus is on mastering the art of SQL queries
through the exploration of different join types. By delving into INNER,
LEFT, RIGHT, and FULL joins, participants aim to enhance their skills in
crafting precise and versatile queries, gaining a comprehensive
understanding of how to retrieve and combine data effectively from
diverse tables in a relational database.

Program Code:
Select * from student join takes on student.ID=
takes.ID; Select * from student, takes where
student.ID = takes.ID; Select * from student natural
left outer join takes;
Select ID from student natural left outer join takes where
course_id is null; Select * from takes natural right outer join
student;
Output:
Discussion:
Successful implementation of diverse SQL joins in this experiment
underscores their pivotal role in data retrieval. By mastering INNER, LEFT,
RIGHT, and FULL joins, participants gain a nuanced understanding of
combining information from related tables. This proficiency enhances
query flexibility and precision, crucial in database management.

You might also like