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

Relational Data Model

Uploaded by

Sudha G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Relational Data Model

Uploaded by

Sudha G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

RELATIONAL MODEL

 Relational Model Concepts


 Characteristics of relational model
 Relational Model constraints
 Key constraints
 primary and foreign key constraints
 integrity constraints
 Null values
 Relational Algebra
 Operations
 Set theory operations
 Joining Operations
 Aggregate Functions
The relational model in DBMS is an abstract model used to organize and manage the
data stored in a database.
It stores data in two-dimensional inter-related tables, also known as relations in which
each row represents an entity and each column represents the properties of the entity.

The relational model for database management is an approach to logically represent


and manage the data stored in a database.
In this model, the data is organized into a collection of two-dimensional inter-related
tables, also known as relations.
Each relation is a collection of columns and rows, where the
column represents
The attributes of an entity and the rows (or tuples) represent the records.
The use of tables to store the data provided a straightforward, efficient, and flexible
way to store and access structured information.
Because of this simplicity, this data model provides easy data sorting and data
access.
Hence, it is used widely around the world for data storage and processing.

USN NUMBER NAME CGPA


U03PE22S010 Asha 9.33
U03PE22S011 Banu 9.34 TUPLE
U03PE22S012 Chitra 9.6 (Record )
U03PE22S013 Deepu 9.99

Black Background for Business


Attribute 3
Black Background for Business 4
Characteristics of a Relational Model

The following properties define a relational database:

Each row is unique.


The values in a column are of the same data type.
The values are atomic.
The sequence of columns is not significant.
The sequence of rows is not important.
Each column's name is unique.

Black Background for Business 5


Relational Model Concepts
A relational database is based on the relational model. This database consists of various components
based on the relational model. These include:

Relation: Two-dimensional table used to store a collection of data elements.


Tuple: Row of the relation, depicting a real-world entity.
Attribute/Field: Column of the relation, depicting properties that define the relation.
Attribute Domain: Set of pre-defined atomic values that an attribute can take i.e., it describes the
legal values that an attribute can take.
Degree: It is the total number of attributes present in the relation.
Cardinality: It specifies the number of entities involved in the relation i.e., it is the total number of
rows present in the relation

Black Background for Business 6


Black Background for Business 7
Relational Schema: It is the logical blueprint of the relation i.e., it describes the design and the
structure of the relation. It contains the table name, its attributes, and their types:

TABLE_NAME(ATTRIBUTE_1 TYPE_1, ATTRIBUTE_2 TYPE_2, ...)

For our Student relation example, the relational schema will be:

STUDENT(USN_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)

Relational Instance: It is the collection of records present in the relation at a given time.
Relation Key: It is an attribute or a group of attributes that can be used to uniquely
identify an entity in a table or to determine the relationship between two tables. Relation
keys can be of 6 different types:
Candidate Key
Super Key
Composite Key
Primary Key
Alternate Key
Foreign Key

Black Background for Business 8


A Relation is a collection of rows (tuples) and columns (attributes).
In a relation, the tuples depict real-world entities, while the attributes are the
properties that define the relation.
The structure of the relation is described by the relational schema.
Relational keys are used to uniquely identify a row in a table or to determine the
relationship between two tables.
Integrity Constraints in DBMS
In Database Management Systems, integrity constraints are pre-defined set of rules
that are applied on the table fields(columns) or relations to ensure that the overall
validity, integrity, and consistency of the data present in the database table is
maintained.
Evaluation of all the conditions or rules mentioned in the integrity constraint is
done every time a table insert, update, delete, or alter operation is performed. The
data can be inserted, updated, deleted, or altered only if the result of the constraint
comes out to be True.
Thus, integrity constraints are useful in preventing any accidental damage to the
database by an authorized user.
Constraints in Relational Model
Relational models make use of some rules to ensure the accuracy and accessibility of
the data.
These rules or constraints are known as Relational Integrity Constraints. These
constraints are checked before performing any operation like insertion, deletion, or
updation on the data present in a relational database.
These constraints include:
Domain Constraint: It specifies that every attribute is bound to have a value that
lies inside a specific range of values. It is implemented with the help of the
Attribute Domain concept.
A domain is a unique set of values present in a table. In a table student which
consists of 3 attributes as NAME, USN_Number, and CGPA. Now ROLL NO
attributes can have only numbers associated with them and they won't contain any
alphabet.
Key Constraint: It states that every relation must contain an attribute or a set of
attributes (Primary Key) that can uniquely identify a tuple in that relation. This
key can never be NULL or contain the same value for two different tuples.

