Lab Book: F. Y. B. B. A. (C.A.) Semester I
Lab Book: F. Y. B. B. A. (C.A.) Semester I
) Semester I
Lab Book
Name: _____________________________________________
College Name: ______________________________________
Roll No. ____________________________________________
Division: ____________________________________________
Academic Year: _____________________________________
CA-106:
Computer Laboratory Based on
CA-104
(DBMS)
Assignment Completion Sheet
Index
9 Joins
10 Case Study
Date:-
Introduction
This workbook is intended to be used by FYBBA (CA) students for their practical
purpose. It helps to the students for clearing their theoretical as well as practical concepts.
Editors:
The table name and column names are helpful to interpret the meaning of values in each row. The
data are represented as a set of relations. In the relational model, data are stored as tables.
However, the physical storage of the data is independent of the way the data are logically
organized.
1. Attribute: Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is stored along
with its entities. A table has two properties rows and columns. Rows represent records and
columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system. Relation
instances never have duplicate tuples.
9. Relation key - Every row has one, two or multiple attributes, which is called relation key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is known as
attribute domain
Relational Integrity constraints are referred to conditions which must be present for a valid
relation. These integrity constraints are derived from the rules in the mini-world that the database
represents. There are many types of integrity constraints. Constraints on the Relational database
management system are mostly divided into three main categories are:
1. Domain constraints
2. Key constraints
3. Referential integrity constraints
Domain Constraints:
Domain constraints can be violated if an attribute value is not appearing in the
corresponding domain or it is not of the appropriate data type.
Domain constraints specify that within each tuple, and the value of each attribute must be
unique. This is specified as data types which include standard data type’s integers, real numbers,
characters, Booleans, variable length strings, etc.
Key constraints:
An attribute that can uniquely identify a tuple in a relation is called the key of the table. The value
of the attribute for different tuples in the relation has to be unique.
Example:
In the given table, CustomerID is a key attribute of Customer Table. It is most likely to have a
single key for one customer, CustomerID =1 is only for the CustomerName =" Google".
1 Google Active
2 Amazon Active
3 Apple Inactive
Referential integrity constraints are base on the concept of Foreign Keys. A foreign key is an
important attribute of a relation which should be referred to in other relationships. Referential
integrity constraint state happens where relation refers to a key attribute of a different or same
relation. However, that key element must exist in the table.
Example:
Whenever one of these operations are applied, integrity constraints specified on the relational
database schema must never be violated.
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.
Example:
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Keys help you to identify any row of data in a table. In a real-world application, a table could
contain thousands of records. Moreover, the records could be duplicated. Keys ensure that you
can uniquely identify a table record despite these challenges.
Allows you to establish a relationship between and identify the relation between tables
Help you to enforce identity and integrity in the relationship.
DBMS has following seven types of Keys each have their different functionality:
A superkey is a group of single or multiple keys which identifies rows in a table. A Super key
may have additional attributes that are not needed for unique identification.
Example:
A column or group of columns in a table which helps us to uniquely identifies every row in that
table is called a primary key. This DBMS can't be a duplicate. The same value can't appear more
than once in the table.
Example:
All the keys which are not primary key are called an alternate key. It is a candidate key which is
currently not the primary key. However, A table may have single or multiple choices for the
primary key.
Example: In this table.
StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary
key, Roll No, Email becomes the alternative key.
The Primary key should be selected from the candidate keys. Every table must have at least a
single candidate key.
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.
A foreign key is a column which is added to create a relationship with another table. Foreign keys
help us to maintain data integrity and also allows navigation between two different instances of an
entity. Every relationship in the model needs to be supported by a foreign key.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
In this example, we have two table, teach and department in a school. However, there is no way to
see which search work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship
between the two tables.
Compound key has many fields which allow you to uniquely recognize a specific record.
It is possible that each column may be not unique by itself within the database. However, when
combined with the other column or columns the combination of composite keys become unique.
Example:
OrderNo PorductID Product Name Quantity
In this example, OrderNo and ProductID can't be a primary key as it does not uniquely
identify a record. However, a compound key of Order ID and Product ID could be used as it
uniquely identified each record.
What is the Composite key?
A key which has multiple attributes to uniquely identify rows in a table is called a composite key.
The difference between compound and the composite key is that any part of the compound key
can be a foreign key, but the composite key may or maybe not a part of the foreign key.
An artificial key which aims to uniquely identify each record is called a surrogate key. These kind
of key are unique because they are created when you don't have any natural primary key. They do
not lend any meaning to the data in the table. Surrogate key is usually an integer.
Above, given example, shown shift timings of the different employee. In this example, a
surrogate key is needed to uniquely identify each employee.
Helps you to uniquely identify a record It is a field in the table that is the primary key of
in the table. another table.
Primary Key never accepts null values. A foreign key may accept multiple null values.
Primary key is a clustered index and A foreign key cannot automatically create an
data in the DBMS table are physically index, clustered or non-clustered. However, you
organized in the sequence of the can manually create an index on the foreign
clustered index. key.
You can have the single Primary key in You can have multiple foreign keys in a table.
a table.
SQL (Structured Query Language)
What is SQL?
SQL programming can be effectively used to insert, search, update, delete database records.
That doesn't mean SQL cannot do things beyond that. It can do a lot of things including, but not
limited to, optimizing and maintenance of databases.
Relational databases like MySQL Database, Oracle, Ms SQL Server, Sybase, etc. use SQL.
What is NoSQL?
NoSQL is a non-relational DBMS, that does not require a fixed schema, avoids joins, and is easy
to scale. NoSQL database is used for distributed data stores with humongous data storage needs.
NoSQL is used for Big data and real-time web apps. For example companies like Twitter,
Facebook, Google that collect terabytes of user data every single day.
NoSQL database stands for "Not Only SQL" or "Not SQL." Though a better term would NoREL
NoSQL caught on. Carl Strozz introduced the NoSQL concept in 1998.
Traditional RDBMS uses SQL syntax to store and retrieve data for further insights. Instead, a
NoSQL database system encompasses a wide range of database technologies that can store
structured, semi-structured, unstructured and polymorphic data.
Definition SQL databases are primarily called NoSQL databases are primarily called as
RDBMS or Relational Databases Non-relational or distributed database
Design for Traditional RDBMS uses SQL syntax NoSQL database system consists of
and queries to analyze and get the data various kinds of database technologies.
for further insights. They are used for These databases were developed in
OLAP systems. response to the demands presented for the
development of the modern application.
Schema SQL databases have a predefined NoSQL databases use dynamic schema for
schema unstructured data.
Ability to SQL databases are vertically scalable NoSQL databases are horizontally scalable
scale
Best suited An ideal choice for the complex query It is not good fit complex queries.
for intensive environment.
Hierarchical SQL databases are not suitable for More suitable for the hierarchical data
data storage hierarchical data storage. store as it supports key-value pair method.
Variations One type with minor variations. Many different types which include key-
value stores, document databases, and
graph databases.
Development It was developed in the 1970s to deal Developed in the late 2000s to overcome
Year with issues with flat file storage issues and limitations of SQL databases.
Consistency It should be configured for strong It depends on DBMS as some offers strong
consistency. consistency like MongoDB, whereas
others offer only offers eventual
consistency, like Cassandra.
Best Used RDBMS database is the right option NoSQL is a best used for solving data
for for solving ACID problems. availability problems
Importance It should be used when data validity is Use when it's more important to have fast
super important data than correct data
Best option When you need to support dynamic Use when you need to scale based on
queries changing requirements
Storage Type Highly Available Storage (SAN, Commodity drives storage (standard
RAID, etc.) HDDs, JBOD)
Best features Cross-platform support, Secure and Easy to use, High performance, and
free Flexible tool.
Average The average salary for any The average salary for "NoSQL
salary professional SQL Developer is developer" ranges from approximately
$84,328 per year in the U.S.A. $72,174 per year
ACID vs. ACID( Atomicity, Consistency, Base ( Basically Available, Soft state,
BASE Isolation, and Durability) is a standard Eventually Consistent) is a model of many
Model for RDBMS NoSQL systems
SQL language is divided into four types of primary language statements: DML, DDL, DCL and
TCL. Using these statements, we can define the structure of a database by creating and altering
database objects, and we can manipulate data in a table through updates or deletions. We also can
control which user can read/write data or manage transactions to create a single unit of work.
DML statements affect records in a table. These are basic operations we perform on data such as
selecting a few records from a table, inserting new records, deleting unnecessary records, and
updating/modifying existing records.
Syntax:-
Example:-
2. Update :-
The UPDATE statement in SQL is used to update the data of an existing table in database. We
can update single columns as well as multiple columns using UPDATE statement as per our
requirement.
Syntax :
UPDATE table_name SET column1 = value1, column2 = value2,...
WHERE condition;
Example:-
Updating single column: Update the column NAME and set the value to ‘Satyavan’ in all the
rows where Age is 35.
UPDATE Student SET NAME = 'Satyavan' WHERE Age = 35;
3. DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can
delete a single record or multiple records depending on the condition we specify in the
WHERE clause.
DDL statements are used to alter/modify a database or table structure and schema. These
statements handle the design and storage of database objects.
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes.
Name
Data type
Size (column width).
Each table column definition is a single clause in the create table syntax.
Each table column definition is separated from the other by a comma.
Finally, the SQL statement is terminated with a semicolon.
Syntax:
DROP TABLE <table_name>
Example:
DROP TABLE Student;
It will destroy the table and all data which will be recorded in it.
By The use of ALTER TABLE Command we can modify our exiting table.
Adding New Columns
Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),......n)
Example:
ALTER TABLE Student ADD (Age number(2), Marks number(3));
The Student table is already exist and then we added two more columns Age and
Marks respectively, by the use of above command.
Syntax:
ALTER TABLE <table_name> DROP COLUMN <column_name>
Example:
ALTER TABLE Student DROP COLUMN Age;
This command will drop particular column
Syntax:
ALTER TABLE <table_name> MODIFY (<column_name> <NewDataType>(<NewSize>))
Example:
ALTER TABLE Student MODIFY (Name Varchar2(40));
The Name column already exist in Student table, it was char and size 30, now it is modified by
Varchar2 and size 40.
Restriction on the ALTER TABLE:
Using the ALTER TABLE clause the following tasks cannot be performed.
Once data has been inserted into a table, the next most logical operation would be to view
what has been inserted. The SELECT SQL verb is used to achieve this.
SELECT First_name, DOB FROM STUDENT WHERE Roll_no = '101'; Cover it by single
inverted comma if its datatype is varchar or char.
This Command will show one row. Because you have given condition for only one row and
particular records. If condition which has given in WHERE Clause is true then records will be
fetched otherwise it will show no records selected.
Eliminating Duplicates:
A table could hold duplicate rows. In such a case, you can eliminate duplicates.
Syntax: SELECT DISTINCT col, col, .., FROM table_name;
eg : SELECT DISTINCT * FROM Student;
It scans through entire rows, and eliminates rows that have exactly the same contents in
each column.
Sorting DATA:
The Rows retrieved from the table will be sorted in either Ascending or Descending order
depending on the condition specified in select statement, the Keyword has used ORDER BY.
it will in show records as alphabetical order from A to Z ascending order. If you want Descending
order means Z to A then used DESC Keyword at last.
1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX
Aggregate functions allow us to easily produce summarized data from our database.
COUNT Function:
The COUNT function returns the total number of values in the specified field. It works on both
numeric and non-numeric data types. All aggregate functions by default exclude nulls values
before working on the data.
COUNT (*) is a special implementation of the COUNT function that returns the count of all the
rows in a specified table. COUNT (*) also considers Nulls and duplicates.
11 20-06-2012 NULL 1 1 0
12 22-06-2012 25-06- 1 2 0
2012
13 22-06-2012 25-06- 3 2 0
2012
14 21-06-2012 24-06- 2 2 0
2012
15 23-06-2012 NULL 3 3 0
Let's suppose that we want to get the number of times that the movie with id 2 has been rented out
SELECT COUNT(`movie_id`) FROM `movierentals` WHERE `movie_id` = 2;
COUNT('movie_id')
3
DISTINCT Keyword:
The DISTINCT keyword that allows us to omit duplicates from our results. This is achieved by
grouping similar values together.
Now let's execute the same query with the distinct keyword -
SELECT DISTINCT `movie_id` FROM `movierentals`;
MIN function:
The MIN function returns the smallest value in the specified table field.
As an example, let's suppose we want to know the year in which the oldest movie in our library
was released, we can use MIN function to get the desired information.
MAX function:
Just as the name suggests, the MAX function is the opposite of the MIN function.
It returns the largest value from the specified table field.
Let's assume we want to get the year that the latest movie in our database was released. We can
easily use the MAX function to achieve that.
Suppose we want a report that gives total amount of payments made so far. We can use the
SUM function which returns the sum of all the values in the specified column. SUM works on
numeric fields only. Null values are excluded from the result returned.
The query shown below gets the all payments made and sum them up to return a single result.
SELECT SUM(`amount_paid`) FROM `payments`;
SUM('amount_paid')
10500
AVG function:
AVG function returns the average of the values in a specified column. Just like the SUM
function, it works only on numeric data types.
Suppose we want to find the average amount paid. We can use the following query -
SELECT AVG(`amount_paid`) FROM `payments`;
AVG('amount_paid')
3500
Normalization
Normalization is a process of organizing the data in database to avoid data redundancy, insertion
anomaly, update anomaly & deletion anomaly. Let’s discuss about anomalies first then we will
discuss normal forms with examples.
Anomalies in DBMS
There are three types of anomalies that occur when the database is not normalized. These are –
Insertion, update and deletion anomaly. Let’s take an example to understand this.
Example: Suppose a manufacturing company stores the employee details in a table named
employee that has four attributes: emp_id for storing employee’s id, emp_name for storing
employee’s name, emp_address for storing employee’s address and emp_dept for storing the
department details in which the employee works. At some point of time the table looks like this:
The above table is not normalized. We will see the problems that we face when a table is not
normalized.
Update anomaly: In the above table we have two rows for employee Rick as he belongs to two
departments of the company. If we want to update the address of Rick then we have to update the
same in two rows or the data will become inconsistent. If somehow, the correct address gets
updated in one department but not in other then as per the database, Rick would be having two
different addresses, which is not correct and would lead to inconsistent data.
Insert anomaly: Suppose a new employee joins the company, who is under training and
currently not assigned to any department then we would not be able to insert the data into the
table if emp_dept field doesn’t allow nulls.
Delete anomaly: Suppose, if at a point of time the company closes the department D890 then
deleting the rows that are having emp_dept as D890 would also delete the information of
employee Maggie since she is assigned only to this department.
To overcome these anomalies we need to normalize the data. In the next section we will discuss
about normalization.
Normalization
Here are the most commonly used normal forms:
As per the rule of first normal form, an attribute (column) of a table cannot hold multiple
values. It should hold only atomic values.
Example: Suppose a company wants to store the names and contact details of its employees. It
creates a table that looks like this:
Two employees (Jon & Lester) are having two mobile numbers so the company stored them in the
same field as you can see in the table above.
This table is not in 1NF as the rule says “each attribute of a table must have atomic (single)
values”, the emp_mobile values for employees Jon & Lester violates that rule.
To make the table complies with 1NF we should have the data like this:
An attribute that is not part of any candidate key is known as non-prime attribute.
Example: Suppose a school wants to store the data of teachers and the subjects they teach.
They create a table that looks like this: Since a teacher can teach more than one subjects, the table
can have multiple rows for a same teacher.
The table is in 1 NF because each attribute has atomic values. However, it is not in 2NF
because non prime attribute teacher_age is dependent on teacher_id alone which is a proper subset
of candidate key. This violates the rule for 2NF as the rule says “no non-prime attribute is
dependent on the proper subset of any candidate key of the table”.
To make the table complies with 2NF we can break it in two tables like this:
teacher_details table:
Teacher_id Teacher_age
111 38
222 38
333 40
teacher_subject table:
teacher_id Subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
An attribute that is not part of any candidate key is known as non-prime attribute.
In other words 3NF can be explained like this: A table is in 3NF if it is in 2NF and for each
functional dependency X-> Y at least one of the following conditions hold:
An attribute that is a part of one of the candidate keys is known as prime attribute.
Example: Suppose a company wants to store the complete address of each employee, they create a
table named employee_details that looks like this:
To make this table complies with 3NF we have to break the table into two tables to
remove the transitive dependency:
employee table:
It is an advance version of 3NF that’s why it is also referred as 3.5NF. BCNF is stricter
than 3NF. A table complies with BCNF if it is in 3NF and for every functional dependency X->Y,
X should be the super key of the table.
Example: Suppose there is a company wherein employees work in more than one
department. They store the data like this:
emp_idemp_nationality emp_dept dept_type dept_no_of_emp
1001 Austrian Production and planning D001 200
1001 Austrian Stores D001 250
1002 American design and technical support D134 100
1002 American Purchasing department D134 600
The table is not in BCNF as neither emp_id nor emp_dept alone are keys.
To make the table comply with BCNF we can break the table in three tables like this:
emp_nationality table:
Emp_id Emp_nationality
1001 Austrian
1002 American
emp_dept table:
Emp_id Emp_dept
1001 Production and planning
1001 Stores
1002 design and technical support
1002 Purchasing department
Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}
Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}
This is now in BCNF as in both the functional dependencies left side part is a key.
Exercise 1: DDL(Data Definition Language)
Creating Tables:
Create table for the information given below by choosing appropriate data types and
also specifying proper primary key constraint on fields which are underlined
Create table for the information given below by choosing appropriate data types and
also specifying proper primary key constraint on fields which are underlined.
Create table for the information given below by choosing appropriate data types and
also specifying proper primary key constraint on fields which are underlined.
1. Employee(ENo, EName, Joining_date,company_name,salary,Designation)
2. College(College_Code,College_Name,Address,Establish_year)
3. Doctor(Dno, Dname, Specialization,Qualification)
4. ClassRoom(CRoomNo,location,capacity)
Create table student( Roll_no, sname, date_of_birth). Add new column into student relation
named address as a text data type and a column phone of data type integer.
Create table driver (licence_no, Name, Address) and perform the following queries
1. Add new column age of data type integer.
2. Alter table by modifying driver_name to “Patil”
3. Alter table driver ,drop the column age.
4. Remove the driver table from the database.
Create table Game (name, no-of-players, captain_name) and perform the following queries
1. Add new column game_no of data type integer.
2. Alter table by adding constraint uppercase to captain_name.
3. Modify table by adding the column game_duration.
4. Add column game_type with values cricket,hockey,tennis.
5. Remove game table from the database.
Consider the following table Hospital (HNo, HName, Addr, Est_Year , speciality) and
answer the following query.
1. Insert at least five records into the table.
2. Update an address of hospital to “Pimple Gurav” whose name is “Birla”.
3. Update the specialty of hospital to “Multi” whose established year is between 1990 to
2000.
4. Delete the details of Hospital whose address is “Pimpri”.
Consider the following table Student (Roll_No, Name, class, DOB, college) and answer
the following query.
1. Insert at least 10 records into the table.
2. Update the class of student to “TY” whose birth date is ‘18/03/1999’.
3. Delete the details of students whose college is “Dr.D Y Patil”.
4. Update the college of student to “Dr. D Y Patil “ whose name is “Yash”.
Consider the following entities and their relationships. Create a RDB in 3 NF for the
following and answer the queries:
Consider the following entities and their relationships. Create a RDB in 3 NF for the
following and answer the queries:
Consider the following entities and their relationships. Create a RDB in 3 NF for the
following and answer the queries:
Consider the following tables and integrity constraints given and create the tables
accordingly:
Constraints:
1. Emp_name should be in uppercase.
2. Emp_desg can be (‘Manager’, ‘staff’, ‘worker’).
3. Emp_sal should be greater than zero.
Constraints:
1. Room type must be one of single, double, family.
2. Price must be between Rs.500/- and 1000/-.
3. Room no must be between 1 and 100.
Emp and Dept are related with many to one with each other. Create the Relations
accordingly, so that the relationship is handled properly and relations are in normalized
form (3NF).
Execute following select queries & write the business task performed by each
query.
1. Select * from emp;
2. Select empno, name from emp;
3. Select distinct deptno from emp;
4. Select * from emp where deptno = ___;
5. Select * from emp where address = ‘pune’ and sal > _____;
6. Select * from emp where address = ‘pune’ and salary between _____
and _____;
7. Select * from emp where name like ‘---%’
8. Select * from emp where name like ‘%and%’
9. Select * from emp where salary is null;
10. Select * from emp order by eno;
11. Select * from emp order by deptno, eno desc;
12. Select deptno as department, sum(salary) as total from emp group by
deptno order by deptno;
13. Select deptno as department , count(eno) as total_emp from emp group
by deptno having count(eno ) > _____ order by deptno;
14. select avg(salary) from emp;
15. select max(salary),deptno from emp group by deptno having max(sal) >
__________;
16. select deptno, min(salary) from emp order by deptno;
17. update emp set salary = salary + 0.5*salary where deptno = (select
deptno from department where dname = ‘finance’);
18. update emp set deptno = (select deptno from department where dname
= ‘finance’) Where deptno = (select deptno from department where
dname = ‘inventory’);
An area can have one or more person living in it , but a person belongs to exactly
one area. The attribute ‘area_type’ can have values as either urban or rural.
Create the Relations accordingly, so that the relationship is handled properly and
the relations are in normalized form (3NF).
Assume appropriate data types for all the attributes. Add any new attributes as
required, depending on the queries. Insert sufficient number of records in the relations /
tables with appropriate values as suggested by some of the queries.
Execute following select queries & write the business task performed by each query.
1 List the names of all people living in ‘______’ area.
2 List details of all people whose names start with the alphabet ‘_’ & contains
maximum _alphabets in it.
3 List the names of all people whose birthday falls in the month of ______.
4 Give the count of people who are born on ‘_______’
5 Give the count of people whose income is below ______.
2. List names of all people whose income is between _____ and _____;
3. List the names of people with average income
4. List the sum of incomes of people living in ‘____’
5. List the names of the areas having people with maximum income (duplicate
areas must be omitted in the result)
6. Give the count of people in each area
7. List the details of people living in ‘____’ and having income greater than
_____;
8. List the details pf people, sorted by person number
9. List the details of people, sorted by area, person name
10. List the minimum income of people.
11. Transfer all people living in ‘pune’ to ‘mumbai’.
12. Delete information of all people staying in ‘urban’ area
Each employee can work on one or more projects, and a project can have many
employees working in it. The number of hours worked on each project by an employee
also needs to be stored.
Create the Relations accordingly, so that the relationship is handled properly and
the relations are in normalized form (3NF).
Assume appropriate data types for the attributes. Add any new attributes, new
relations as required by the queries.
Insert sufficient number of records in the relations / tables with appropriate values
as suggested by some of the queries.
Write the queries for following business tasks & execute them.
1. list the names of departments that controls projects whose budget is greater
than ___
2. list the names of projects, controlled by department No __, whose budget is
greater than atleast one project controlled by department No __
3. list the details of the projects with second maximum budget
4. list the details of the projects with third maximum budget.
5. list the names of employees, working on some projects that employee
number __ is working.
6. list the names of employees who do not work on any project that employee
number __ works on
7. list the names of employees who do not work on any project controlled by
‘______’ department
8. list the names of projects along with the controlling department name, for
those projects which has atleast __ employees working on it.
9. list the names of employees who is worked for more than 10 hrs on atleast
one project controlled by ‘______’ dept.
10. list the names of employees , who are males , and earning the maximum
salary in their department.
11. list the names of employees who work in the same department as ‘_____’.
12. list the names of employees who do not live in _____ or _______.
Each actor has acted in one or more movie. Each producer has produced many
movies but each movie can be produced by more than one producers. Each movie has one
or more actors acting in it, in different roles.
Create the Relations accordingly, so that the relationship is handled properly and
the relations are in normalized form (3NF).
Assume appropriate data types for the attributes. Add any new attributes, new
relations as required by the queries.
Insert sufficient number of records in the relations / tables with appropriate values
as suggested by some of the queries.
Write the queries for following business tasks & execute them.
1. List the names of actors who have acted in at least one movie, in
which ‘shahrukh’ has acted.
2. List the names of movies with the highest budget.
3. List the names of movies with the second highest budget
4. List the names of actors who have acted in the maximum number of
movies.
5. List the names of movies, produced by more than one producer.
6. List the names of actors who are given with the maximum charges
for their movie.
7. List the names of producers who produce the same movie as
‘_____’.
8. List the names of actors who do not live in _____ or _______.
You can combine multiple queries using the set operators UNION, UNION ALL, INTERSECT
and Except. ALL set operators have equal precedence.
2. Union all: Returns the union of two sets of values, retaining all duplicates.
Syntax: <select query>
Union all
<select query>
4 Intersect all: Returns the intersection of two sets of values, retaining duplicates.
Syntax: <select query>
Intersect all
<select query>
5 Except: Returns the difference between two set of values, I.e returns all values from set1 , not
contained in set2 .eliminates duplicates.
Syntax: <select query>
except
<select query>
6 Except all: Returns the difference between two set of values, i.e. returns all values from set1,
Not contained in set2 .Retains all duplicates.
Syntax: <select query>
Except all
<select query>
Student and Subject are related with many-to-many relationship with attribute
marks and status. Create the Relations accordingly, so that the relationship is handled
properly and the relations are in normalized form (3NF).
The meaning of above relationship is enrolled has one record per student _class pair
such that the student is enrolled in the class. Read the query carefully and insert sufficient
number of records in the relations / tables with appropriate values to perform the
following queries.
1. Find the names of all classes that either meet in room R128 or have five or more
students enrolled.
Sql>Select c.name from class c where c.room =’r128’ or c.name in ( select e.cname
from enrolled e group by e.name having count(*)>= 5);
2. Find the name of the oldest student who is either a history subject or enrolled in a
course taught by I.teach.
Sql> Select max(s.age) from student s where (s.subject=’history’) or s.num in (select
e.num from class c ,enrolled e ,faculty f where e.name =c.name and c.fid=f.id and
f.fname =’I.teach);
5. Find the names of faculty members who teach in every room in which some class
is taught.
Sql> Select distinct f.name from faculty f where not exists (( select * from class)
except (select c1.room from class c1 where c1.fid = f.fid));
A housing society needs to manage the administrative information related to the society.
The society is made up of different types of flats like 2BHK, 1BHK, 3BHK. Each type has a well
defined square-feet area . The outright sale rate & the rental value of the flat depends on the type
of the flat. Each flat has a single owner. Each owner can have one or more flats in his name. The
name, address , phone etc of the owner need to be maintained. For each flat, its type, the floor no,
any internal specifications needs to be maintained.
The society also contains a club-house, which is rented out to flat owners , at a nominal
rate for conducting various functions / programmes. Society would like to print reports like
number of functions held in the club-house during a month / period etc.
Every month maintenance amount is collected from the owners of the flats. Society needs
to maintain this finance information, like how much amount collected for a month, whether any
defaulters for a month, sending reminders to the defaulters etc. The expenditure information
includes money spent on maintenance of the society like paying the sweepers, cleaners of the
common area of the society, any emergency expense, salaries of the security etc. Every month the
society would like to print a report of expenditure versus collection.
Design the relational database for the above, so that the following queries can be answered:
The following constraints are to be defined for the vehicles, drivers, and destination places:
Design the relational database for the above company, so that the following queries can be
answered:
1. List the names of drivers who have driven a car to “Mumbai”
2. List the name of customers who have booked a “SUV” to “satara”
3. List the names of customers who have booked cars to pune or Mumbai or Lonavla
4. List the details of cars that have never driven down to “Mumbai”
5. List the details of the place to which maximum number of customers have driven
down.
6. List the details of the driver who have driven all the vehicles of the company.
7. List the names of the drivers who have driven atleast two cars to “Mumbai
8. List the names of drivers who have also driven some vehicles to “Mumbai”
9. List the details of customers who have booked more than two vehicles to “solapur”
10. List the names of customers who have booked maximum number of vehicles