01. Data Lecture
01. Data Lecture
Lecture Sheet-01
Database Management System
Syllabus: ER model, relational model (relational algebra, tuple calculus), database design (integrity
constraints, normal forms), query languages (SQL), transactions and concurrency control
Database
The collection of data usually referred to as the database, contains information relevant to an enterprise. The
primary goal of DBMS is to provide a way to store and retrieve database information that is both
convenient and efficient. Database systems are designed to manage large bodies of information. Management
of data involves both, defining structures for storage of information and providing mechanisms for the
manipulation of information. In addition, the database system must ensure the safety of the information stored,
despite system crashes or attempts an unauthorized access. If data are to be shared among several users, the
system must avoid possible anomalous results.
Database System Applications
Databases are widely used. There are some representative applications:-
Banking ○ Airlines ○ Universities ◦ Credit card transactions Telecommunication • Finance ◦
Sales • Online retailers
Purpose of Database System
One way to keep the information on a computer is to store in operating system. To allow users to manipulate
the information, the system has a number of application programs that manipulates the files. This typical
file processing system is supported by a conventional operating system. The system stores permanent records
in various files and it needs different application program to extract records from and add records to, the
appropriate files. Functions of database management system are:
Redundancy control
Data independence
Data integrity
Concurrency control
Security
backup
There are problems with the straight file-processing approach: [ICT Ministry-2017]
(i) Data redundancy and inconsistency
a) Same information may be duplicated in several places
b) All copies may not be updated properly. [ককোন একটো ফোইল update করল অনয ফোইল update
হলব নো কেটো inconsistency]
(ii) Difficulty in accessing data
a) May have to write a new application program to satisfy an unusual request.
b) e.g. find all customers with the same postal code
c) Could generate this data manually, but a long job [Query লখলই database এ data চল আলে লকন্তু
ফোইল আলে নো ]
1
Cloud IT Online Lecture Sheet
(iii) Data isolation
a) Data in different files
b) Data in different formats
c) Difficult to write new application programs. [ককোন একটো data database এ একইভোলব রোলখ। লকন্তু
ফোইল দুই বো তলতোলদক ভোলবও থোকলত পোলর। কযমনঃ male বো female ফোইল character লহলেলব রোখো যোয় বো
Boolean লহলেলব রোখো যোয়, কযটি data isolation বো লবলিন্নতো]
(iv) Multiple users
a) Want concurrency for faster response time.
b) Need protection for concurrent updates
(v) Security problems
a) Every user of the system should be able to access only the data they are permitted to see. e.g.
payroll people only handle employee records, and cannot see customer accounts; tellers only access account
data and cannot see payroll data.
b) Difficult to enforce this with application programs
(vi) Integrity problems
a) Data may be required to satisfy constraints e.g. no account balance below 5000/-
b) Difficult to enforce or to change constraints with the file-processing approach. These problems and
others led to the development of database management systems.
Components of database systems
Software
Hardware
Data: It is the collection of facts. The database contains the data and the metadata
Procedures
Data access language:SQL
People: System Administrator, Database Administrator, Database Designer, Application Programmer,
End User
Database Administrator [RAKUB-ADA-2021]
A person who has central control of both the data and the programs that access those data over the system is
called database administrator. The functions of DBA include
Schema definition The DBA creates the original database schema by executing a set of data
definitions statement in the DDL.
Storage structure and access method definition
Schema and physical organization modification The DBA carries out changes to the schema and
physical organization.
Granting of authorization of data access
Routine maintenance
Examples of routine maintenance activities are
1. Periodically backing up the database
2. Ensuring that enough free disk space is available for normal operations and upgrading disk space as
required.
3. Monitoring jobs running on the database and ensuring that performance is not degraded by very
expensive tasks submitted by some users.
2
Cloud IT Online Lecture Sheet
Previous Year Questions
1. What are the roles of database engineer?[RAKUB-ADA-2021]
2. What is RDBMS? Why data are stored in database system instead of file? [ICT Ministry-2017]
Data abstraction/architecture
Main purpose of a database system is to provide users with an abstract view of the system. The system hides
certain details of how data is stored and created and maintained. Complexity should be hidden from database
users.
Levels of Database system There are several levels of abstraction:
1. Physical Level:
○ How the data are stored: e.g. index, B-tree, hashing
◦ Lowest level of abstraction e.g. Data Compression and encryption techniques
. Complex low-level structures
2. Conceptual Level:
◦ Next highest level of abstraction
◦ describes what data are stored
◦ describes the relationships among data.
◦ Database administrator level.
3. View Level:
◦ Highest level.
• Describes part of the database for a particular group of users
◦ Can be many different views of a database. e.g. tellers in a bank get a view of customer
accounts, but not of payroll data,.
Data models
Data model is a collection of tools, which describes data, relationships, constraints and semantics. It gives the
logical structure of the database. It describes the relationship of data in the database. Data models are of
various types:
1. Relational Model: It is a collection of relations (tables). In relational model, each table is stored as a separate
file.
3
Cloud IT Online Lecture Sheet
2. Entity-Relationship data model: This model is based on the notion of real-world entities and relationship
among them.
3. Object-Based data model: It defines the database as objects, its properties and its operations. In this model,
objects with the similar structures comprise a class. The classes are organized into hierarchies. The operations
on these classes are performed through methods.
4. Semi-Structured data model: It is also known as XML model. This model is used to exchange data over the
web. It uses hierarchical tree structures. In this model, data can be represented as elements by using tags.
5. Network model: In this model, data is represented as record types. The data in this model has many-to-many
relationship.
6. Hierarchical model: In this model, the data is represented as a hierarchical tree structure.
Relational Model
The database in relational model is represented as a collection of relations (tables). A relation is a kind of set.
It is also a subset of a Cartesian product of an unordered set of ordered tuples. Relational model was proposed
by E. F. Codd, which stores data in a tabular form. It consists of a table where rows represent records and
columns represent the attributes. It has various terminologies as follows:
1. Tuple: It represents a single row of a table, which contains a single record for that relation.
2. Relation instance: It represents a finite set of tuples in the relational database system.
3. Relation schema: It represents the relation name, that is, table name, attributes and their names.
4. Relation key: It represents the unique key for the relation or table. Each row has one or more attributes,
which can identify the row in the table uniquely.
5. Attribute domain: It represents the predefined value scope of each attribute.
6. Attribute: Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc.
7. Degree: The total number of attributes which in the relation is called the degree of the relation.
8.Cardinality: Total number of rows present in the Table.
4
Cloud IT Online Lecture Sheet
Constraints in Relational Model
Constraints are the restrictions that one wishes to apply on database. The following constraints are applied on
relational model.
1. Key constraints: Each relation has at least one minimal subset of attributes, which can identify a tuple
uniquely.
• No two tuples have identical value for key attributes.
• Key attribute does not have NULL value.
2. Domain constraints: Attributes have specific domain values in real world. For example, value of age can
only be positive.
3. Referential integrity constraints: If a relation refers to a key attribute of a different relation then that key
element must exist.
Relational Algebra and SQL discus later
5
Cloud IT Online Lecture Sheet
Entity:
Entities represent the real-world things. These are data objects which maintain different relationships with
each other, for example, Employee, Department, etc. An entity can be place, person, object, event or a concept,
which stores data in the database. These are represented by means of rectangles.
Examples of entities:
Person: Employee, Student, Patient
Place: Store, Building
Object: Machine, product, and Car
Event: Sale, Registration, Renewal
Concept: Account, Course
Entity set is a collection of similar types of entities. For example, all Employees set may contain all
employees of all departments. Weak entity depends on the existence of another entity. A weak entity cannot be
identified by its own attributes. It uses a foreign key combined with its attributes to form the primary key. It is
represented by means of double rectangles.
Attribute
Attribute is a property, trait or characteristic of an entity, relationship or another attribute. All attributes have
values. A domain or range of values can be assigned to attributes. For example, name, class, age are attributes
of the entity student. These are represented by ovals.
For example
1. Simple attribute: Simple attributes contains atomic values. Atomic values cannot be divided into sub parts.
Examples are mobile number, roll number
2. Composite attribute: Composite attributes are composition of many simple attributes. For example address
can be divided into house number, street number, locality and city.
6
Cloud IT Online Lecture Sheet
3. Derived attribute: Derived attributes are those whose value is derived from some other attribute in the
database. For example, age of person can be calculated from date of birth. Dotted oval is used to represent
derived attributes.
4. Single-valued attribute: Single valued attributes contain only one value for that attribute. For example age
for person, blood group.
5. Multi-value attribute: In this, an attribute may contain more than one value. Multiple values are represented
by double ovals. For example contact number, email ids.
Relationship
Relationship is nothing but an association among two or more entities. E.g., Tom works in the Chemistry
department. Entities take part in relationships. We can often identify relationships with verbs or verb phrases.
For example:
You are attending this lecture
A student attends a lecture
A lecturer is giving a lecture.
1. Relationship set: Relationship of similar type is called relationship set. It has attributes. These attributes are
called descriptive attributes.
2. Degree of relationship: It defines the number of participating entities in a relationship. They are of the
following types:
Unary relationship (Degree 1): One entity participates. For example,
7
Cloud IT Online Lecture Sheet
Binary relationship (Degree 2): Two entities participate. For example,
3. Cardinality: It defines the number of instances of an entity, which can be associated to the number
of instances of other entity via relationship set.
• One to one: One entity from an entity set is related to one entity in another entity set . It is represented as `1-
1‟. For example
One to many: One instance of an entity is associated with more than one instance of another entity
with the relationship. It is represented as `1-N‟. For example
Many to one: More than one instance of an entity is associated with another entity with the
relationship. It is represented as `N-1‟. For example
Many to many: More than one instance of an entity is associated with more than one instance of
another entity with the relationship. It is represented as `N-N‟. For example
4. Generalization: It is a collection of entity sets, having similar characteristics, brought together into
one generalized entity. For example, salaried and contract employees are generalized as employee.
5. Specialization: It is the process of identifying subsets of an entity set. It is a reverse process of
generalization.
8
Cloud IT Online Lecture Sheet
To design an ER Diagram consists of the following steps:
1. Identifying entities
2. Identifying relationships
3. Identifying attributes
4. Presenting entities and relationships: Entity relationship diagram (ERD)
5. Assigning keys
6. Defining the attribute‟s data type
7. Normalization
See the following example. It will help you to design an ER diagram from scratch.
Example 1: Draw an ER model of the Banking database application considering the following
constraints. Draw an E-R Diagram using the following constrain:
A bank has many entities.
Each customer has multiple accounts.
Multiple customers belong to a single branch.
Single customer can borrow multiple loans.
A branch has multiple employees.
9
Cloud IT Online Lecture Sheet
Step 4 − Identify the relationship between entity sets
One customer is enrolled by multiple accounts and one account for multiple customers. Hence, the
relationship is many to many.
Customer M N Account
Depositor
Many customers belong to one branch but one branch belongs to many customers. Hence, the
relationship between customer and branch is many to one.
One customer can borrow multiple loans in the same way multiple loans can borrow a single
customer, hence the relationship between customer and loan is one to many.
One branch has many employees and in the same way the number of employees works in a single
branch.
10
Cloud IT Online Lecture Sheet
Types of Keys
1. Super Key
A super key for an entity is a set of one or more attributes whose combined value uniquely identifies the
entities in the entity set. e.g., for an entity set employees, the set of attributes (emp_name, address) can be
considered to be a super key, if we assume that there are no two employees with the same name emp_name
and same address.
2. Primary Key
The primary key of a relation can be said to be a minimal super key. The field or group of fields which forms
the unique identifier for a table is called the table's primary key. The primary key uniquely identifies each
record in the table and must never be the same for two records. .g., emp_code can be primary key for the
entity set employees. The primary key should be chosen such that its attributes are never or very rarely
changed. For instance, the address field of a person should not be part of the primary key, since it is likely to
change. Emp_code, on the other hand, is guaranteed to never change, till he is in the organization.
3. Candidate Key
There is only one primary key in a table. But there can be multiple candidate keys. A candidate key is an
attribute or set of attributes that uniquely identifies a record. These attributes or combinations of attributes are
called candidate key. In such a case, one of the candidate key is chosen to be a primary key. The remaining
candidate keys are called alternate keys.
4. Composite Key
In many cases, as we design a database, we will have tables that will use more than one column as part of
the primary key. These are called composite keys or concatenated keys. In other words, when a record
cannot be uniquely identified by a single field, in such cases a composite key is used. A composite key is a
group of fields that uniquely identify a record.
5. Foreign Key
In a relation, column whose data values correspond to the values of a key column in another relation is called
foreign key. In a relational database, the foreign key of a relation would be the primary key of another relation.
নিচের Table নি দেনি। এই Table দেচে েীভাচে নেনভন্ন key দের েরা যায় দেিাই দেিচোোঃ
Sid Name Marks Dept Course
1 A 78 CS C1
2 B 60 EE C2
3 C 65 CS C3
4 D 75 EE C4
5 E 80 IT C5
SQL এর মোধ্যলম প্রলতটি column কক আোদো করলত পোরলবো লকন্তু প্রলত row কক আোদো করোর জনয
আমোলদর key োগলব অবলযই।
এখোলন Sid একটি key, key একটি attribute হলত পোলর বো অলনকগুলো attribute লমল হলত পোলর। তোই
এখোলন কদলখন {Sid} এটো প্রলতটো row কতই আোদো তোই কে একটো key, {Sid, Dept} এটোও একটো key
হলত পোলর, লকন্তু {Name, mark} key হলত পোলরনো। কোরণ অলনলকর name, mark একই হলত পোলর।
11
Cloud IT Online Lecture Sheet
Super key: Attribute or set of attribute that uniquely identified each record of relation. এখোলন {Sid},
{Sid, name} েবোই supper key কোরণ Sid হলো super key তোই এর েোলথ ককোন attribute add করল
কেগুলোও super key হলয় যোলব। Total super key possible= 2n-1
উত্তরঃ
A B C D
1 1 5 1
2 1 7 1
3 1 7 1
4 2 7 1
5 2 5 1
6 2 5 2
এখোলন super key = {A}, {AB},{AC} {AD} {ABC} {ACD} {ABD} {ABCD} = 8 super key.
লকন্তু {BCD} =super key হলব নো কোরণ তোরো uniquely identified নো। তোই তোলদর subset গুলো super
key হলব নো। বোস্তলব super key হলো theoretical concepts.
Candidate Key:
Condition 1: Super key whose proper subset is not a super key
Condition 2: Minimal Super key.
Proper Subset: Set A is considered to be a proper subset of set B if set B contains at least one element that
is not present in set A. Here B={12,24,36}, A={12,24} Here A is the proper subset of B because 36 is not
preset in set A.
Here,
Set = {A}, {AB}, {AC}, {ABC}, {BC}
এখন কদলখন কীভোলব এগুলো কথলক Super key আর Candidate Key বোনোই।
12
Cloud IT Online Lecture Sheet
Set Super key Candidate key মন্তবয
{ABC} yes No ABC এর proper subset AB, AC, A, B, C যোলত
A লনলজই Super key. তোই condition 1 অনুযোয়ী এটো
candidate key নো।
{AC} yes No AC এর proper subset A, C যোলত A লনলজই
Super key. তোই condition 1 অনুযোয়ী এটো
candidate key নো।
{A} yes yes A এর proper subset {}. তোই condition 2
অনুযোয়ী এটি Candidate key.
{BC} yes yes BC এর proper subset B, C যো super key নো।
তোই BC Candidate key
Primary Key: তোহল candidate key এর মলধ্য যোর ককোন null value নোই কেটি হলব primary key.
Previous Year Questions
13
Cloud IT Online Lecture Sheet
14
Cloud IT Online Lecture Sheet
General Definition of 2 NF and 3NF
The previous definitions were considered with respect to partial and full functional dependencies and
transitive dependencies on primary key. Now, these will be considered w.r.t. all candidate keys of a
relation.
2 NF A relation schema R is in 2 NF, if every non-prime attribute A in R is fully functionally dependant on
every key of R.
3 NF A relation schema R is in 3 NF, if whenever a non-trival functional dependency X -> A holds in R
either
(a) X is a super key of R Or
(b) A is a prime attribute of R.
Functional Dependency
A function dependency is a constraints between two set of attributes from the database suppose that our
relational database schema has n attributes A1, A2,……,An. Let us think at whole database as being
described by a single universal relation schema,
R= { A1, A2,……,An }
Definition
A functional dependency x->y between two set at attribute X and Y that are subset of R specifies a constraint
on possible tuple that can form state r of R the constraints is that for any two tuple, t1, and t2 in r if t1[x]= t2[y]
then t1[y]= t2[y]
So we can say that the value of X component at tuple uniquely determines the values at the Y component or
we can say that there is functional dependency from X to Y or Y is functionally dependent on X .
Normalization of Relation
Normalization process takes a relation schema through series of tests of satisfy whether it satisfied a
certain normal form.
1. First normal form
2. Second normal form
3. Third normal form
4. BCNF (Boyce-Codd Normal Form)
5. Fourth normal form
6. Fifth normal form
First Normal Form (1NF)
The following criteria‟s must be satisfied for 1NF.
Each table cell should contain a single value.
No composite values
All entries in any column must be of the same kind
Each column must have a unique name
No two rows are identical
Student Table
Student Age Subject
Fuad 15 Biology, Maths
Akas 14 Maths
Jibon 17 Maths
15
Cloud IT Online Lecture Sheet
In First Normal Form, any row must not have a column in which more than one value is saved, like
separated with commas. Rather than that, we must separate such data into multiple rows. Student Table
following 1NF will be-
Student Age Subject
Fuad 15 Biology
Fuad 15 Maths
Akas 14 Maths
Jibon 17 Maths
Id Student Age
1 Adam 15
2 Alex 14
3 Stuart 17
In Student Table, the primary key will be Id column, and the foreign key for the Stu_subject table is Id
which is referenced bt Student table.
Id Student Subject
1 Adam Biology
1 Adam Maths
2 Alex Maths
3 Stuart Maths
Now, if somebody want‟s to insert a subject in Stu_subject table which does not exist in Student table
then an error will be shown!
Third normal form (3 NF)
Third normal form (3 NF) is based on concept of transitive dependency. A functional dependency x ->y is a
relation schema R is a transitive dependency, if there is set of attribute z that is neither a candidate key nor a
subset of any key of R and x -> z and x -> y holds.
16
Cloud IT Online Lecture Sheet
The dependency ENO-DNO hold and DNO-DNAME DMGRND, so transitive dependency holds so, we have
to normalize the table.
3NF accourding to codd‟s original definition a relation schema R is in 3 NF, if it satisfies 2NF and no non-
prime attribute at R is transitively depends on primary key.
Transaction Management
Concurrency
Concurrency is the ability of a database system to handle simultaneously a number of transactions by
overlapping or interleaving parts of their actions. The goal is to service many users and to provide better
throughout by sharing resources (especially by letting another transaction use the CPU when a transaction is
waiting for a time consuming input-output operation).
Transaction property
ACID Property [RAKUB-ADA-2021] [ADBA-2022][MilkVita-23]
17
Cloud IT Online Lecture Sheet
(i) Atomicity
It ensures that either all operations of the transactions are reflected properly in the database or none are.
This property is maintained by transaction management component of DBMS.
e.g.,
T1: read (A) A = A+100
write (A); B = B-100
write (B);
• Here, read (A) is the operation in which the transaction reads the databases item A into a program variable,
Write (A) is the operation in which transaction writes the value of a program variable A into the database
item named A.
• So, if the above transaction fails at a point let say after executing B = B - 100, then it is to be made sure that
the value of A will also not be updated in the database. This is to ensure the property of atomicity and we
need to roll back the transaction.
ii) Consistency
A transaction should take the database from one consistent state to another.
• It is the duty of application programmer, take into consideration all the constraints
If the database is in a consistent state before execution of the transaction, it will be in consistent state
after complete execution of transaction, assuming that no interference with other transaction occurs.
(ii) Isolation
It says that execution of a transaction should not be interfered by any other transaction that is executing
concurrently.
• For pair of transaction (Ti, Tj) executing a concurrently, it must be ensured that, it should appear to Ti,
that either Ti, finished executing before Ti, started or Tj started execution after Ti, finished.
Transaction States
Active: The initial state, the transaction stays in this state while it is executing.
Partially committed: After the final statement has been executed.
18
Cloud IT Online Lecture Sheet
Failed: After the delivery, the normal execution can no longer proved.
Aborted: After the transaction has been rolled back and the database has been restored to its state prior to the
state of the transaction.
Committed: After successful completion a transaction is said to have terminated, if it has either committed or
aborted.
1. A transaction consists of a sequence of query and/or update statements. SQL statement must
be required to end the transaction. List the SQL statements, required to end the transaction
and also write their functions.[officer(SBL-JBL-IT)-2020]
2. Write down the details of ACID properties.[RAKUB-ADA-2021] [ADBA-2022][MilkVita-23]
3. Describe the ACID properties in a Database. When does a deadlock occur, and how do you
prevent it, in a database? [Combined(SO-IT/ICT)-2019] [Basic bank (AM)-2019] [BDBL-2017]
RAID 0
RAID 1
RAID 5
RAID 10 (also known as RAID 1+0)
This article explains the main difference between these raid levels along with an easy to understand diagram.
In all the diagrams mentioned below:
A, B, C, D, E and F – represents blocks
p1, p2, and p3 – represents parity
RAID LEVEL 0
Minimum 2 disks.
Excellent performance (as blocks are striped).
No redundancy ( no mirror, no parity ).
Don‟t use this for any critical system.
19
Cloud IT Online Lecture Sheet
RAID LEVEL 1
Minimum 2 disks.
Good performance ( no striping. no parity ).
Excellent redundancy ( as blocks are mirrored ).
RAID LEVEL 5
Minimum 3 disks.
Good performance ( as blocks are striped ).
Good redundancy ( distributed parity ).
Best cost effective option providing both performance and redundancy. Use this for DB that is heavily
read oriented. Write operations will be slow.
RAID LEVEL 10
Minimum 4 disks.
This is also called as “stripe of mirrors”
Excellent redundancy ( as blocks are mirrored )
Excellent performance ( as blocks are striped )
If you can afford the dollar, this is the BEST option for any mission critical applications (especially
databases).
20
Cloud IT Online Lecture Sheet
STRUCTURED QUERY LANGUAGE (SQL)
In common usage SQL also encompasses DML (Data Manipulation Language), for INSERTs, UPDATES,
DELETEs and DDL (Data Definition Language), used for creating and modifying tables and other database
structure when an SQL statement is entered, it is stored in a part of memory called the SQL buffer and
remains there until a new SQL statement is entered.
Features of SQL
1. SQL can be used by a range of users, including those with little or no programming experience.
2. It is non-procedural language.
3. It reduces the amount of time required for maintaining systems.
4. It is English like language.
Components of SQL
1. DDL (Data Definition Language)
It is set of SQL commands used to create, modify and database structure but not data. These commands
are normally not used by a general user, who should be accessing the database via an application. They
are normally used by the DBA to a limited extent, a database designer or application developer
2. DML (Data Manipulation Language) It is the area of SQL that allows changing data within
the database.
3. DCL (Data Control Language) it is the component of SQL statement that control access to data and the
database occasionally DCL statements are grouped with DML statements.
4. DQL (Data Query Language) It is the component of SQL statement that allows getting data from the
database and imposing ordering upon it. It includes the SELECT statement.
21
Cloud IT Online Lecture Sheet
CREATE
ALTER
DROP
TRUMCATE
COMMENT
GRANT
REVOKE
DML : Data Manipulation Language Statements
INSERT
UPDATE
DELETE
CALL
EXPLAIN PLAN
LOCK
DCL : Data Control Language Statements
COMMIT
SAVE POINT
ROLL BACK
SET TRANSACTION
GRANT/REVOKE
DQL : Data Query Language Statement
SELECT
Date Types
1. VARCHAR2 (size) It is variable length character string having maximum length „Size' bytes. Maximum
size is 2000.
2. CHAR (size) It specifies a fixed length character string. Maximum size is 255. Default size is 1 byte.
If you insert a value that is shorter than the column length, ORACLE black-pads the value of column
length. Trying to insert a value too large for column returns an error.
3. VARCHAR (size) VARCHAR data type is currently synonymous with VARCHAR2 data type.
ORACLE corporation recommends use of VARCHAR rather than VARCHAR2 as meaning of
VARCHAR may change in future versions.
22
Cloud IT Online Lecture Sheet
4. NUMBER (P,S) It is used to store fixed or floating point numbers ranging from 1.0* 10-130 to 9.99*
10125 P-precision or total number of digits range 1 to 38. S-scale or number of digits to right of decimal
point.
5. LONG It stores variable length character strings containing upto 2 gigabytes LONG columns can be
referenced in SQL statements in these places.
SQL Commands
23
Cloud IT Online Lecture Sheet
Suppose you want to display records with NAME values Khilan, Hardik and Muffy from the
CUSTOMERS table, you can use the following query
SELECT * from CUSTOMERS WHERE NAME IN ('Khilan', 'Hardik', 'Muffy');
LIKE condition
The LIKE operator is used to list all rows in a table whose column values match a specified pattern. It is useful
when you want to search rows to match a specific pattern, or when you do not know the entire value. For this
purpose, we use a wildcard character '%'.
The LIKE condition is used to specify a search for a pattern in a column. A '%' sign can be used to define
wildcards (missing letters in the pattern) both before and after the pattern.
SELECT column FROM table WHERE column LIKE pattern
24
Cloud IT Online Lecture Sheet
Finds any values that start with "joh"
SELECT * FROM EmployeeDetails WHERE FullName LIKE'joh%'
Finds any values that end with "joh"
SELECT * FROM EmployeeDetails WHERE FullName LIKE'%joh'
Finds any values that any position with "joh"
SELECT * FROM EmployeeDetailsWHERE FullName LIKE '%joh%’
Finds any values that have „h‟ in the second position
SELECT * FROM EmployeeDetails WHERE FullName LIKE '-h%’
Natural Join
Used to join tables on the basis of a common column
Cartesian Join
Returns the Cartesian product of tables‟ rows
Inner Join
The result table consists of only the common rows
Left Outer Join
The result table contains all the rows from the first table
Right Outer Join
The result table contains all the rows from the second table
Full Outer Join
The result table contains all the rows from both the tables
Self Join
Used to join a table to itself
Aggregate Function in SQL
An aggregate function in SQL performs a calculation on multiple values and returns a single value. SQL
provides many aggregate functions that include avg, count, sum, min, max, etc. An aggregate function ignores
NULL values when it performs the calculation, except for the count function. We often use aggregate
functions with the GROUP BY and HAVING clauses of the SELECT statement.
25
Cloud IT Online Lecture Sheet
There are 5 types of SQL aggregate functions:
Count()
Sum()
Avg()
Min()
Max()
Count():
Count(*): Returns the total number of records .
Count(salary): Return the number of Non-Null values over the column salary.
Count(Distinct Salary): Return the number of distinct Non-Null values over the column salary
Sum():
sum(salary): Sum all Non-Null values of Column salary
sum(Distinct salary): Sum of all distinct Non-Null values
Avg():
Avg(salary) = Sum(salary) / count(salary)
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary)
Min():
Min(salary): Minimum value in the salary column except NULL
Max():
Max(salary): Maximum value in the salary
GROUP BY Statement
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number
of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
Syntax: SELECT column_name(s) FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Having Clause
The HAVING clause is associated with grouping queries and aggregate queries. It is optional in both cases.
In grouping queries, it follows the GROUP BY clause. In aggregate queries, HAVING follows the WHERE
clause or the FROM clause, if the WHERE clause is missing.
The HAVING clause has the following general format: Having predicate Like the WHERE clause, HAVING
filters the query result rows. WHERE filters the rows from the FROM clause. HAVING filters the grouped
rows (from the GROUP BY clause) or the aggregate row (for aggregate queries).
Predicate is a logical expression referencing grouped columns and set functions. If the having predicate
evaluates to true for a grouped or aggregate row, the row is included in the query result, otherwise, the row is
skipped (not included in the query result)
SELECT sno, COUNT(*) parts FROM sp GROUP BY sno HAVING COUNT(*) > 1
26
Cloud IT Online Lecture Sheet
SQL Subquery
An SQL subquery is nothing but a query inside another query. We use a subquery to fetch data from two
tables. A subquery is often also referred to as an inner query, while the statement containing a subquery is also
called an outer query or outer select. We can implement subqueries with the SELECT, INSERT, UPDATE,
and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
General Rules of Subqueries
There are some rules which are followed while using subqueries. They are:
A subquery must be enclosed in parentheses.
Subqueries that return over one row can only be used with multiple value operators such as the IN
operator.
SQL Server allows you to nest subqueries up to 32 levels.
Super Key: {roll_no}, {Registration_no}, {roll_no, name}, {roll_no,class}, {roll_no, age}, {Registration_no,
name },……..
27
Cloud IT Online Lecture Sheet
3) consider the table [BAPEX-BUET-2023]
Employee(id, name, salary)
Equipment (id, name, price)
Hire(employee_id, equipment_id)
a) Draw the E-R diagram for the relation
4) employee (employee_NID, emp_id, emp_name, emp_mobile). Employee doesn‟t share mobile number to
other employee. How many super key and candidate key are there? [NPCBL-JET-2022]
Total super key= 2n-3 .
Candidate Key= { employee_NID }, { emp_id }, { emp_mobile }
Let a Relation R have attributes {a1, a2, a3,…, an} and the candidate key is “a1 a2 a3” then the
possible number of super keys?
Following the previous formula, we have 3 attributes instead of one. So, here the number of possible super
keys is 2(N-3).
7. Let you are created the following two table into your database.
Combined Assistant Programmer-10/02/2024 (SBL, BKB, KB, ICB)]
Create table DWPARTMENT Create table DWPARTMENT
( (
DEPTCODE int(10), EmpCode int(4),
DeptNAme CHAR (30), EmpLName Varchar(17),
LOCATION VARCHAR(33) Job Varchar(17),
); Manager CHAR(4)
ItineDate DATE,
Salary INT(6),
Commission INT(^),
DEPTCODE INT(2)
);
28
Cloud IT Online Lecture Sheet
a) Display EMPFNAME and “Total Salary” for each employee.
[Hints: Total salary is the sum of salary and Commission]
b) Display EMPFNAME, EMPLNAME, DEPTNAME, LOCATION from EMPLOYEE and
DEPARTMENT tables. Make sure the result are in ascending order are on the EMPFNAME and
LOCATION of the department.
c) Find the average salary of each employee in each department.
d) Return a list of all employees who are paid above the average salary.
e) List of all departments, ordered by the number of employes in each department.
DEPARTMENT
Department Code Department Name
EMPLOYEE
EMPNAME SALARY COMMISSION
8. SQL [Bakhrabad Gas field- AE-BUET-15/03/2024]
Employee(empid, empname, empjob, empmsal, deptId)
Department(deptId, location,deptName)
a) Find employee who‟s salary (empmsal) is equal to „Rahium‟ but not in the same company.
b) List the deptname which employee avg salary is greater than the company avg salary.
10. Suppose that we have a relational database with the following tables. Underlined one represent
primary key. [Bangladesh Bank AP 2023]
Movies(mid, title, year)
People (pid, name)
Genres(gid, genre)
HasRole(pid, mid, role)
HasGenre (gid, mid)
Write a sql query to return the number of movies that are romantic comedies.
11. Relational database with 5 tables. [Combined 5 Banks & FIs ,AP 2023 ]
S ( sid, A)
T ( tid, B)
U ( uid, C)
R ( sid, tid, D)
Q ( tid, uid, E)
R many to many relation with S and T and
Q many to many relation with T and U.
Now, write a sql command to return all the sid and uid from relation R and Q. Use only select but not
use select distinct.
29
Cloud IT Online Lecture Sheet
12. A book composed number of parts, composed number of chapters, and composed number of
sections . [Combined 5 Banks & FIs ,AP 2023]
A book includes publisher, publication date, ISB.
A part includes a title, a number.
A chapter includes a title, a number, and an abstract.
A section includes a title, a number.
Draw class diagram
30
Cloud IT Online Lecture Sheet
c) List all the patients admitted today.
SELECT p.patientNo, p.patName
FROM Patient p, Contains c
WHERE c.patientNo= p.patientNo AND admission Date = 'today'
d) Find the names of all the patients being prescribed „Napa‟.
SELECT p.patName
FROM Patient p, Prescribed pr, Drug d
WHERE pr.patientNo= p.patientNo & pr.drugNo= d.drugNo & drugName = „Napa‟.
e) What is the total cost of Napa supplied to a patient called 'John Smith'?
SELECT SUM(((finishDate- startDate)* unitsPerDay) * costPerUnit)
AS totalCost FROM Patient p, Prescribed pr, Drug d
WHERE pr.patientNo= p.patientNo AND pr.drugNo= d.drugNo AND drugName = „Napa‟
AND patName = 'John Smith'
f) What is the maximum, minimum and average number of beds in a ward? Create appropriate column
headings for the results table.
SELECT MAX(noOfBeds) AS Maximum, MIN(noOfBeds) AS Minimum, AVG(noOfBeds) AS
Average FROM Ward.
g) For each ward that admitted more than 10 patients today, list the ward number, ward type and
number of beds in each ward.
SELECT w.wardNo, wardType, noOfBeds
FROM Patient p, Ward w, Contains c
WHERE w.wardNo= c.ward No AND c.patientNo= p.patientNo AND admissionDate = 'today'
GROUP BY wardNo, wardType, noOfBeds HAVING COUNT(*) > 10
26. See the following Database table and answer the queries. [BCIC-AP-2022]
Customers
Cus_id Cus_name Contact District
1 Akas Monju Dhaka
2 Hridoy Milon Khulna
3 Jibon Shipon Feni
a) Sorting the district column and show the customer name.
SELECT Cus_name
From Customers
ORDER BY District ASC;
b) Change the customer name “Rayhan” those districts is Dhaka.
UPDATE Customers
SET Cus_name=‟Rayhan‟
Where District=’Dhaka’;
27. See the following Database table and answer the queries. [NWPGCL-2022]
emp_info (emp_id,emp_name,salary,dept_id)
department (department_id,dept_name,location_id)
locations(localtion_id,location_city)
a) Show the information of all employees who live in Dhaka cities and salary more than 20000.
SELECT * from emp-info e, department d, locations l
31
Cloud IT Online Lecture Sheet
Where e.dept_id=d.department_id and d.location_id=l.location_id and l.location_city=‟Dhaka‟
and e.salary>=20000.
b) Update emp_name As Mr. x where emp_id=21.
UPDATE emp_info
Set emp_name=‟Mr. X‟
Where emp_id=21;
28. Query for retrieving uncommon name from name column of two given table [RAKUB-ADA-2021]
29. Query to find out even number row from given table.[RAKUB-ADA-2021]
Select * from table where id % 2 = 0
30. Difference: Primary key vs unique key, Shared vs Exclusive lock, Drop vs Drop purge, Delete vs
truncate.[RAKUB-ADA-2021]
31. What is inner join? Explain with syntax and example. [BTV (AP)-2019]
32. What is cardinality and modality? [BB-AP-2016]
33. Scenario: Batch Processing [Combined bank –SO (IT-222 post) 2023]
In this scenario, there are three tables in a database. The 'Students' table contains the roll numbers and
name of the students in a class, with potentially many students enrolled. The 'Result' table contains
information about the subjects chosen by students, including credit hours and the grades obtained by each
student. A student has the flexibility to enroll in as many subjects as he/she prefers. The 'Grade' table will
be populated through a program designed to process the final Cumulative Grade Point Average (CGPA)
for all students.
To calculate CGPA, the program will sum the products of the credit hours and GPA for cach student and
then divide this sum by the total credit hours studied by the students. By using the knowledge of a
programming language and database system write an appropriate program to prepare the final result and
store in the Grade table.
Student
Roll Name
R101 Md. Hafizur Rahman
R206 Sharmeen akter
R334 Abdulla
R543 Sheems voumic
Result
Roll Subject Credit GPA
R101 Physics 3 3.5
R101 Computer 2 4
R206 Physics 3 3
R334 Biology 6 3.75
Grade
Roll Name CGPA
32