Referential Integrity Constraint: It is defined between two interrelated tables. It


states that if a given relation refers to a key attribute of a different or same table,
then that key must exist in the given relation.

Highlights:
1.To ensure data accuracy and accessibility, Relational Integrity
Constraints are implemented.
2.It includes domain, key, and referential integrity constraints.
Insertion Anomalies: It is the inability to insert data in the database due to the absence
of other data. For example: Suppose we are dividing the whole class into groups for a
project and the GroupNumber attribute is defined so that null values are not allowed. If a
new student is admitted to the class but not immediately assigned to a group then this
student can't be inserted into the database.
Deletion Anomalies - It is the accidental loss of data in the database upon deletion of any
other data element. For example: Suppose, we have an employee relation that contains
the details of the employee along with the department they are working in. Now, if a
department has one employee working in it and we remove the information of this
employee from the table, there will be a loss of data related to the department also. This
can lead to data inconsistency.
Modification/Update Anomalies - It is the data inconsistency that arises from
data redundancy and partial updation of data in the database.
For example: Suppose, while updating the data into the database duplicate entries
were entered. Now, if the user does not realize that the data is stored redundantly
after updation, there will be data inconsistency in the database.
Entity Integrity Constraint
Entity Integrity Constraint is used to ensure that the primary key cannot be null.
A primary key is used to identify individual records in a table and if the primary
key has a null value, then we can't identify those records.
There can be null values anywhere in the table except the primary key column.

ID Name Salary
101 XXX 60000
102 Yyyy 70000
103 Ssss 80000
Zzzz 90000
Referential Integrity Constraint
Referential Integrity Constraint ensures that there must always exist a valid
relationship between two relational database tables.
This valid relationship between the two tables confirms that a foreign key exists in a
table.
It should always reference a corresponding value or attribute in the other table or be
null.
ID Name Salary Dept_ID Dept_ID Dep_Name
101 XXX 60000 1 1 CSC
102 Yyyy 70000 2 2 MECH
103 Ssss 80000 3 3 CIVIL
104 Zzzz 90000 1 4 ELEC
What are Keys?

The main point of a database is to store data to be retrieved easily in the future.
During retrieval, it would be convenient to get back relevant data based on as few
parameters as possible.
Example, in a database of Students , it would be hard to find a student based on his
grades or age. A slightly better method would be to query the student’s name.
However, in many cases, more than one student can have the same name. To
distinguish between these students, we use the concept of roll numbers.
no two students can have the same roll number, which means, we can
uniquely identify all students from their roll numbers.
Keys in DBMS are just that.
A key is an attribute or a set of attributes that can uniquely identify an entire row in a database.
In simple databases, one attribute is enough to uniquely identify all rows. However, that’s not
always the case. Consider the following example,
There are 3 sections (A, B, C) in the college for commerce. Based on the alphabetic order, the
students are given a roll number, which uniquely identifies them
in a classroom. However, since there are 3 such sections, there will be
many students who have the same roll number but are in different
sections. In this case, we can uniquely identify a student using their
section and roll numbers together. For example, (5B, 12) represents

