You are on page 1of 36

CS2072 Database Engineering Laboratory

&
CS2082 Database Management Systems
Laboratory
(LAB-5)

Prof. Sambit Bakshi


Computer Science and Engineering,
National Institute of Technology, Rourkela.
bakshisambit@nitrkl.ac.in
Relational Algebra
The basic set of operation for relational model is relational algebra.

These operations allows users to specify basic retrieval requests.

The input for these operation is one or more relation(table).

The output of these retrieval is again a relation which may be combination


of one or more relations.
Set Compatibility
A SQL set Operator is a binary operator used to combine the result of two
queries into a single query.
For eg.
Let 𝑅1 𝑎𝑛𝑑 𝑅2 are two relation defined on a particular schema.

Rule 1) Degree(𝑅1 )= Degree(𝑅2 )

Rule 2)Dom(𝑅1 )= Dom(𝑅2 )


Set Operator in SQL Server
Set Operators
Operator Meaning

(Union) Binary Operator includes distinct


records from two or more table.
(Union All) Binary Operator includes duplicates
records from both the table.
(Intersect) Binary operator includes common
records from both table.
(Except) Binary Operator includes the record
from first table not present in
second.
Characteristics of Set Operation

Commutative Property – 𝑅1 ∪ 𝑅2 = 𝑅2 ∪ 𝑅1

Associative Property- 𝑅1 ∪ (𝑅2 ∪ 𝑅3 ) = (𝑅1 ∪ 𝑅2 ) ∪ 𝑅3


UNION()
Consider two relation A and B having same degree. The Union of relation
A and relation B is represented by AUB. UNION operator gives the
distinct elements from both relation.

SELECT (Column_name _1,


SELECT E_ID,
Column_name_2...) from Table_1
UNION E_NAME,E_ADD FROM
SELECT EMPLOYEE UNION SELECT
( Column_name _1, Column_name_ S_ID, S_NAME,S_ADD FROM
2...) STUDENT;
FROM Table_2;
UNION ALL()
UNION ALL operator is used to include all the records from both
the table. It allows duplicity.

SELECT (Column_name _1, Co


lumn_name_2...) from
Table_1 UNION ALL SELECT E_ID, E_NAME,E_ADD
SELECT ( Column_name _1, C FROM EMPLOYEE UNION ALL
olumn_name_2...) SELECT S_ID, S_NAME,S_ADD
FROM Table_2; FROM STUDENT;
INTERSECT()
Consider two relation A and B having same degree and same
domain. The Intersection of relation A and relation B
is represented by A and B . It includes the element that are common
in both relation.

SELECT (Column_name _1, Colu SELECT E_ID, E_NAME,E_AGE


mn_name_2...) from Table_1
INTERSECT FROM EMPLOYEE INTERSECT
SELECT ( Column_name _1, Colu SELECT S_ID, S_NAME,S_AGE
mn_name_2...) FROM STUDENT;
FROM Table_2;
EXCEPT()
EXCEPT returns any distinct values from the left select query that are not a
found on the right select query.

SELECT Column1, SELECT E_ID, E_NAME,E_ADD FROM


column2 FROM EMPLOYEE EXCEPT SELECT S_ID,
Table-1 S_NAME,S_ADD FROM STUDENT;
EXCEPT
SELECT column1,
coulmn2 FROM
Table-2
Rich Set Operator
Operator Return Descriptions
IN VALUE The IN operator in SQL is used to search for specified value
matches any value in set of multiple values.

BETWEEN NUMBER/TEXT/DATES The BETWEEN operator in SQL is used to get values within a
range.

LIKE STRING The LIKE operator in SQL is used to search for character string
with the specified pattern using wildcards in a column.

EXISTS BOOLEAN It returns TRUE if the subquery returns one or more records.

IS NULL BOOLEAN Return TRUE if it has null values

SOME/ALL BOOLEAN Returns TRUE if ALL of the subquery values meet the condition.
/ANY
IN()
The IN operator in SQL is used to search for specified value matches
any value in set of multiple values.

