Professional Documents
Culture Documents
Q1. Draw a generic operator tree for the following SQL query?
Q2. Draw and discuss the client/server system and
peer to peer distributed system architecture
details?
Answer.
Remember:
In the following example, the id is a common column for both the table and matched
rows based on that common column from both the table have appeared.
Code:
SELECT id,aval1,cval1
FROM table111
Copy
The INNER JOIN using ON clause do the same job. Here is the following -
Code:
SELECT table1111.id,table111.aval1,table113.cval1
FROM table111
ON table111.id=table113.id;
Sample tables:
Sample Output:
mysql> SELECT id,aval1,cval1
-> FROM table111
-> NATURAL JOIN table113;
+------+-------+-------+
| id | aval1 | CVAL1 |
+------+-------+-------+
| 3 | 200 | 17 |
| 2 | 401 | 12 |
| 1 | 405 | 16 |
+------+-------+-------+
3 rows in set (0.00 sec)
2 Equijoin:
An equi join is a type of join that combines tables based on matching values
in specified columns.
Remember:
Example
The following tables have been created
1. product_list
ID Pname
1 Apples
2 Oranges
3 Mangoes
2. product_details
ID Brand Origin
3. brand_details
Brand OfficeAddress
When performing an inner join, rows from either table that are unmatched in the other
table are not returned. In an outer join, unmatched rows in one or both tables can be
returned.
There are few types of Outer Join.
SQL left outer join is also known as SQL left join. Suppose, we want to join two tables: A and B. SQL
left outer join returns all rows in the left table (A) and all the matching rows found in the right table
(B). It means the result of the SQL left join always contains the rows in the left table
1 SELECT c.customerid,
2 c.companyName,
3 orderid
4 FROM customers c
6 ORDER BY orderid
SQL right outer join returns all rows in the right table and all the matching rows found in the left table.
1 SELECT c.customerid,
2 c.companyName,
3 orderid
4 FROM customers c
6 ORDER BY orderid
Some database management systems do not support SQL full outer join syntax e.g., MySQL.
Because SQL full outer join returns a result set that is a combined result of both SQL left join and
SQL right join. Therefore you can easily emulate the SQL full outer join using SQL left join and SQL
right join with
1 SELECT c.customerid,
2 c.companyName,
3 orderid
4 FROM customers c
6 ORDER BY orderid
4 INNER JOIN:
The INNER JOIN creates a new result table by combining column values of two tables
(table1 and table2) based upon the join-predicate. The query compares each row of
table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate.
When the join-predicate is satisfied, column values for each matched pair of rows of A
and B are combined into a result row.
Example
Consider the following two tables.
Table 1 − CUSTOMERS Table is as follows.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ahmad | 32 | Mardan | 2000.00 |
| 2 | Abbas | 25 | Karachi | 1500.00 |
| 3 | Khan | 23 | Pesh | 2000.00 |
| 4 | Aryan | 25 | Lahore | 6500.00 |
| 5 | Ali | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Rawl | 4500.00 |
| 7 | Sadia | 24 | Murre | 10000.00 |
+----+----------+-----+-----------+----------+
Now, let us join these two tables using the INNER JOIN as follows −
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
5 Self Join:
A self join is a join in which a table is joined with itself (which is also called Unary
relationships), especially when the table has a FOREIGN KEY which references
its own PRIMARY KEY. To join a table itself means that each row of the table is
combined with itself and with every other row of the table.
In the following example, we will use the table EMPLOYEE twice and in order to
do this we will use the alias of the table.
To get the list of employees and their supervisor the following SQL statement has
used:
SQL Code:
SELECT a.emp_id AS "Emp_ID",a.emp_name AS "Employee Name",
6 CROSS JOIN:
Cross join is used to combine each row of the first table with each row of the
second table. It is also known as the Cartesian join since it returns the
Cartesian product of the sets of rows from the joined tables.
Examples
Consider the two tables, called employee and department, below:
/*CROSS JOIN*/
2
SELECT *
4
FROM employee
5