exactly one student, who is in 5B and has the roll number 12.
There are various classes of keys in DBMS. Some of them are:
1) Super Keys
A Super Key is essentially just a key, i.e. it can uniquely identify all the attributes in a
database (uniquely identify all rows in a relational database).
The main purpose of a super key is just to identify rows in the table. In
many cases, you can't identify a table with any random column, since a
column with duplicates will not be able to identify a unique row.
Super Keys remove this ambiguity and make data retrieval easy.
Consider a database that stores customer orders and the products they have
purchased. In this scenario, the super key could be a composite key made up of the
customer ID and the product ID, as this would ensure that each customer order is
uniquely identified based on the products they have purchased.
Another example could be a database that stores orders placed by customers,
including the order number, customer name, and order date. In this case, the super
key could be the combination of order number and order date, as this would ensure
that each order is uniquely identified.
Create table class( section char(1), roll_number number(1), first_name
varchar(20),last_name(varchar(20));

Section Roll_number First_name Last_name


A 101 SSS GGG
B 101 CCC GGG
A 102 DDD GGG
B 102 RRR GGG
There are various classes of keys in DBMS. Some of them are:
1) Super Keys
A Super Key is essentially just a key, i.e. it can uniquely identify all the attributes in a
database (uniquely identify all rows in a relational database).
2) Composite Keys
A Composite Key is a key that contains more than one attribute. In the student table
mentioned above, the key – (Section, Roll Number) is a Composite Key. This key can
contain any number of attributes (greater than 1). Trivially, the key involving all the
columns in the table is the largest Composite Key possible.
3) Candidate Keys
A Candidate Key is a key that contains the least possible attributes, and that maintains the
criteria that it can uniquely identify any table row. Again, in the student table mentioned
above, Roll Number cannot be a candidate key, since it cannot identify a student across 5th
grade. Similarly, the key (Section, Roll Number, Name) cannot be a candidate key since we can
make do with (Section, Roll Number) as a key, which has 1 less attribute. Therefore, the
key (Section, Roll Number) is a candidate key.
4) Primary Key
One candidate key from the set of all possible candidate keys is chosen to be the primary key.
This primary key is used to identify rows once decided, which reduces the complexity of data
retrieval since we would rely on only 1 key for most queries. A primary key cannot have null
values for obvious reasons.
5) Alternate Keys
After a primary key is chosen from the set of candidate keys,
the leftover keys are called Alternate Keys.
6) Foreign Key
A Foreign Key in table X is a primary key in another table Y,
which is used to identify the rows in table Y from the point of
view of table X. For example, if each college student had a
proctor, we could put the details of the proctor on the student
table itself. But, since many students can have the same proctor,
doing so will result in redundant data. To eliminate this
redundancy, we can create a separate proctor table and mention
the proctor’s id for each student in the student table.
In this scenario, proctor_Id is the foreign key in the Student
table and is used to cross-reference the proctor’s details
Types of Constraints in SQL
• Not Null Constraint in SQL
By default, SQL tables can accept null values. But if you want to restrict columns
from taking null values, you can use the NOT NULL constraint in SQL.

CREATE TABLE Student1


(
ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int
);
• UNIQUE Constraint in SQL
As the name gives out, the UNIQUE constraint prohibits any duplicate values in a
column. If a column has a UNIQUE constraint, it will not allow any similar row entry
for that field. You can declare more than one column as UNIQUE in a table.

CREATE TABLE Student1


(
ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int,
Email_Id NVARCHAR(50) UNIQUE
);
• PRIMARY KEY Constraint in SQL
The PRIMARY KEY constraint is a combination of both NOT NULL and UNIQUE
constraints. This means that when you define a column with PRIMARY KEY, it will
not accept any null or duplicate values.

CREATE TABLE Student2


(
Name varchar(25) NOT NULL,
ID int PRIMARY KEY,
Age int,
Email_Id NVARCHAR(50) UNIQUE
);
• FOREIGN KEY Constraint in SQL
The FOREIGN KEY constraint in SQL is usually used to build relationships
between tables in a database. It handles foreign affairs by connecting to the
PRIMARY KEY values of another table
FOREIGN KEY (column_name_first_table) REFERENCES (column_name_second_table)
CREATE TABLE ORDERS
( CREATE TABLE CUSTOMERS
O_ID int PRIMARY KEY, (
ORDER_NO int UNIQUE, C_ID int PRIMARY KEY,
C_ID int, NAME VARCHAR(25) NOT NULL,
FOREIGN KEY (C_ID) REFERENCES CITY VARCHAR(20)
CUSTOMERS(C_ID) );
);
• CHECK Constraint in SQL
The CHECK constraint allows you to specify a condition that needs to be met to
insert data into a table successfully.

CREATE TABLE VOTING_LIST


(
ID int PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
Age int NOT NULL CHECK (AGE>=18)
);
• DEFAULT Constraint in SQL
The DEFAULT constraint in SQL is used to assign a default value to the left empty
fields. Thus, if you neither want a user to enter empty fields nor want to implement the
NOT NULL constraint, DEFAULT constraint can come in handy. It is also helpful to
provide a default value to a field if set to NOT NULL. This will prevent the constraint
error, as the field will be given the default value and will not remain NULL.

