You are on page 1of 12

The ICS/IT, The university of Agriculture Peshawar

Assignment No .1 ,BSCS ,6TH semester


Name: Ahmad Munir, Roll no:190, Section:D

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.

Client–server model is a distributed application structure that partitions tasks or


workloads between the providers of a resource or service, called servers, and service
requesters, called clients. Often clients and servers communicate over a computer
network on separate hardware, but both client and server may reside in the same
system. A server host runs one or more server programs, which share their resources
with clients. A client does not share any of its resources, but it requests content or
service from a server. Clients, therefore, initiate communication sessions with servers,
which await incoming requests. Examples of computer applications that use the client-
server model are email, network printing, and the World Wide Web.

Peer-to-peer (P2P) computing or networking is a distributed application architecture


that partitions tasks or workloads between peers. Peers are equally privileged,
equipotent participants in the application. They are said to form a peer-to-peer network
of nodes.
Q3.Write SQL queries with proper examples for each of the following.
Answer.
1 Natural join:
A natural join is a type of join that combines tables based on columns with
the same name and type.

Remember:

• There is no need to specify the column names to join.


• The resultant table does not contain repeated columns.

Example : MySQL NATURAL JOIN

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

NATURAL JOIN table113;

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

INNER JOIN table113

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:

• The column names do not need to be the same.


• The resultant table contains repeated columns.
• It is possible to perform an equi join on more than two tables

Example
The following tables have been created

1. product_list
ID Pname

1 Apples

2 Oranges

3 Mangoes

2. product_details

ID Brand Origin

1 Fresh Foods USA


ID Brand Origin

2 Angro Ltd Pakistan

3. brand_details

Brand OfficeAddress

Fresh Foods 123 Seattle USA

Angro Ltd 124 Lahore

3 Outer Join and it’s types:


Outer joins

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.

Left 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

left outer join example

The following query selects all customers and their orders:

1 SELECT c.customerid,

2 c.companyName,

3 orderid

4 FROM customers c

5 LEFT JOIN orders o ON o.customerid = c.customerid

6 ORDER BY orderid

Right outer join

SQL right outer join returns all rows in the right table and all the matching rows found in the left table.

SQL OUTER JOIN – right outer join example

The following example demonstrates the SQL right outer join:

1 SELECT c.customerid,

2 c.companyName,

3 orderid

4 FROM customers c

5 RIGHT JOIN orders o ON o.customerid = c.customerid

6 ORDER BY orderid

Full Outer Join


• all rows in the left table table_A.
• all rows in the right table table_B.
• and all matching rows in both tables.

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

full outer join example

The following query demonstrates the SQL full outer join:

1 SELECT c.customerid,

2 c.companyName,

3 orderid

4 FROM customers c

5 FULL OUTER JOIN orders o ON o.customerid = c.customerid

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 |
+----+----------+-----+-----------+----------+

Table 2 − ORDERS Table is as follows.


+-----+---------------------+-------------+--------+
| OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+

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;

This would produce the following result.


+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | Ahmad | 3000 | 2009-10-08 00:00:00 |
| 3 | Ahmad | 1500 | 2009-10-08 00:00:00 |
| 2 | Abbas | 1560 | 2009-11-20 00:00:00 |
| 4 | komal | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+

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.

Example of SQL SELF JOIN

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",

b.emp_id AS "Supervisor ID",b.emp_name AS "Supervisor Name"

FROM employee a, employee b

WHERE a.emp_supv = b.emp_id;

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:

Executing cross join on these gives us the following output:

/*CROSS JOIN*/
2

SELECT *
4

FROM employee
5

CROSS JOIN department

7 Semi join and anti join:


Semi-joins and anti-joins are two closely related join methods (options of join
methods, actually) that the Oracle optimizer can choose to apply when retrieving
information. The SQL language is designed to specify the set of data that the user
wishes to retrieve, but to leave the decisions as to how to actually navigate to the
data up to the database itself. Therefore, there is no SQL syntax to specifically
invoke a particular join method. Of course, Oracle does provide the ability to give
the optimizer directives via hints. This chapter will cover these two join
optimization options, the SQL syntax that can provoke them, requirements for
and restrictions on their use, and finally, some guidance on when and how they
should be used.

You might also like