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

siraj

The document explains five types of join operations in relational databases: Theta Join, Equi Join, Natural Join, Outer Join, and Semi Join, providing definitions and examples for each. It also describes Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC), highlighting their differences and applications in querying data. TRC focuses on tuples while DRC emphasizes values and can utilize aggregate functions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

siraj

The document explains five types of join operations in relational databases: Theta Join, Equi Join, Natural Join, Outer Join, and Semi Join, providing definitions and examples for each. It also describes Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC), highlighting their differences and applications in querying data. TRC focuses on tuples while DRC emphasizes values and can utilize aggregate functions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:Sirajuddin Kamal Reg:FA21-BCS-221

QUESTION#01

Illustrate the difference between the five Join operations: Theta Join, Equi Join, Natural Join, Outer Join, and Semi join
with the help of examples.

ANSEWR:

Theta join:

Theta join is a type of join operation in relational databases that combines rows from two or more tables based on a
specified condition (also called a "theta predicate") that uses a comparison operator other than equality. Unlike other
join operations such as inner join or outer join, which use the equality operator (=) to match rows between tables, the
theta join allows for more complex matching conditions, such as greater than (>), less than (<), or not equal to (!=).

The general syntax for a theta join is as follows:

SELECT * FROM table1 [INNER/LEFT/RIGHT/FULL] JOIN table2 ON condition

In this syntax, "table1" and "table2" are the names of the tables to be joined, and "condition" is the theta predicate that
defines the matching criteria. The type of join (inner, left, right, or full) determines whether the result will include only
matching rows, or also include non-matching rows from one or both tables.

Here is an example of a theta join that uses the greater than operator to match rows between two tables:

SELECT * FROM orders JOIN customers ON orders.customer_id > customers.customer_id

This query will return all orders where the customer ID is greater than the customer ID in the corresponding row of the
customers table.

Equal Join:

An equal join, also known as an equijoin, is a type of join operation in relational databases that combines rows from two
or more tables based on a matching condition using the equality operator (=).

The general syntax for an equal join is as follows:

SELECT * FROM table1 [INNER/LEFT/RIGHT/FULL] JOIN table2 ON table1.column = table2.column

In this syntax, "table1" and "table2" are the names of the tables to be joined, and "table1.column" and "table2.column"
are the column names that will be used to match the rows between the tables. The type of join (inner, left, right, or full)
determines whether the result will include only matching rows, or also include non-matching rows from one or both
tables.

Here is an example of an equal join that matches rows between two tables based on a common column called
"customer_id":

Page | 1
Name:Sirajuddin Kamal Reg:FA21-BCS-221
SELECT * FROM orders JOIN customers ON orders.customer_id = customers.customer_id

This query will return all orders where the customer ID in the orders table matches the customer ID in the corresponding
row of the customers table.

Natural binding:

A natural join is a type of join that returns only rows from two tables where the values of all columns with the same
name in both tables match. A natural join implicitly matches like-named columns between two tables.

example:

Let's say he has two tables, Employees and Salary, and both tables have a column named employee_id. You can use a
natural join to join the two tables based on the employee_id column, as shown below.

SELECT e.employee_name, s.salary

FROM Employees e

NATURAL JOIN Salaries s

Outer join:

An outer join is a type of join operation in relational databases that combines rows from two or more tables based on a
matching condition and includes all the rows from one table even if there are no matching rows in the other table.

There are three types of outer joins: left outer join, right outer join, and full outer join.

Left outer join: returns all the rows from the left table and only the matching rows from the right table. If there are no
matching rows in the right table, the result will contain null values.

Right outer join: returns all the rows from the right table and only the matching rows from the left table. If there are no
matching rows in the left table, the result will contain null values.

Full outer join: returns all the rows from both tables and includes null values in the result if there are no matching rows.

The general syntax for an outer join is as follows:

SELECT * FROM table1 LEFT/RIGHT/FULL OUTER JOIN table2 ON table1.column = table2.column

In this syntax, "table1" and "table2" are the names of the tables to be joined, and "table1.column" and "table2.column"
are the column names that will be used to match the rows between the tables. The type of outer join (left, right, or full)
determines which table's rows will be included in the result even if there are no matching rows.

Here is an example of a left outer join that returns all customers and their orders, even if there are no orders for a
particular customer:

Page | 2
Name:Sirajuddin Kamal Reg:FA21-BCS-221

SELECT * FROM customers LEFT OUTER JOIN orders ON customers.customer_id = orders.customer_id

This query will return all customers, and for each customer, it will include all their orders if they have any. If a customer
has no orders, the result will contain null values in the order columns.

Semijoin:
A semijoin is a type of join operation in relational databases that returns only the matching rows from one table based
on a matching condition with another table, without duplicating the rows of the other table.

The syntax for a semijoin is as follows:

SELECT * FROM table1 WHERE EXISTS ( SELECT * FROM table2 WHERE table1.column = table2.column )

In this syntax, "table1" and "table2" are the names of the tables to be joined, and "table1.column" and "table2.column"
are the column names that will be used to match the rows between the tables. The subquery in the WHERE clause is
used to check if there is a match between the rows in the two tables.

Here is an example of a semijoin that returns all customers who have placed an order:

SELECT * FROM customers WHERE EXISTS ( SELECT * FROM orders WHERE customers.customer_id =
orders.customer_id )

This query will return all customers who have at least one order in the orders table. It will not include any order
information in the result, only the customer information. If a customer has multiple orders, they will only appear once in
the result.

QUESTION #02

Explain the following terms:

1. Tuple Relational Calculus

2. Domain Relational Calculus

ANSWER:

Tuple relation calculation:


Tuple Relational Calculus (TRC) is a formal language used to describe and query data in relational databases. A TRC
specifies a set of tuples or rows that meet certain criteria or conditions and returns a subset of those tuples as query
results. TRC is based on the mathematical concept of first-order logic and its syntax resembles predicate computation. A

TRC doesn't specify how to get the data, it only describes what data to get. It is primarily used as a theoretical tool for
describing the properties of relational databases.
Page | 3
Name:Sirajuddin Kamal Reg:FA21-BCS-221
Domain Relational Computation:
(DRC) is another formal language used to describe and query data in relational databases. A DRC specifies a set of values
or attributes that meet certain criteria and returns a subset of those values or attributes as query results. DRC is also
based on the mathematical concept of first-order logic, but its syntax differs from TRC. DRC makes more sense than TRC
because it can use aggregate functions such as SUM, COUNT, AVG, MAX, MIN. DRCs are primarily used as a theoretical
tool for describing the properties of relational databases, but in practice they can also be used to query data.

Page | 4

You might also like