0% found this document useful (0 votes)
23 views6 pages

Assignment No 3 Help File

The document outlines an experiment focused on SQL queries, specifically exploring various types of joins, sub-queries, and views. It provides examples of inner joins, outer joins, natural joins, and nested subqueries, along with SQL syntax for creating views. The conclusion emphasizes the creation of a student database using SQL DML queries incorporating these concepts.

Uploaded by

Swapnali Limkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Assignment No 3 Help File

The document outlines an experiment focused on SQL queries, specifically exploring various types of joins, sub-queries, and views. It provides examples of inner joins, outer joins, natural joins, and nested subqueries, along with SQL syntax for creating views. The conclusion emphasizes the creation of a student database using SQL DML queries incorporating these concepts.

Uploaded by

Swapnali Limkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Database Management System

Laboratory
Experiment No. : 3

SQL Queries – all types of Join, Sub-Query


and View

25
Experiment No. 3
Aim : To Study SQL Queries – all types of Join, Sub-Query and View

TITLE: SQL Queries – all types of Join, Sub-Query and View:


Write at least10 SQL queries for suitable database application using SQL DML statements.

JOIN THEORY :

SQL provides not only the basic Cartesian-product mechanism for joining tuples of
relations found in its earlier versions, but, SQL also provides various other mecha nisms for
joining relations, including condition joins and natural joins, as well as various
forms of outer joins. These additional operations are typically used as subquery expressions in
the from clause

Figure 1. The loan and borrower relations.

A) EXAMPLES :
The various join operations by using the relations loan and borrower in Figure 4.1. We
start with a simple example of inner joins. Figure 4.2 shows the result
of the expression
loan inner join borrower on [Link]-number = borrower .loan-number
The expression computes the theta join of the loan and the borrower relations, with the
join condition being [Link]-number = [Link]-number. The attributes of the result
consist of the attributes of the left-hand-side relation followed by the attributes of the right-
hand-side relation.
Note that the attribute loan-number appears twice in the figure—the first occurrence is
from loan, and the second is from borrower. The SQL standard does not require attribute names
in such results to be unique. An as clause should be used to assign
unique names to attributes in query and sub query [Link] rename the result relation of a join
and the attributes of the result relation by using an as clause, as illustrated here:

loan inner join borrower on [Link]-number = [Link]-number


as lb(loan-number, branch, amount, cust, cust-loan-num)

We rename the second occurrence of loan-number to cust-loan-num. The ordering of the


attributes in the result of the join is important for the renaming. Next, we consider an example
of the left outer join operation:

Figure 2The result of loan inner join borrower on


26
[Link]-number = borrower .loan-number .

LEFT OUTER JOIN :


We can compute the left outer join operation logically as follows. First, compute the
result of the inner join as before. Then, for every tuple t in the left-hand-side relation loan that
does not match any tuple in the right-hand-side relation borrower in the inner join, add a tuple r
to the result of the join: The attributes of tuple r that are derived from the left-hand-side relation
are filled in with the values from tuple t, and the remaining attributes of r are filled with null
values. Figure 4.3 shows the resultant relation. The tuples (L-170, Downtown, 3000) and (L-
230, Redwood, 4000) join with tuples from borrower and appear in the result of the inner join,
and hence in the result of the left outer join. On the other hand, the tuple (L-260, Perryridge,
1700) did not match any tuple from borrower in the inner join, and hence a tuple (L-260,
Perryridge, 1700, null, null) is present in the result of the left outer join.

Figure 3 The result of loan left outer join borrower on


[Link]-number = borrower .loan-number.

NATURAL INNER JOIN :


We consider an example of the natural join operation:
loan natural inner join borrower
This expression computes the natural join of the two relations. The only attribute name
common to loan and borrower is loan-number. Figure 4.4 shows the result of the expression.
The result is similar to the result of the inner join with the on condition in Figure 4.2, since they
have, in effect, the same join condition. However, the attribute loan-number appears only once
in the result of the natural join, whereas it appears twice in the result of the join with the on
condition.

Figure 4 The result of loan natural inner join borrower.

NATURAL RIGHT OUTER JOIN :


Join operations in SQL consists of a join type and a join condition. The join condition
defines which tuples in the two relations match and what attributes are present in the result of
the join. The join type defines how tuples in each relation that do not match any tuple in the
other relation (based on the join condition) are treated. Figure 4.5 shows some of the allowed
join types and join conditions. The first join type is the inner join, and the other three are the
outer joins. Of the three join conditions, we have seen the natural join and the on condition
before, and we shall discuss the using [Link] use of a join condition is mandatory for
outer joins, but is optional for inner joins (if it is omitted, a Cartesian product results).
Syntactically, the keyword natural appears before the join type, as illustrated earlier, whereas
the on and using conditions appear at the end of the join expression. The keywords inner and
outer are optional, since the rest of the join type enables us to deduce whether the join is an
inner join or an outer join.
The meaning of the join condition natural, in terms of which tuples from the two
relations match, is straightforward. The ordering of the attributes in the result of a natural join is

