You are on page 1of 4

DBMS Questions asked in Virtusa

▪ How should data be structured to facilitate Join clauses in a one-to-many relationship? What
about many to many relationship?
Ans: Generally, one-to-many relationships are structured using a single FOREIGNKEY. Consider our
example of customers and orders above:

CREATE TABLE customers (


customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
);

CREATE TABLE orders (


order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
order_placed_date DATE NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);

This is a one-to-many relationship, because one customer can place multiple orders, but
one order cannot be assigned to more than one customer. As such, we've defined it with a simple
foreign key in the orders table pointing to a given customer_id, and we can use JOIN clauses in
our SELECT queries fairly easily.
Many-to-many relationships are a bit more complicated. For example, what if we had
an orders table and a products table with a many-to-many relationship: any order can contain
multiple products, and any product can be assigned to multiple orders.
How would we structure our database?
The answer: we use an intermediary mapping table with two FOREIGN KEYs. Consider the following:

CREATE TABLE orders (


order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_placed_date DATE NOT NULL,
);

CREATE TABLE products (


product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price INT NOT NULL
);

CREATE TABLE products_to_orders (


product_to_order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);

Above, we've created a separate table called products_to_orders that maps items on
the products table to items on the orders table. Each row in our products_to_orders table
represents one product-order combination, so that multiple products can be assigned to
one order — and a single product can be assigned to multiple orders.

In this example, we need to use two JOIN statements to link all these tables together: one to
link products_to_orders to products, and one to link products_to_orders with orders.

▪ Explain Merge Join?

Ans: 1. Merge join If both join relations come in order, sorted by the join attribute(s), the system
can perform the join trivially, thus: It can consider the current group of tuples from the inner relation
which consists of a set of contiguous tuples in the inner relation with the same value in the join
attribute.

2.For each matching tuple in the current inner group, add a tuple to the join result. Once the inner
group has been exhausted, advance both the inner and outer scans to the next group.

▪ Explain non-equi join with example?

Ans: Non Equi Join :


1.When two or more tables are joining without Equal to condition then that join is known as Non
Equi join.
2.The use of non equi join is very rare in real life scenarios.You can join tables using any other
attributes except equal to operator.
3.You can use any operator for non equi join that is <>,!=,<,>,Between.
Example:

Select b.Department_ID,b.Department_name from


Employee a,Department b where a.Department_id <> b.Department_ID;
▪ What is cartesian join and Hash join?
Ans: Cartesian join is very less used join in day to day application. Developers have strict instructions
that join should not be Cartesian product. Because if we use this join then each and every record
from first table will join to each and every record of second table. When we are not giving any
joining condition then it displays Cartesian product.
A hash join algorithm can only produce Equi-joins. The database system pre-forms access to the
tables concerned by building hash tables on the join-attributes.

▪ What is equi join?

Ans: 1. When two or more tables has been joined using equal to operator then this category is called
as equi join.

/*Equi Join*/

2.Just we need to concentrate on condition is equal to(=) between the columns in the table.

3.Make sure that we are using where clause to apply the condition between two tables.

4.If the condition of join misses or there is not relation between the tables in the join then Equi join
fails and the result will be the Cartesian product or cross join.

Syntax:
Select alias1.column_name1, alias1.column_name2, alias2.column_name1..

from table1 alias1, table2 alias2

where table1.column=table2.column; —Equi join condition


Example:
Select a.Employee_name,b.Department_name from Employee a,Employee b
where a.Department_ID=b.Department_ID;

The above query will fetch the Employee name from Employee table and Department name from
department table.
Questions: How can you insert multiple rows using one insert statements.

Questions: Difference between rowid and rownum in SQL?

Question: How can you swap values between the rows in a table using single- SQL statement.

You might also like