CREATE TABLE VOTING_LIST


(
ID int PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
Age int NOT NULL DEFAULT 18
);
TABLE CUSTOMER ( customer_name , street , city );

TABLE BRANCH ( BRANCH_NAME , ASSETS TEXT , BRANCH_CITY );

DEPOSIT_TABLE ( CUSTOMER_NAME , BRANCH_NAME , ACCOUNT_NO , BALANCE );

BORROW ( CUSTOMER_NAME , BRANCH_NAME , LOAN_NO , AMOUNT );


Customer ( custid , fname , mname , ltname , city mobileno ,
occupation , dob )
Branch ( bid, bname, bcity)
Account ( acnumber, custid, bid, opening_balance, aod, atype,
astatus,;
Trandetails ( tnumber , acnumber , dot , medium_of_transaction,
transaction_type,
transaction_amount)
Loan ( custid, bid, loan_amount )

BANK
Types of Relational Operations in DBMS
In Relational Algebra, we have two types of Operations.
•Basic Operations
•Derived Operations
Basic Operations
Six fundamental operations are used The majority of data retrieval operations are
carried out by these operations

STUDENT(ROLL, NAME, AGE) and EMPLOYEE(EMPLOYEE_NO, NAME, AGE)


STUDENT EMPLOYEE

ROLL NAME AGE EMPLOYEE_


NAME AGE
1 Aman 20 NO
2 Atul 18 E-1 Anant 20
3 Baljeet 19 E-2 Ashish 23
4 Harsh 20 E-3 Baljeet 25
5 Prateek 21 E-4 Harsh 20
E-5 Pranav 22
Select (σ)
Select operation is done by Selection Operator which is represented by "sigma"(σ).
It is used to retrieve tuples(rows) from the table where the given condition is satisfied. It is
a unary operator means it requires only one operand.

Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
Suppose we want the row(s) from STUDENT Relation where "AGE" is 20

ROLL NAME AGE


σ AGE=20 (STUDENT)
1 Aman 20
4 Harsh 20
Project (∏)
Project operation is done by Projection Operator which is represented by "pi"(∏). It is
used to retrieve certain attributes(columns) from the table. It is also known as vertical
partitioning as it separates the table vertically. It is also a unary operator.

Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list

Suppose we want the names of all students from STUDENT Relation

∏ NAME(STUDENT) NAME
Aman
Atul
Baljeet
Harsh
For multiple attributes, we can separate them using a ","

∏ ROLL,NAME(STUDENT)

ROLL NAME
1 Aman
2 Atul
3 Baljeet
4 Harsh
5 Prateek
Union (∪)
Union operation is done by Union Operator which is represented by "union"(∪). It is the
same as the union operator from set theory, i.e., it selects all tuples from both relations
but with the exception that for the union of two relations/tables both relations must
have the same set of Attributes. It is a binary operator as it requires two operands.

Notation: R ∪ S
Where R is the first relation
S is the second relation

If relations don't have the same set of attributes, then the union of such relations will result in NULL.
NAME
Suppose we want all the names from STUDENT and EMPLOYEE relation.
Aman
Anant
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE) Ashish
Atul
Baljeet
Set Difference (-)
Set Difference as its name indicates is the difference between two relations (R-S). It is
denoted by a "Hyphen"(-) and it returns all the tuples(rows) which are in relation R
but not in relation S. It is also a binary operator.
Notation : R - S
Where R is the first relation
S is the second relation
like union, the set difference also comes with the exception of the same set of attributes in both relations.

∏ NAME(EMPLOYEE) - ∏ NAME(STUDENT)

NAME NAME
Aman Anant
Atul Ashish
Prateek Pranav
Cartesian product (X)
Cartesian product is denoted by the "X" symbol. we have two relations R and S. Cartesian
product will combine every tuple(row) from R with all the tuples from S.

Notation: R X S
Where R is the first relation
S is the second relation

two relations STUDENT and EMPLOYEE STUDENT X EMPLOYEE


