DBMS LAB Manual Final22
DBMS LAB Manual Final22
LABORATORY MANUAL
Prepared By
Dr. C. Viji
Prof. KSN Sushma
Prof. Rezni
Verified By
DQAC HOD
Vision
To empower students through wholesome education and enable the students to develop into
highly qualified and trained professionals with ethics and emerge as responsible citizen with
broad outlook to build a vibrant nation.
Mission
To achieve academic excellence in science, engineering and technology through
dedication to duty, innovation in teaching and faith in human values.
To enable our students to develop into outstanding professional with high ethical
standards to face the challenges of 21st century.
To provide educational opportunities to the deprived and weaker section of the
society to uplift their socioeconomic status.
Vision
To advance the intellectual capacity of the nation and the international community by imparting
knowledge to graduates who are globally recognized as innovators, entrepreneur and competent
professionals.
Mission
To provide excellent technical knowledge and computing skills to make the graduates
globally competitive with professional ethics.
To involve in research activities and be committed to lifelong learning to make positive
contributions to the society
Programme Outcomes
PO1 Engineering Knowledge: Apply knowledge of mathematics, science, engineering
fundamentals and an engineering specialization to the solution of complex engineering
problems.
PO2 Problem Analysis: Identify, formulate, research literature and analyze
complex engineering problems reaching substantiated conclusions using firstprinciples
of mathematics, natural sciences and engineering sciences
PO3 Design/Development of Solutions: Design solutions for complex engineering problems
and design system components or processes that meet specified needswith appropriate
consideration for public health and safety, cultural, societal and environmental
considerations.
PO4 Conduct investigations of complex problems: Using research-basedknowledge and
research methods including design of experiments, analysis and interpretation of data
and synthesis of information to provide valid conclusions.
PO5 Modern Tool Usage: Create, select and apply appropriate techniques, resources and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an under- standing of the limitations.
PO6 The Engineer and Society: Apply reasoning informed by contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to professional engineering practice.
PO7 Environment and Sustainability: Understand the impact of professional engineering
solutions in societal and environmental contexts and demonstrateknowledge of and need
for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics andresponsibilities
and norms of engineering practice.
PO9 Individual and Team Work: Function effectively as an individual, and as amember
or leader in diverse teams and in multi-disciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as being able to comprehend and
write effective reports and design documentation, make effective presentations and give
and receive clear instructions.
PO11 Project Management and Finance: Demonstrate knowledge and understanding of
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
PO12 Life-long Learning: Recognize the need for and have the preparation andability to
engage in independent and life- long learning in the broadest context of technological
change.
COURSE OUTCOMES
CO1 Create, Update and query on the database.
CO2 Demonstrate the working of different concepts of DBMS
CO3 Implement, analyze and evaluate the project developed for an application.
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 2 - 3 - 3 - - - 1 - 3 1
CO2 2 1 - 3 3 - - - 3 - - - - - -
CO3 2 - 3 3 3 - - 3 3 3 1 - - - 3
CO4 - - - - - - - - - - - - - - -
CO5 - - - - - - - - - - - - - - -
Avg 2 3 3 3 3 2.33 3 2 1 3
No Correlation -
Low 1
Moderate 2
High 3
Syllabus
Course Objectives:
Design, develop, and implement the specified queries for the following problems using
Oracle, MySQL, MS SQL Server, or any other DBMS under LINUX/Windows
environment.
Create Schema and insert at least 5 records for each table. Add appropriate database
constraints.
Lab Experiments:
Part A: SQL Programming
List of Experiments
S.No. CONTENTS
PART-A: SQL Programming
1. EXPERITMENT 1 - Library Database
Introduction
Data are known facts that can be recorded and that have implicit meaning. A
Database is collection of related data. A database management system (DBMS) is a
collection of programs that enables users to create and maintain a database. The DBMS is
a general-purpose software system that facilitates the processes of defining, constructing,
manipulating, and sharing databases among various users and applications.
The main SQL command for data definition is the CREATE statement, which can
be used to create schemas, tables (relations) as well as other constructs such as views,
assertions, and triggers.
A schema is created via the CREATE SCHEMA statement, which can include all
the schema elements definitions.
Create schema company authorization ‘Jsmith’;
Data Types:
Numeric: NUMBER, NUMBER(s,p), INTEGER, INT, FLOAT, DECIMAL
Character: CHAR(n), VARCHAR(n), VARCHAR2(n), CHAR VARYING(n)
Bit String: BLOB, CLOB
Boolean: true, false, and null
Date and Time: DATE (YYYY-MM-DD) TIME( HH:MM:SS)
Timestamp: DATE + TIME
SQL Constraints:
1. NOT NULL Constraint: By default, a column can hold NULL values. The NOT
NULL constraint enforces a column to NOT accept NULL values. This enforces
a field to always contain a value, which means that you cannot insert a new
record, or update a record without adding a value to this field.
3. CHECK Constraint: The CHECK constraint is used to limit the value range
that can be placed in a column.
5. UNIQUE Constraint: The UNIQUE constraint ensures that all values in a columnare
different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically
has a UNIQUE constraint. However, you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
6. FOREIGN KEY Constraint: A FOREIGN KEY is a key used to link two tables
together. A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table. The table containing the foreign key
is called the child table, and the table containing the candidate key is called the
referenced or parent table.
Set NULL: Sets the column value to NULL when you delete the parent table row.
Set DEFAULT: sets the column value to DEFAULT when you delete the parent
table row.
CASCADE: CASCADE will propagate the change when the parent changes. If
you delete a row, rows in constrained tables that reference that row will also be
deleted, etc.
RESTRICT: RESTRICT causes you cannot delete a given parent row if a child
row exists that references the value for that parent row.
The DROP command can be used to drop named schema elements, such as tables,
domains,or constraints. One can also drop a schema. For example, if a whole
schema is no longer needed, the DROP SCHEMA command can be used. There
are two drop behavior options:
If the RESTRICT option is chosen in place of CASCADE, the schema is dropped only
if ithas no elements in it; otherwise, the DROP command will not be executed. To use
the RESTRICT option, the user must first individually drop each element in the
schema, then drop the schema itself.
Notice that the DROP TABLE command not only deletes all the records in the table if
successful, but also removes the table definition from the catalog.
The definition of a base table or of other named schema elements can be changed by using
the ALTER command. For base tables, the possible alter table actions include adding or
dropping a column (attribute), changing a column definition, and adding or dropping table
constraints.
SQL has one basic statement for retrieving information from a database: the SELECT
statement.
SELECT <attribute list>
FROM <table list>
WHERE <condition>;
where
■ <attribute list> is a list of attribute names whose values are to be retrieved by the query.
■ <table list> is a list of the relation names required to process the query.
■ <condition> is a conditional (Boolean) expression that identifies the tuples to be
retrieved by the query.
In SQL, the basic logical comparison operators for comparing attribute values with one
another and with literal constants are =, <, <=, >, >=, and <>.
Retrieve the birth date and address of the employee(s) whose nameis ‘John B. Smith’.
This query involves only the EMPLOYEE relation listed in the FROM clause. The
query selects the individual EMPLOYEE tuples that satisfy the condition of the WHERE
clause, then projects the result on the Bdate and Address attributes listed in the SELECT
clause. The SELECT clause of SQL specifies the attributes whose values are to be
retrieved, which are called the projection attributes, and the WHERE clause specifies the
boolean condition that must be true for any retrieved tuple, which is known as the
selection condition.
A query that involves only selection and join conditions plus projection attributes is known
as a select-project-join query.
Retrieve the name and address of all employees who work for the ‘Research’ department.
In the WHERE clause, the condition Dname = ‘Research’ is a selection condition that
chooses the particular tuple of interest in the DEPARTMENT table, because Dname is an
attribute of DEPARTMENT. The condition Dnumber = Dno is called a join condition,
because it combines two tuples: one from DEPARTMENT and one from EMPLOYEE,
whenever the value of Dnumber in DEPARTMENT is equal to the value of Dno in
EMPLOYEE.
For each employee, retrieve the employee’s first and last name and the first and lastname of his or her
immediate supervisor.
In this case, we are required to declare alternative relation names E and S, called aliases or
tuple variables, for the EMPLOYEE relation. An alias can follow the keyword AS, or it
can directly follow the relation name.
select Ssn
from employee;
CSE @ HKBKCE 15 2022-23
18CSL58 DBMS LAB
To retrieve all the attribute values of the selected tuples, we do not have to list the attribute
names explicitly in SQL; we just specify an asterisk (*), which stands for all the attributes.
select *
from employee
where Dno=5;
DISTINCT:
DISTINCT keyword in the SELECT clause, meaning that only distinct tuples should
remain in the result.
select distinct Salary
from employee;
Set Operations:
SQL has directly incorporated some of the set operations from mathematical set theory.
There are set union (UNION), set difference (EXCEPT) and set intersection
(INTERSECT) operations.
The relations resulting from these set operations are sets of tuples; that is, duplicate tuples
are eliminated from the result. These set operations apply only to union-compatible
relations, so we must make sure that the two relations on which we apply the operation
have the same attributes and that the attributes appear in the same order in both relations.
Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project
union
( select distinct Pnumber
from project, works_on, employee
where Pnumber=Pno and Essn=Ssn
and Lname=‘Smith’ );
The first SELECT query retrieves the projects that involve a ‘Smith’ as manager of the
department that controls the project, and the second retrieves the projects that involve a
‘Smith’ as a worker on the project. Notice that if several employees have the last name
‘Smith’, the project names involving any of them will be retrieved. Applying the UNION
operation to the two SELECT queries gives the desired result.
SQL also has corresponding multiset operations, which are followed by the keyword ALL
(UNION ALL, EXCEPT ALL, INTERSECT ALL). Their results are multisets (duplicates
are not eliminated).
The first feature allows comparison conditions on only parts of a character string, using the
LIKE comparison operator. This can be used for string pattern matching. Partial strings
are specified using two reserved characters: % replaces an arbitrary number of zero or
more characters, and the underscore (_) replaces a single character.
from employee
To retrieve all employees who were born during the 1950s, ‘5’must be the third character
of the string (according to our format for date), so we use the value ‘_ _ 5 ’,
with each underscore serving as a placeholder for an arbitrary character.
from employee
Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.
select *
from employee
where (Salary BETWEEN 30000 AND 40000) and Dno = 5;
The condition (Salary BETWEEN 30000 AND 40000) is equivalent to the condition
((Salary >= 30000) AND (Salary <= 40000)).
SQL allows the user to order the tuples in the result of a query by the values of one or more
of the attributes that appear in the query result, by using the ORDER BY clause.
The default order is in ascending order of values. We can specify the keyword DESC if
we want to see the result in a descending order of values. The keyword ASC can be used
to specify ascending order explicitly.
Retrieve a list of employees and the projects they are working on, ordered by department and, within
each department, ordered alphabetically by last name, then first name.
GROUP BY
In many cases, we want to apply the aggregate functions to subgroups of tuples in a
relation. Each subgroup of tuples consists of the set of tuples that have the same value for
the grouping attribute(s). The function is applied to each subgroup independently. SQL has
a GROUP BY-clause for specifying the grouping attributes, which must also appear in the
SELECT-clause.
For each department, retrieve the department number, the number of employees inthe
department, and their average salary.
The EMPLOYEE tuples are divided into groups. Each group having the same value for the
grouping attribute DNO. The COUNT and AVG functions are applied to each such group
of tuples separately. The SELECT-clause includes only the grouping attribute and the
functions to be applied on each group of tuples. A join condition can be used in
conjunctionwith grouping.
THE HAVING-CLAUSE
Sometimes we want to retrieve the values of these functions for only those groups
that satisfy certain conditions. The HAVING-clause is used for specifying a selection
condition on groups (rather than on individual tuples)
For each project on which more than two employees work, retrieve the project number, project name, and
the number of employees who work on that project.
Nested Queries
A complete SELECT query, called a nested query, can be specified within the WHERE-
clause of another query, called the outer query.
Retrieve the name and address of all employees who work for the 'Research'department.
from employee
Note: The nested query selects the number of the 'Research' department. The outer query
selects an EMPLOYEE tuple if its DNO value is in the result of either nested query.
The comparison operator IN compares a value v with a set (or multi-set) of values V, and
evaluates to TRUE if v is one of the elements in V
Correlated Queries
Retrieve the name of each employee who has a dependent with the same first name asthe employee.
select e.fname, e.lname from employee as e where e.ssn in (select essn from dependent
where essn=e.ssn and e.fname=dependent_name)
The nested query has a different result in the outer query. A query written with nested
SELECT... FROM… WHERE... blocks and using the = or IN comparison operators can
always be expressed as a single block query.
EXISTS is used to check whether the result of a correlated nested query is empty
(contains no tuples) or not. We can formulate the above Query in an alternative form that
uses EXIST.
Note: In this correlated nested query retrieves all DEPENDENT tuples related to an
EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected
EXPLICIT SETS
SQL allows queries that check if a value is NULL (missing or undefined or not
applicable). SQL uses IS or IS NOT to compare NULLs because it considers each NULL
value distinct from other NULL values, so equality comparison is not appropriate.
Note: If a join condition is specified, tuples with NULL values for the join attributes are not
included in the result.
Aggregate functions
Find the maximum salary, the minimum salary, and the average salary among allemployees.
FROM EMPLOYEE
Note: Some SQL implementations may not allow more than one function in the SELECT-
claus
In SQL, three commands can be used to modify the database: INSERT, DELETE, and
UPDATE.
In its simplest form, INSERT is used to add a single tuple to a relation. We must specify
the relation name and a list of values for the tuple. The values should be listed in the same
order in which the corresponding attributes were specified in the CREATE TABLE
command.
values ( ‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘1962-12-30’, ’98 Oak Forest, Katy, TX’,
‘M’, 37000, ‘653298653’, 4 );
A second form of the INSERT statement allows the user to specify explicit attribute names
that correspond to the values provided in the INSERT command. This is useful if a
relation has many attributes but only a few of those attributes are assigned values in the
new tuple.
where Lname=‘Brown’;
The UPDATE command is used to modify attribute values of one or more selected
tuples. As in the DELETE command, a WHERE clause in the UPDATE command selects
the tuples to be modified from a single relation. However updating a primary key value
may propagate to the foreign key values of tuples in other relations if such a referential
triggered action is specified in the referential integrity constraints of the DDL.
For example, to change the location and controlling department number of projectnumber 10 to ‘Bellaire’
and 5, respectively.
Update project
Several tuples can be modified with a single UPDATE command. An example is to give all
employees in the ‘Research’ department a 10 percent raise in salary,
Update employee
It is also possible to specify NULL or DEFAULT as the new attribute value. Notice that
each UPDATE command explicitly refers to a single relation only. To modify multiple
relations, we must issue several UPDATE commands.
VIEWS IN SQL
A view is a single virtual table that is derived from other tables. The other
tables could be base tables or previously defined view.
Allows for limited update operations Since the table may not physically be stored
Allows full query operations
A convenience for expressing certain operations
A view does not necessarily exist in physical form, which limits the possible
update operations that can be applied to views.
SQL has various techniques for writing programs in various programming languages that
include SQL statements to access one or more databases. These include embedded (and
dynamic) SQL, SQL/CLI (Call Level Interface) and its predecessor ODBC (Open Data
Base Connectivity), and SQL/PSM (Persistent Stored Modules).
SQL has transaction control commands. These are used to specify units of database
processing for concurrency control and recovery purposes. SQL has language constructs
for specifying the granting and revoking of privileges to users. Privileges typically
correspond to the right to use certain SQL commands to access certain relations. Each
relation is assigned an owner, and either the owner or the DBA staff can grant to selected
users the privilege to use an SQL statement—such as SELECT INSERT, DELETE, or
UPDATE— to access the relation. In addition, the DBA staff can grant the privileges to
create schemas, tables, or views to certain users. These SQL commands—called GRANT
and REVOKE.
SQL has language constructs for creating triggers. These are generally referred to as active
database techniques, since they specify actions that are automatically triggered by events
such as database updates.
PROGRAM 1
1. Consider the following schema for a Library Database:
BOOK_AUTHORS(Book_id, Author_Name)
1. Retrieve details of all books in the library – id, title, name of publisher, authors,
numberof copies in each branch, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from
Jan2017 to Jun 2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this
datamanipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working
with asimple query.
5. Create a view of all books and its number of copies that are currently available in
theLibrary.
Schema Diagram:
CREATE TABLE Publisher ( Name varchar(200) PRIMARY KEY, Address varchar(400), Phone
varchar(10) );
INSERT INTO publisher (`Name`, `Address`, `Phone`) VALUES ('Pearson', 'New Delhi', '8976789543');
INSERT INTO `publisher` (`Name`, `Address`, `Phone`) VALUES ('PHI', 'New Delhi', '9876785333');
INSERT INTO `publisher` (`Name`, `Address`, `Phone`) VALUES ('Tata McGraw-Hill', 'New Delhi',
'9876785432');
INSERT INTO `publisher` (`Name`, `Address`, `Phone`) VALUES ('Technical Publications', 'Pune',
'9823451678');
INSERT INTO `publisher` (`Name`, `Address`, `Phone`) VALUES ('Universities Press', 'Hyderabad',
'9123214566');
Title varchar(200),
Publisher_Name varchar(200),
Pub_Year int,
FOREIGN KEY (Publisher_Name) REFERENCES Publisher(Name) ON DELETE SET NULL );
Date_out Date,
Due_date Date,
PRIMARY KEY (Book_id,Branch_id,Card_no),
FOREIGN KEY (Book_id) REFERENCES Book(Book_id) ON DELETE CASCADE, FOREIGN KEY
(Branch_id) REFERENCES Library_Branch(Branch_id) ON DELETE CASCADE,
FOREIGN KEY (Card_no) REFERENCES Borrower(Card_no) ON DELETE CASCADE );
Table: Book
Table: Book_Authors
Table:Book_copies
Table: Book_lending
Table:Borrower
Table: library_branch
Table: Publisher
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in ea
2. Get the particulars of borrowers who have borrowed more than 3 books, but fromJan 2017 to
Jun 2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect thisdata
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its workingwith a simple
query.
5. Create a view of all books and its number of copies that are currently available in the Library.
PROGRAM 2
2. Consider the following schema for Order Database:
SALESMAN(Salesman_id, Name, City, Commission)
CUSTOMER(Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS(Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesman who had more than one customer.
3. List all the salesman and indicate those who have and don’t have customers in their
cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order
of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
table salesman:-
INSERT INTO salesman (salesman_id, name, city, commission) VALUES (5001, 'James', 'Bangalore',
0.15);
INSERT INTO salesman (salesman_id, name, city, commission) VALUES (5002, 'John', 'Bangalore',
0.13);
INSERT INTO salesman (salesman_id, name, city, commission) VALUES (5003, 'Alex', 'Chennai', 0.11);
INSERT INTO salesman (salesman_id, name, city, commission) VALUES (5004, 'Lyon', 'Delhi', 0.14);
INSERT INTO salesman (salesman_id, name, city, commission) VALUES (5005, 'Nail', 'Delhi', 0.12);
table customer:-
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3001, 'Davis',
'Bangalore', 200, 5001);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3002, 'Rimando',
'Bangalore', 100, 5001);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3003, 'Johns',
'Delhi', 300, 5005);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3004, 'Brainard',
'Chennai', 200, 5002);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3005, 'Zusi',
'Chennai', 100, 5004);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3006, 'Shaini',
'Bangalore', 200, 5004);
INSERT INTO customer (customer_id, cust_name, city, grade, salesman_id) VALUES (3007, 'Smith',
'Mumbai', 400, 5005);
table orders :-
INSERT INTO orders (ord_no, purchase_amt, ord_date, customer_id, salesman_id)
VALUES (7001, 150.5, '2017-01-05’, 3005, 5004);
INSERT INTO orders (ord_no, purchase_amt, ord_date, customer_id, salesman_id)
VALUES (7002, 270.5, '2017-02-10’, 3001, 5001);
INSERT INTO orders (ord_no, purchase_amt, ord_date, customer_id, salesman_id)
Table: Salesman
Table: Customer
Table: Orders
select count(*)No_of_Customers from customer where grade > (select avg(grade) from
customer where city='Bangalore');
2. Find the name and numbers of all salesman who had more than one customer.
5001 James
5004 Lyon
5005 Nail
3. List all the salesman and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted.
PROGRAM 3
3. Consider the schema for Movie Database:
Schema Diagram:
'Ed'),
('110', '4', 'Cathie');
Table Actor:
Table: Director
Table: Movies
CSE @ HKBKCE 46 2022-23
18CSL58 DBMS LAB
Table: Movie_cast
Table: Rating
2. Find the movie names where one or more actors acted in two or more movies.
select mov_title from movies where mov_id in( select mov_id from movie_cast where
act_id in( select act_id from movie_cast group by act_id having count(*)>1));
(OR)
select distinct(mov_title) from movies join movie_cast on movies.mov_id =
movie_cast.mov_id and act_id in( select act_id from movie_cast group by act_id
having count(*)>1);
3. List all actors who acted in a movie before 2000 and also in a movie after 2015(use
JOIN operation).
4. Find the title of movies and number of stars for each movie that has at least one rating and
find the highest number of stars that movie received. Sort the result bymovie title.
update rating r set rev_stars=5 where r.mov_id = (select m.mov_id from movies
m,director d where m.dir_id=d.dir_id and Dir_Name='Steven Spielberg');
PROGRAM 4
4. Consider the schema for College Database:
Schema Diagram:
Table: Student
Table: Semsec
Table: Subject
Table: Class
Table: iamarks
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester andin each section.
Expected output:-
15CS61|Cryptography|8
15CS63|SS |10
4 Calculate the FinalIA (average of best two test marks) and update thecorresponding
table for all students.
UPDATE iamarks i set final=(SELECT finalia from finalia f where i.usn=f.usn and
i.subcode=f.subcode);(Syntax error near i)
60 rows affected
Give these details only for 8th semester A, B, and C section students.
PROGRAM 5
5. Consider the schema for Company Database:
1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls the
project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as
the maximum salary, the minimum salary, and the average salary in this department
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than Rs. 6,00,000.
table Employee:-
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('23412356', 'JENNIFER', 'PARIS', 'FEMALE','700000', '4');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('23456781', 'JAMES', 'NEWYORK', 'MALE','300000', '4'); INSERT INTO `employee` (`SSN`, `NAME`,
`ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES ('33344555', 'FRANKLIN',
'CALIFORNIA','MALE', '600000', '5');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('67891234', 'JOYCE', 'WASHINGTON','FEMALE', '400000', '5');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('123456789', 'JOHN', 'TEXAS', 'MALE','300000', '5'); INSERT INTO `employee` (`SSN`, `NAME`,
`ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES ('888666555', 'AHMAD', 'CALIFORNIA','MALE',
'700000', '4');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('984600445', 'MARK', 'WASHINGTON','MALE', '800000', '5');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('56789012', 'ALEENA', 'LONDON', 'FEMALE', '700000', '5');
INSERT INTO `employee` (`SSN`, `NAME`, `ADDRESS`, `SEX`, `SALARY`, `DNO`) VALUES
('89012345', 'ALICE', 'STAFFORD', 'FEMALE', '1200000', '5');
UPDATE `employee` SET `superssn` = '984600445' WHERE `ssn` = 23412356;
UPDATE `employee` SET `superssn` = '67891234' WHERE `ssn` = 23456781;
UPDATE `employee` SET `superssn` = '123456789' WHERE `ssn` = 33344555;
UPDATE `employee` SET `superssn` = '89012345' WHERE `ssn` = 56789012;
UPDATE `employee` SET `superssn` = '888666555' WHERE `ssn` = 67891234;
UPDATE `employee` SET `superssn` = '56789012' WHERE `ssn` = 89012345;
UPDATE `employee` SET `superssn` = '33344555' WHERE `ssn` = 123456789;
UPDATE `employee` SET `superssn` = '23456781' WHERE `ssn` = 888666555;
UPDATE `department` SET `mgrssn` = '888666555' WHERE `dno` = 1;
UPDATE `department` SET `mgrssn` = '123456789' WHERE `dno` = 4;
UPDATE `department` SET `mgrssn` = '33344555' WHERE `dno` = 5;
table department:-
INSERT INTO department values(1,’ADMINISTRATION’,888666555,’1992-06-25’);
INSERT INTO department values(4,’RESEARCH’,23456781,’1990-04-23’);
INSERT INTO department values(5,’ACCOUNTS’,333445,’1998-05-22’);
CSE @ HKBKCE 69 2022-23
18CSL58 DBMS LAB
table dlocation:-
INSERT INTO `dlocation` (`DNO`, `DLOC`) VALUES ('1', 'WASHINGTON'); INSERT INTO
`dlocation` (`DNO`, `DLOC`) VALUES ('4', 'CALIFORNIA'); INSERT INTO `dlocation` (`DNO`,
`DLOC`) VALUES ('5', 'NEW YORK'); INSERT INTO `dlocation` (`DNO`, `DLOC`) VALUES ('5',
'WASHINGTON');
table project:-
INSERT INTO project (PNAME, PNO, PLOCATION, DNO) VALUES ('PRODUCTA', 1, 'HOUSTON',
5);
INSERT INTO project (`PNAME`, `PNO`, `PLOCATION`, `DNO`) VALUES ('PRODUCTB', 2,
'WASHINGTON', 5);
INSERT INTO project (`PNAME`, `PNO`, `PLOCATION`, `DNO`) VALUES ('PRODUCTC', 3,
'CALIFORNIA', 5);
INSERT INTO project (`PNAME`, `PNO`, `PLOCATION`, `DNO`) VALUES
('COMPUTERIZATION',10,'NEW YORK',4);
INSERT INTO project (`PNAME`, `PNO`, `PLOCATION`, `DNO`) VALUES ('IOT',20,'PARIS',1);
INSERT INTO project (`PNAME`, `PNO`, `PLOCATION`, `DNO`) VALUES
('REORGANIZATION',30,'STAFFORD',4);
table works_on:-
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('123456789', '1', '33');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('123456789', '2', '8');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('984600445', '3', '40');
INSERT INTO `works_on`(`SSN`, `PNO`, `HOURS`) VALUES ('984600445', '1', '50');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('888666555', '1', '45');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('888666555', '2', '54');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('33344555', '2', '10');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('33344555', '3', '10');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('33344555', '10', '10');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('33344555', '20', '10');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('67891234', '30', '20');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('67891234', '10', '20');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('23412356', '30', '12');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('23412356', '20', '14');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('23456781', '20', '20');
INSERT INTO `works_on` (`SSN`, `PNO`, `HOURS`) VALUES ('23456781', '1', '34');
CSE @ HKBKCE 70 2022-23
18CSL58 DBMS LAB
Table: Employee
Table: Department
Table: Dlocation
Table: Project
Table: Works_on
1. Make a list of all project numbers for projects that involve an employee whose last name is
‘Scott’, either as a worker or as a manager of the department that controls the project.
select distinct p.pno from project p, department d, employee e where p.dno=d.dno and
d.mgrssn=e.ssn and lname="scott" union select distinct p.pno from project p, works_on
w,employee e where w.pno=p.pno and w.ssn=e.ssn and lname="scott";
2. Show the resulting salaries if every employee working on the ‘IoT’ project isgiven a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as wellas the
maximum salary, the minimum salary, and the average salary in this department.
4. Retrieve the name of each employee who works on all the projects controlled bydepartment
number 5 (use NOT EXISTS operator).
select name from employee where not exists ( ( select Pnumber from project where
Dnum=5) except ( select pno from works_on where ssn=essn) );
name
AHMAD
5. For each department that has more than five employees, retrieve the department number and
the number of its employees who are making more than Rs. 6,00,000.
Select d.dno as D_NO,count(*) as No_Of_Employees from department d, employee e
where d.dno=e.dno and e.salary>600000 and e.dno in (select dno from employee group by
dno having count(*)>5) group by d.dno