SELECT column_name(s) SELECT * FROM EMPLOYEE


FROM table_name WHERE E_NAME
WHERE column_name IN (SELEC IN('MOHIT','AJAY','ANJA
T STATEMENT); LI’)

SELECT * FROM EMPLOYEE WHERE E_NAME


NOT IN('MOHIT','AJAY','ANJALI’)
BETWEEN()
The BETWEEN operator in SQL is used to get values within a range.

SELECT * FROM table_name SELECT * FROM EMPLOYEE


WHERE column_name WHERE E_SALARY
BETWEEN value1 AND value2; BETWEEN 10000 AND 20000;

SELECT * FROM EMPLOYEE


WHERE E_SALARY NOT
BETWEEN 10000 AND 20000;
LIKE()
The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column. There are two wildcards often used in
conjunction with the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character

SELECT column1, column2, ... SELECT * FROM EMPLOYEE


FROM table_name WHERE E_NAME LIKE 'A%';
WHERE columnname LIKE pattern;
SELECT * FROM EMPLOYEE
WHERE E_NAME LIKE '%A';
LIKE OPERATORS
LIKE Operators Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE Finds any values that have "or" in any
'%or%' position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomeName LIKE 'a_%' Finds any values that start with "a"
and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a"
and are at least 3 characters in length
IS NULL

The SQL Server IS NULL operator is used to test for a NULL value. It can
be used with SELECT, INSERT and UPDATE .

SELECT * SELECT * FROM


FROM TABLENAME WHERE EMPLOYEE
COULMN NAME IS NULL; WHERE SALARY
IS NULL;
IS NULL
IS NULL OPERATOR SYNTAX
INSERT INSERT INTO TABLE NAME(COL-
1,COL-2,Col-3) SELECT (COL-1, Col-2
COL-4) FROM DEPARTMENT WHERE
NAME IS NULL;
UPDATE UPDATE TABLE NAME SET
COLUMN=XXX WHERE COLUMN IS
NULL
DELETE DELETE FROM EMPLOYEE WHERE
COLUMN IS NULL;
ALL()
The SQL ALL operator is a logical operator that compares a single
value with a single-column set of values returned by a subquery. It
must be preceded by a comparison operator such as >, >=, <, <=,
<>, = and followed by a subquery.

SELECT * FROM TABLE NAME SELECT * FROM EMPLOYEE


WHERE COLUMN NAME WHERE
> ALL(SUBQUERY) E_SALARY=ALL(SELECT
E_SALARY FROM EMPLOYEE
WHERE E_NAME='RAKESH');
ANY()
The ANY Operator in SQL Server is used to compare a value to
each value in a list of results from a query and evaluate to true if the
result of an inner query contains at least one row

SELECT COLUMN
SELECT * FROM
NAME…|EXP_1 FROM
TEACHING WHERE
TABLE NAME WHERE EXP_2 Age > ANY
COMP_OPERATOR {ANY} (SELECT Age
(SUBQUERY) FROM
NONTEACHING);
SOME()
The SOME Operator in SQL Server is used to compare a value to
each value in a list of results from a query and evaluate to true if the
result of an inner query contains at least one row.

SELECT COLUMN
SELECT * FROM
NAME…|EXP_1 FROM
TEACHING WHERE
TABLE NAME WHERE EXP_2 Age > SOME
COMP_OPERATOR {SOME} (SELECT Age
(SUBQUERY) FROM
NONTEACHING);
EXIST()
The SQL Server EXISTS operator is used in combination with a
subquery and is considered to be met if the subquery returns at least
one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE
statement.

SELECT COLUMN
NAME…|EXP_1 FROM SELECT * FROM TEACHING
WHERE EXISTS (SELECT *
TABLE NAME WHERE EXP_2
FROM CONTACT WHERE
COMP_OPERATOR {SOME} TEACHING.EId =
(SUBQUERY) CONTACT.EmployeeID);
Why Date and Time Function?
To ensure that the date and time module is accessible while
creating and accessing a database.
Date and Time Function in SQL Server
The date and time function in SQL server allows us to handle date
and time data effectively.

DATE: YYYY-MM-DD

DATETIME: YYYY-MM-DD HH: MI: SS

TIMESTAMP: YYYY-MM-DD HH: MI: SS

YEAR: YYYY or YY
Date and Time Functions
Returning the System Date and Returning the Modify Date and Time
Time Values Values

Returning the Date and Time Parts SET or Return Session Format
Functions

Returning the Date and Time Values Returning the Validate Date and
from Their Parts Time Values

Returning the Date and Time


Difference Values
Returning the System Date and Time Values
Functions Description
CURRENT_TIMESTAMP() This function is used to get the current date and time values without
including the time zone offset.

GETUTCDATE() This function is used to get the current UTC date and time values as an
integer.

GETDATE() This function is used to get the system's current date and time on which the
SQL Server is installed.

SYSDATETIME() This function is used to get the system's current date and time with more
fractional second precision without including the time zone offset.

SYSUTCDATETIME() This function is used to get the system's current date and time value based on
the UTC timestamp as an integer.
System Date and Time Values
SELECT CURRENT_TIMESTAMP AS Date;

SELECT GETDATE() AS Date;

SELECT GETUTCDATE() AS Date;

SELECT SYSDATETIME() AS Date;

SELECT SYSDATETIMEOFFSET() AS Date;


Return Date and Time Parts
Functions Description
DATENAME() This function is used to get a portion of the date in day,
month, or year as a character string.
DATEPART() This function is used to get the portion of the date as
an integer number.
DAY() This function is used to get the day value from the
input dates as an integer.
MONTH() This function is used to get the month value from the
input dates as an integer..

YEAR() This function is used to get the year value from the
input dates as an integer.
DATENAME()
DATENAME() function is used to extract the part of the date such
as day, month, or year.

SELECT DATENAME(day, '2021/09/10') AS Result1,

DATENAME(month, '2021/09/10') AS Result2,

DATENAME(year, '2021/09/10') AS Result3;


DATEPART()

DATEPART() function is used to extract the part of the date


as an integer value, which makes it different from the
DATENAME() function.

SELECT DATEPART(day, '2021/09/10') AS Result1,

DATEPART(month, '2021/09/10') AS Result2,

DATEPART(year, '2021/09/10') AS Result3;


YEAR()
YEAR() function is used to extract only the year portion of the
input dates

SELECT YEAR('2021/09/10') AS Result1,

YEAR('2012/05/17') AS Result2;
Date and Time Difference
DATEDIFF()- This function is used to get the difference in a date
part of the two input dates values.

SELECT DATEDIFF(dd,'2019/2/3', '2020/3/5') AS TotalDays,

DATEDIFF(MM,'2019/2/3', '2020/3/5') AS TotalMonths,

DATEDIFF(WK,'2019/2/3', '2020/3/5') AS TotalWeeks;


Modify Date and Time Values
Functions Description
DATEADD() This function is used to add an integer value to a date
part of the input dates and returns the new date value.
EOMONTH() This function is used to get the last day of the month
with the specified date and an optional offset.
SWITCHOFFSET() This function is used to modify the time-zone offset of
a datetime offset value and preserves the UTC value.
TODATETIMEOFFSET() This function is used to change the DATETIME value
into a DATETIMEOFFSET value.
DATEADD()

DATEADD function and returns a new date value after adding


an integer value to the date portion.

SELECT DATEADD(second, 1, '2020-12 31 23:59:59') AS Result1,

DATEADD(day, 1, '2020-12-31 20:59:59') AS Result2;


EOMONTH()

This function returns the last day of the month for a specified
date.

SELECT EOMONTH('2020-02-2') AS end_of_feb2020,

EOMONTH('2021-02-22') AS end_of_feb2021;
IS DATE()
This function is used to check the entered dates follows the
standard format of date, time, or datetime value or not.

SELECT ISDATE('2020-06-15') AS Result1,

ISDATE('2020-15-06') AS Result2;
THANK YOU!

You might also like