EMPLOYEE
ROLL NAME AGE NAME AGE
_NO
1 Aman 20 E-1 Anant 20
1 Aman 20 E-2 Ashish 23
1 Aman 20 E-3 Baljeet 25
1 Aman 20 E-4 Harsh 20
1 Aman 20 E-5 Pranav 22
2 Atul 18 E-1 Anant 20
2 Atul 18 E-2 Ashish 23
2 Atul 18 E-3 Baljeet 25
2 Atul 18 E-4 Harsh 20
2 Atul 18 E-5 Pranav 22
Rename (ρ)
Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to
rename the output relation. Rename operator too is a binary operator.
Notation: ρ(R,S)
Where R is the new relation name
S is the old relation name
we are fetching the names of students from STUDENT relation. WeSTUDENT_NAME
would like to
rename this relation as STUDENT_NAME.

STUDENT NAME
ρ(STUDENT_NAME,∏ NAME(STUDENT))

ρ(STUDENT_NAME,DOB ∏ AGE,NAME(STUDENT))
NAME
Aman
Atul
Baljeet
•Joins are used to Join two or more tables in the Database.
•There are mainly three types of Join - Inner Join, Natural Join, Outer Join.
•Inner joins are of two types - Theta Join and Equi Join.
•Outer joins are of Three types - Left Outer Join, Right Outer Join and Full Outer Join.
•Natural Join is performed only when there is at least one matching attribute in both the
tables.
•Left Outer join always returns all the rows of left table irrespective of the Join condition.
•Right Outer Join always returns all the rows of right table irrespective of the Join
condition.
•Full Outer Join always returns all the Rows of both the table irrespective of the join
condition.
Join in DBMS
Joins in relational algebra are simply cartesian products followed by selection.

In DBMS, a join statement is mainly used to combine two tables based on a


specified common field between them.
If we talk in terms of Relational algebra, it is the cartesian product of two tables
followed by the selection operation.
Thus, we can execute the product and
selection process on two tables using
a single join statement.
We can use either 'on' or 'using' clause
in MySQL to apply predicates to the
join queries.
A Join can be broadly divided into two types:
1.Inner Join
2.Outer Join
Employee Department

EmpId Empname DeptId DeptId Deptname


101 X 1 1 CSC
102 Y 2 2 MECH
103 Z 2 3 ECE
104 A 3 4 CIVIL
105 B 4 5 IT
Inner Join
Inner Join is a join that can be used to return all the values that have matching values in
both the tables.

The inner join can be further divided into the following types:
1.Equi Join
2.Natural Join
1. Equi Join
Equi Join is an inner join that uses the equivalence condition for fetching the values of
two tables.

Select employee.empId, employee.empName, department.deptName


from employee Inner Join department on employee.deptId =
department.deptId;
Employee Department
Department

EmpId Empnam DeptId EmpId Empn DeptId


DeptId Deptname
e ame
1 CSC
101 X 1 101 X 1
2 MECH
102 Y 2 102 Y 2
3 ECE
103 Z 2 103 Z 2
4 CIVIL
104 A 3 104 A 3
5 IT
105 B 4 105 B 4
2. Natural Join
Natural Join is an inner join that returns the values of the two tables on the
basis of a common attribute that has the same name and domain. It does not
use any comparison operator. It also removes the duplicate attribute from the
results.

Select * from employee Natural Join department;

EmpId Empname DeptId DeptNa


me
101 X 1 CSC
102 Y 2 MECH
104 A 3 ECE
105 B 4 CIVIL
Outer Join
Outer Join is a join that can be used to return the records in both the tables whether
it has matching records in both the tables or not.
The outer join can be further divided into three types:
1.Left-Outer Join
2.Right-Outer Join
3.Full-Outer Join
1. Left-Outer Join:
The Left-Outer Join is an outer join that returns all the values of the left table, and the
values of the right table that has matching values in the left table. If there is no matching
result in the right table, it will return null values in that field.
Select employee.empId, employee.empName,
department.deptName from employee Left Outer Join
department on employee.deptId = department.deptId;

EmpId Empname Dept.Name


101 X CSC
102 Y MECH
103 Z MECH
104 A ECE
105 B CIVIL
2. Right-Outer Join:
The Right-Outer Join is an outer join that returns all the values of the right table, and the
values of the left table that has matching values in the right table.

Select employee.empId, employee.empName,


department.deptName from employee Right
Outer Join department on employee.deptId =
department.deptId;

EmpId Empname Dept.Name


