Вы находитесь на странице: 1из 1

Joining two tables to solve an SQL query

Joining two tables to solve an SQL query


Assume that there is a database with three tables as the following.

1. Branch (with different attributes as Branch_Name, City; Branch_Name is a primary key.)


2. Assets (with different attributes as Branch_Name, Facility; Branch_Name is a primary key and a foreign
key in this table.)
3. Loan (with different attributes as Loan_No, Amount, Branch_Name; Loan_No is a primary key and
Branch_Name is a foreign key in this table.)

Branch Table

Branch_Name City
ABC Panjim
DEF Mumbai
PQR Pune
RST Mumbai
XYZ Delhi
Loan Table

Loan_No Branch_Name Amount


100 ABC 10000
200 DEF 25000
300 PQR 4000
400 RST 8000
500 XYZ 12000
Assets Table

Branch_Name Facility
ABC Fixed Deposit
DEF Net Banking
PQR Demand Draft
RST Fixed Deposit
XYZ Demand Draft

Question: Write an SQL query to display all cities that have at least the “Fixed Deposit” facility.

1. SELECT B.City
2. FROM Branch AS B, Assets AS A
3. WHERE B.Branch_Name=A.Branch_Name AND A.Facility='Fixed Deposit'
4. ORDER BY B.CITY;

Explanation:

1. We select City attribute to display an output


2. We refer data from two tables (Branch and Assets) Fixed Deposit ----> Branch_Name ----> City
3. We selected above two tables (Branch and Assets) because we can reach to our requirement (Fixed
Deposit) using these tables (Branch and Assets). Therefore, we have written in third statement
accordingly.
4. We want to sort the output in one order (by default ascending).

Output:

City
Mumbai
Panjim

Вам также может понравиться