Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Branch-schema = (branch-name, branch-city, assets)

Customer-schema = (customer-name, customer-street, customer-city)


Loan-schema = (loan-number, branch-name, amount)
Borrower-schema = (customer-name, loan-number)
Account-schema = (account-number, branch-name, balance)
Depositor-schema = (customer-name, account-number)

{Note: For below given Objectives use the above tables as


your database to solve queries. Each table should have at least
7 entries}

Practical .5
Objective:- To Implement rename operation and tuple variable.
Syntax: old-name as new-name

Query: Find the customer names, loan numbers, and loan amounts for all loans at the “XYZ”
branch. {Note: we want the attribute name loan-number to be replaced with the name loan-id}

Query: Find the names of all branches that have assets greater than at least one branch located in
“Jaipur”.

Practical .6

Objective:- To Implement String operation on the table.


• ’Perry%’ matches any string beginning with “Perry”.
• ’%idge%’ matches any string containing “idge” as a substring
• ’_ _ _ ’ matches any string of exactly three characters.
• ’ _ _ _%’ matches any string of at least three characters.

Query: Find the names of all customers whose street address includes the substring ‘path’.
Query: Find the names of all customers whose name starts with “A”.
Query: Find the names of all customers whose name ends with “k”.
Query: Find the names of all customers in which the second character is ‘a’.
Practical .7

Objective:- To Implement ordering the display of tuples.

ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order. The
ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

ASC : Optional. ASC sorts the result set in ascending order by expression. This is the default
behavior, if no modifier is provider.
DESC: Optional. DESC sorts the result set in descending order by expression.

Query: List in alphabetic order all customers who have a loan at the “XYZ” branch.
Query: List the entire loan relation in descending order of amount and if several loans have
the same amount, order them in ascending order by loan number.
Practical .8

Objective:- To Implement set operations on the tables.


The SQL operations union, intersect, and except operate on relations and correspond to

the mathematical set-theory operations ∪, ∩, and −.

SQL UNION Operator


The UNION operator is used to combine the result-set of two or more SELECT statements.
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL Syntax
The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

SQL Intersect operator


 It is used to combine two SELECT statements. The Intersect operation returns the common
rows from both the SELECT statements.
 In the Intersect operation, the number of datatype and columns must be the same.
 It has no duplicates and it arranges the data in ascending order by default.
 If we want to retain all duplicates, we must write intersect all in place of intersect.

Intersect Syntax
SELECT column_name(s) FROM table1
INTERSECT
SELECT column_name(s) FROM table2;

SQL Except operator


 It combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
 It has no duplicates and data arranged in ascending order by default.
 If we want to retain duplicates, we must write except all in place of except.
Except Syntax:
SELECT column_name(s) FROM table1
EXCEPT
SELECT column_name(s) FROM table2;

Query: Find all customers having a loan, an account, or both at the bank.
Query: Find all customers who have both a loan and an account at the bank.
Query: Find all customers who have an account but no loan at the bank.
Practical .9

Objective:- To Implement the concept of aggregate functions, group by and having


clause in SQL.
Aggregate functions are functions that take a collection (a set or multiset) of values as input and
return a single value. SQL offers five built-in aggregate functions:
• Average: avg
• Minimum: min
• Maximum: max
• Total: sum
• Count: count

SQL GROUP BY Statement


The GROUP BY statement group’s rows that have the same values into summary rows, like
"find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM,
AVG) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

SQL HAVING Clause


The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
HAVING Syntax
SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Query: Find the average account balance at the “XYZ” branch.


Query: Find the average account balance at each branch.
Query: Find the number of depositors for each branch.
Query: Find those branches where the average account balance is more
than 12000.
Query: Find the average balance for each customer who lives in “XYZ” city
and has at least three accounts.”
Practical .10

Objective:- To Implement the concept of Nested Queries in SQL

Set Membership(in, not in operator)


Query: Find all customers who have both a loan and an account at the bank.
Query: Find all customers who do have a loan at the bank, but do not have an
account at the bank.
Query: selects the names of customers who have a loan at the bank, and whose
names are neither “Abhishek” nor “Ram”.

Set Comparision
(SQL allows < some, <= some, >= some, = some, and<> some comparisons,
< all, <= all, >= all, = all, and <> all comparisons

Query: Find the names of all branches that have assets greater than those of at
least one branch located in “XYZ”.
Query: Find the names of all branches that have an asset value greater than that of
each branch in "XYZ".
Query: Find the branch that has the highest average balance.

Test for Empty Relations (exists)


SQL includes a feature for testing whether a subquery has any tuples in its
result.

Query : Find all customers who have both an account and a loan at the
bank.
Practical .11

Objective:- To Implement the concept of View.

Query: Create a 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.
Practical .12
Objective:- To perform different operations with SQL join.
INNER JOIN: The INNER JOIN keyword selects records that have matching values in both tables.
The syntax of INNER JOIN statement is:
SELECT column_name(s)
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;

NATURAL JOIN: Syntax


SELECT *
FROM table1
NATURAL JOIN table2;

OUTER JOIN:
LEFT OUTER JOIN:
The LEFT OUTER JOIN keyword returns all records from the left table (table1), and the matched
records from the right table (table2). The result is NULL from the right side, if there is no match.

The syntax of LEFT OUTER JOIN statement is:


SELECT column_name(s)
FROM table1 LEFT OUTER JOIN table2
ON table1.column_name = table2.column_name;

RIGHT OUTER JOIN:


The RIGHT OUTER JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side, when there is
no match.
The syntax of RIGHT OUTER JOIN statement is:
SELECT column_name(s)
FROM table1 RIGHT OUTER JOIN table2
ON table1.column_name = table2.column_name;

FULL OUTER JOIN:


The FULL OUTER JOIN keyword return all records when there is a match in either left (table1)
or right (table2) table records.

The syntax of FULL OUTER JOIN statement is:


SELECT column_name(s)
FROM table1 FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
OR
SELECT column_name(s)
FROM table1 LEFT OUTER JOIN table2
ON table1.column_name = table2.column_name;
UNION
SELECT column_name(s)
FROM table1 RIGHT OUTER JOIN table2
ON table1.column_name = table2.column_name;

You might also like