101 X CSC
102 Y MECH
103 Z MECH
104 A ECE
105 B CIVIL
NUL NUL IT
In database management systems, an aggregate function is a function that performs
calculations on a group of values and returns a single value.
It is used to summarize the data. It performs calculations on multiple rows of a
single column of a table to form a single value of more significant meaning.
These Aggregate functions are inbuilt SQL functions. They are often used with the
GROUP BY clause to calculate an aggregate value for each group.

Some of MySQL Aggregate functions are:


•AVG
•COUNT
•MAX
•MIN
•SUM
AVG
The SQL AVG function calculates the average value of a column of numeric type. It
returns the average of all non-NULL values.

SYNTAX : AVG ( [ALL | DISTINCT] expression )

1. SELECT AVG(Salary) FROM Employees;


This query returns the average salary of all the
employees.

2. SELECT Dept_Id,AVG(Salary) FROM Employees GROUP BY Dept_Id;


This query will return the Dept_Id and Average
Salary of Employees of corresponding Department.

3. SELECT Emp_Id, Emp_Name FROM Employees WHERE Salary > AVG(Salary);


This query will return the Emp-Id, Empnme whose
Salary is greater the Average Salary of all the Employees.
COUNT
• The COUNT function is used to count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
• The SQL COUNT function returns the number of rows in a table satisfying the criteria specified in the
WHERE clause. It sets on the number of rows or non-NULL column values.
• COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
• COUNT(*) considers duplicate and Null while Count(column_name) removes Null values
records from counting.
Syntax
COUNT(*) or COUNT( [ALL | DISTINCT] expression )

1. SELECT COUNT(*) FROM Employees;


This query will return the total number of records in the
Employees table.
2. SELECT COUNT(DISTINCT Dept_Id) FROM Employees;
This query will return the count total number of Dept_Id. It
does not consider duplicate Dept_Id and Null.
3. SELECT Dept_Id, COUNT(*) FROM Employees GROUP BY Dept_Id;
This query will return the count of Employees in each
department.
MAX
• The aggregate function MAX() is used to find the maximum value or highest
value of a certain column or expression or a set of values.
• It is applicable to all the data types.
• It excludes NULL values and return distinct value as a result.
Syntax:
MAX (expression)
1. SELECT MAX(Salary) FROM Employees;
This query will return maximum salary from the Employees table.
2.SELECT Dept_Id, MAX(Salary) FROM Employees GROUP BY Dept_Id;
This query will return the Dept_Id along with the maximum salary of
each department.
3.SELECT * FROM Employees WHERE Salary = MAX(Salary);
This query will return all the details of the Employee who has the
maximum salary.
4. SELECT * FROM Employees WHERE Hire_Date = MAX(Hire_Date);
This query will return the details of the Employee who has been in
the Company for the least amount of time.
MIN
• The aggregate function MIN() is used to find the minimum value or lowest value of a certain column or expression
or a set of values.
• It is applicable to all the data types.
• It excludes NULL values and return distinct value as a result.
• Syntax : MIN(expression)

1. SELECT MIN(Salary) FROM Employees;


This query will return minimum salary from the Employees table.
2. SELECT Dept_Id, MIN(Salary) FROM Employees GROUP BY Dept_Id;
This query will return the Dept_Id along with the minimum salary
of each department.
3. SELECT * FROM Employees WHERE Salary = MIN(Salary);
This query will return all the details of the Employee who has the
minimum salary.
4. SELECT * FROM Employees WHERE Hire_Date = MIN(Hire_Date);
This query will return the details of the Employee who has been at
the Company for the longest time.
SUM
• The aggregate function SUM() is used to calculate the sum of all the values of the
select column. It returns the sum of values in a set.
• SUM function ignores the NULL values. If no matching records are found, it
returns NULL.
• It is applicable to numeric values.
• Syntax : SUM([ALL | DISTINCT]) expression)
DISTINCT is used to select unique values.

1. SELECT SUM(Salary) FROM Employees;


This query will return total salary of all the Employees.
2. SELECT Dept_Id, SUM(Salary) FROM Employees GROUP BY
Dept_Id;
This query will return the Dept_Id along with the total
salary of all the employees in each department.
3.SELECT Dept_Id FROM Employees WHERE SUM(Salary) > 200000;
This query will return the Dept_Id in which the total salary
of all the employees in the department is greater than
200000.
Black Background for Business 61

You might also like