27
as follows. The join attributes (that is, the attributes common to both relations) appear first, in
the order in which they appear in the left-hand-side relation. Next come all nonjoin attributes of
the left-hand-side relation, and finally all nonjoin attributes of the right-hand-side relation.
The right outer join is symmetric to the left outer join. Tuples from the right-handside
relation that do not match any tuple in the left-hand-side relation are padded
with nulls and are added to the result of the right outer join. Here is an example of combining
the natural join condition with the right outer join type:
loan natural right outer join borrower
Figure

Figure 6 The result of loan natural right outer join borrower.

FULL OUTER JOIN :

Figure 7 The result of loan full outer join borrower using(loan-number).

Nested Subqueries
a) Set Membership
SQL allows testing tuples for membership in a relation. The in connective tests for set
membership, where the set is a collection of values produced by a select clause. The not in
connective testsfor the absence of set membership. As an illustration, reconsider the query
“Find all the courses taught in the both the Fall2009 and Spring 2010 semesters.”Earlier,we
wrote such a query by intersecting two sets: the set of courses taught in Fall 2009 and the set of
courses [Link]findingallcourses that were
taught in Fall 2009 and that are also members of the set of courses taught in Spring 2010.
Clearly, this formulation generates the same results as the previous one did,but write our query
using the in connective of SQL . We begin by finding all courses taught in Spring 2010, and we
write the sub query

(select course id from section where semester=’Spring’ and year=2010)

We then need to find those courses that were taught in the Fall 2009 and that appear in the set of
courses obtained in the subquery. We do so by nesting the subquery in the where clause of an
outer query. The resulting queryis

select distinct course id from section where semester=’Fall’ and year=2009 and course id in
(select course id from section where semester=’Spring’ and year=2010);

28
We use the notin construct in a way similar to the in construct. For example, to find all the
courses taught in the Fall 2009 semester but not in the Spring 2010 semester,we can write:

select distinct course id from section where semester=’Fall’ and year=2009 and course id not in
(select course id from section where semester=’Spring’ and year=2010);

b) Set Comparison
As an example of the ability of a nested subquery to compare sets, consider the query “Find the
names of all instructors whose salary is greater than at least one instructor in the Biology
department.” In Section 3.4.1, we wrote this query as follows:

select distinct [Link] from instructor as T, instructor as S where [Link] > [Link] and [Link]
name=’Biology’;

SQL does,however,offer an alternative style for writing the preceding [Link] phrase“greater
than at leas tone”is represented in SQLby > [Link] construct allows us to rewrite the query
in a form that resembles closely our formulation of the query in English.

select name from instructor where salary > some (select salary from instructor where dept
name=’Biology’);

SQL also allows < some, <= some, >= some, = some, and<> some comparisons. As an exercise,
verify that=some is identical to in, whereas<> some is not the same as not in.8 Now we modify
our query slightly. Let us find the names of all instructors that have a salary value greater than
that of each instructorin the Biology department. Theconstruct > all corresponds to the phrase
“greater than all.”Using this construct, we write the query as follows:

select name from instructor where salary > all (select salary from instructor where dept
name=’Biology’);

As it does for some, SQL also allows < all, <= all, >= all, = all, and<> all comparisons. As an
exercise, verify that <> all is identical to not in, whereas= all is not the same as in.

VIEWS
We define a view in SQL by using the create view command. To define a view, we
must give the view a name and must state the query that computes the view. The form of the
create view command is
create view v as <query expression> where <query expression> is any legal query expression.
The view name is represented by v. Observe that the notation that we used for view definition
in the relational algebra. Consider the view consisting of branch names and the names of
customers who have either an account or a loan at that branch. Assume that we want this view
to be called all-customer. We define this view as follows:
create view all-customer as
(select branch-name, customer-name
from depositor, account
where [Link]-number = [Link]-number)
union
(select branch-name, customer-name
from borrower, loan
where [Link]-number = [Link]-number)

29
The attribute names of a view can be specified explicitly as follows:
create view branch-total-loan(branch-name, total-loan) as
select branch-name, sum(amount)
from loan groupby branch-name
The preceding view gives for each branch the sum of the amounts of all the loans
at the branch. Since the expression sum(amount) does not have a name, the attribute name is
specified explicitly in the view definition. View names may appear in any place that a relation
name may appear. Using the view all-customer, we can find all customers of the Perryridge
branch by writing
select customer-name
from all-customer
where branch-name = ’Perryridge’

Conclusion: In this assignment we have create student database using SQL DML queries for
suitable database using all types of Join, Sub-Query and View .

VIVA QUESTIONS:

1. What is a join?
2. What kinds of joins do you know? Give examples.
3. How do you add record to a table?
4. How do you add a column to a table?
5. What does COMMIT do?
6. What is the main role of a primary key in a table?
7. What is a primary key?
8. What are foreign keys?
9. What is natural Join?
10. What is inner join?
11. What is outer join?
12. What is left and right outer join ?

REFERENCES:

1. Database System Concepts Fifth Edition Avi Silberschatz Henry F. Korth S. Sudarshan
2. Sql, Pl/Sq Lthe Programming Language Of Oracle by ivan bayross
3. [Link]

30

You might also like