0% found this document useful (0 votes)
7 views9 pages

EquiJoin Example

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

EquiJoin Example

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EquiJoin Example

Let us understand how equijoin works in MySQL through examples. Suppose we have
already two tables named customer and balance that contains the following data:

Execute the below equijoin statement for joining tables:

1. mysql> SELECT cust. customer_name, bal.balance


2. FROM customer AS cust, balance AS bal
3. WHERE cust.account = bal.account_num;
We will get the following result:

We can also get the same result by using the below statement:

1. mysql> SELECT cust. customer_name, bal.balance


2. FROM customer AS cust
3. JOIN balance AS bal
4. WHERE cust.account = bal.account_num;
See the below output that is the same as the result returns from the previous query:
Equi Join Using Three Tables
We know that equijoin can also perform a join operation on more than two tables. To
understand this, let us create another table named cust_info using the below statement:

1. CREATE TABLE cust_info (


2. account_no int,
3. mobile VARCHAR(15),
4. address VARCHAR(65)
5. );
Then, we will fill records into this table:

1. INSERT INTO cust_info (account_no, mobile, address)


2. VALUES(1030, '598675498654', '777 Brockton Avenue, Abington MA 251'),
3. (2035, '698853747888', '337 Russell St, Hadley MA 103'),
4. (5564, '234456977555', '20 Soojian Dr, Leicester MA 154'),
5. (4534, '987656789666', '780 Lynnway, Lynn MA 19'),
6. (7648, '756489372222', '700 Oak Street, Brockton MA 23');
We can verify the data using the SELECT statement. See the below image:

To join three tables using equijoin, we need to execute the statement as follows:

1. mysql> SELECT C.customer_name, C.account, B.balance, I.mobile


2. FROM customer AS C, balance AS B, cust_info AS I
3. WHERE C.account = B.account_num
4. AND B.account_num = I.account_no;
It will give the below result.
Difference between Natural Join, Equi Join and Inner Join
Let us summaries the differences between natural, equi and inner join operation in the
tabular form given below:

Natural JoinNatural Join Equi JoinEqui Join Inner JoinInner Join

It joins the tables based on the


It joins the tables based on the
It joins the tables based on the column name specified in the
equality or matching column
same column names and their ON clause explicitly. It returns
values in the associated
data types. only those rows that exist in
tables.
both tables.

It can return all attributes of It returns all the attributes of


It always returns unique both tables along with both tables along with
columns in the result set. duplicate columns that match duplicate columns that match
the join condition. the ON clause condition.

The syntax of equijoin is given The syntax of inner join is


The syntax of a natural join is below: given below:
given below: SELECT column_name (s) SELECT [column_names | *]
FROM table_name1, FROM table_name1
SELECT [column_names | *] table_name2, ...., table_nameN
FROM table_name1 WHERE INNER JOIN table_name2
NATURAL JOIN table_name2; table_name1.column_name = ON table_name1.column_name
table_name2.column_name; = table_name2.column_name;

MySQL Natural Join


When we combine rows of two or more tables based on a common column between them, this
operation is called joining. A natural join is a type of join operation that creates an implicit
join by combining tables based on columns with the same name and data type. It is similar
to the INNER or LEFT JOIN, but we cannot use the ON or USING clause with natural join as we
used in them.
Points to remember:
o There is no need to specify the column names to join.
o The resultant table always contains unique columns.
o It is possible to perform a natural join on more than two tables.
o Do not use the ON clause.

Syntax:
The following is a basic syntax to illustrate the natural join:

1. SELECT [column_names | *]
2. FROM table_name1
3. NATURAL JOIN table_name2;
In this syntax, we need to specify the column names to be included in the result set after
the SELECT keyword. If we want to select all columns from both tables, the * operator will
be used. Next, we will specify the table names for joining after the FROM keyword and
write the NATURAL JOIN clause between them.

Natural Join Example


Let us understand how natural join works in MySQL through examples. First, we will
create two tables named customer and balance using the below statements:

1. /* -- Table name: customer -*/


2. CREATE TABLE customer (
3. id INT AUTO_INCREMENT PRIMARY KEY,
4. customer_name VARCHAR(55),
5. account int,
6. email VARCHAR(55)
7. );
8.
9. /* -- Table name: balance -*/
10. CREATE TABLE balance (
11. id INT AUTO_INCREMENT PRIMARY KEY,
12. account int,
13. balance FLOAT(10, 2)
14. );
Next, we will fill some records into both tables using the below statements:

1. /* -- Data for customer table -*/


2. INSERT INTO customer(customer_name, account, email)
3. VALUES('Stephen', 1030, '[email protected]'),
4. ('Jenifer', 2035, '[email protected]'),
5. ('Mathew', 5564, '[email protected]'),
6. ('Smith', 4534, '[email protected]'),
7. ('David', 7648, '[email protected]');
8.
9. /* -- Data for balance table -*/
10. INSERT INTO balance(account, balance)
11. VALUES(1030, 50000.00),
12. (2035, 230000.00),
13. (5564, 125000.00),
14. (4534, 80000.00),
15. (7648, 45000.00);
Next, we will execute the SELECT statement to verify the table data:
Now, we will see the condition that fulfills the criteria for natural join. We can do this by
examining the table structure using the DESCRIBE statement. See the below image:

In this image, we can see that column names id and account and its data types are the
same that fulfill the natural join criteria. Hence we can use natural join on them.

Execute the below statement for joining tables using natural join:

1. mysql> SELECT cust. customer_name, bal.balance


2. FROM customer AS cust
3. NATURAL JOIN balance AS bal;
We will get the following result:
We can do the same job with the help of INNER JOIN using the ON clause. Here is the
query to explain this join:

1. mysql> SELECT cust. customer_name, bal.balance


2. FROM customer AS cust
3. INNER JOIN balance AS bal
4. ON cust.id = bal.id;
After successful execution, we will get the same result as the natural join:

Now, we will use (*) in the place of column names as follows:

1. mysql> SELECT * FROM customer NATURAL JOIN balance;


Suppose we use the asterisk (*) in the place of column names, then the natural join
automatically searches the same column names and their data types and join them
internally. Also, it does not display the repeated columns in the output. Hence, we should
get the below output after executing the above statement:

Natural Join with WHERE Clause


The WHERE clause is used to return the filter result from the table. The following example
illustrates this with the natural join clause:

1. mysql> SELECT cust. customer_name, bal.balance


2. FROM customer AS cust
3. NATURAL JOIN balance AS bal
4. WHERE bal.balance > 50000;
We will get the following result where customer information is displayed whose account
balance is greater than 50000.
Natural Join Using Three Tables
We know that natural join can also perform a join operation on more than two tables. To
understand this, we will use the syntax as follows:

1. SELECT [column_names | *]
2. FROM table_name1
3. NATURAL JOIN table_name2
4. NATURAL JOIN table_name3;
Let us create another table named cust_info using the below statement:

1. CREATE TABLE cust_info (


2. account int,
3. mobile VARCHAR(15),
4. address VARCHAR(65)
5. );
Then, we will fill records into this table:

1. INSERT INTO cust_info(account, mobile, address)


2. VALUES(1030, '598675498654', '777 Brockton Avenue, Abington MA 251'),
3. (2035, '698853747888', '337 Russell St, Hadley MA 103'),
4. (5564, '234456977555', '20 Soojian Dr, Leicester MA 154'),
5. (4534, '987656789666', '780 Lynnway, Lynn MA 19'),
6. (7648, '756489372222', '700 Oak Street, Brockton MA 23');
We can verify the data using the SELECT statement. See the below image:

To join three tables using natural join, we need to execute the statement as follows:

1. mysql> SELECT C.customer_name, C.account, B.balance, I.mobile


2. FROM customer AS C
3. NATURAL JOIN balance AS B
4. NATURAL JOIN cust_info AS I;
It will give the below result. Here we can see that the account number is present in all
three columns but arrived only once in the output that fulfills the natural join criteria.

Difference between Natural Join and Inner Join

SN Natural Joinral Join Inner Join

It joins the tables based on It joins the tables based on


1. the same column names and the column name specified in
their data types. the ON clause explicitly.

It returns all the attributes of


It always returns unique both tables along with
2.
columns in the result set. duplicate columns that match
the ON clause condition.

If we have not specified any


condition in this join, it returns It returns only those rows that
3.
the records based on the exist in both tables.
common columns.

The syntax of inner join is


given below:
The syntax of natural join is
SELECT [column_names | *]
given below:
FROM table_name1
SELECT [column_names | *]
4. INNER JOIN table_name2
FROM table_name1
ON
NATURAL JOIN
table_name1.column_name
table_name2;
=
table_name2.column_name;

1. mysql> LOCK TABLE info_table READ;


https://csiplearninghub.com/python-file-handling-programs-class-12/#T1

https://www.learnpython4cbse.com/practical-file-programs-c-sc-xii

https://www.shaalaa.com/search-important-solutions/cbse-class-12-senior-school-cert-examination-
science_1424?subjects=computer-science-python_2869

punc = ‘’’ ! () – [] {} : ; ‘ “ \ , <> . / ? @ # $ % ^ &


*_ ‘’’

str_1 = input (“Enter the string: “)

no_punc = “ “

for char in str_1:

if char not in punc:

no_punc = no_punc + clear

print (no_punc)

You might also like