Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

SQL commands-converted

Uploaded by

Aayush Talukdar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL commands-converted

Uploaded by

Aayush Talukdar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CLASS XII : CS CHAPTER:SQL COMMANDS:

Create Table: This command is used to create a new table in the Database. You must be in
the same database where you want to create the table.(DDL)
Syntax: CREATE TABLE table_name (column1 datatype, column2 datatype, column3
datatype)
Example. CREATE TABLE Persons ( PersonID int, LastName varchar(255))
Drop Table: Drop a Table. The drop table command is used to delete a table and all rows
in the table. To delete an entire table including all of its rows, issue the drop
table command followed by the tablename. Dropping the table removes the table definition
as well as all of its rows.(DDL)
Syntax:DROP TABLE table_name;
Example: DROP TABLE PERSONS;
Alter Table:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.(DDL)
Let us first see the Add column:

Alter Table - Add Column


To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
The following SQL command adds an "Email" column to the "Customers" table:
Example
ALTER TABLE Customers
ADD Email varchar(255);

Alter Table - Drop Column


To delete a column in a table, use the following syntax
ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Email" column from the "Customers" table:
Example

ALTER TABLE Customers


DROP COLUMN Email;
Alter Table - Alter/Modify Column
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
SQL ALTER TABLE Example
Look at the "Persons" table:
ID LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Now we want to add a column named "DateOfBirth" in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons
ADD DateOfBirth date;
The "Persons" table will now look like this:
ID LastName FirstName Address City DateOfBirth
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Change Data Type:
Example
Now we want to change the data type of the column named "DateOfBirth" in the "Persons"
table.
We use the following SQL statement:
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a
two- or four-digit format.
Drop Column :
Example
we want to delete the column named "DateOfBirth" in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons
DROP COLUMN DateOfBirth;
The "Persons" table will now look like this:
ID LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Update ....Set....,
The SQL UPDATE Query is used to modify the existing records in a table. You can use the
WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows
would be affected.
Syntax:
The basic syntax of the UPDATE query with a WHERE clause is as follows −
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
You can combine N number of conditions using the AND or the OR operators.

Example:

Consider the CUSTOMERS table having the following records −


+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The following query will update the ADDRESS for a customer whose ID number is 6 in the
table.
UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;
Now, the CUSTOMERS table would have the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Pune | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to modify all the ADDRESS and the SALARY column values in the
CUSTOMERS table, you do not need to use the WHERE clause as the UPDATE query
would be enough as shown in the following code block.
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune', SALARY = 1000.00;
Now, CUSTOMERS table would have the following records −
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 1 | Ramesh | 32 | Pune | 1000.00 |
| 2 | Khilan | 25 | Pune | 1000.00 |
| 3 | kaushik | 23 | Pune | 1000.00 |
| 4 | Chaitali | 25 | Pune | 1000.00 |
| 5 | Hardik | 27 | Pune | 1000.00 |
| 6 | Komal | 22 | Pune | 1000.00 |
| 7 | Muffy | 24 | Pune | 1000.00 |
Insert Into:
The SQL INSERT INTO Statement is used to add new rows of data to a table in the
database.

Syntax

There are two basic syntaxes of the INSERT INTO statement which are shown below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Here, column1, column2, column3,...columnN are the names of the columns in the table into
which you want to insert the data.
You may not need to specify the column(s) name in the SQL query if you are adding values
for all the columns of the table. But make sure the order of the values is in the same order as
the columns in the table.
The SQL INSERT INTO syntax will be as follows −
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
Example
The following statements would create six records in the CUSTOMERS table.
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
You can create a record in the CUSTOMERS table by using the second syntax as shown
below.
INSERT INTO CUSTOMERS
VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
Populate one table using another table:
You can populate the data into a table through the select statement over another table;
provided the other table has a set of fields, which are required to populate the first table.
Here is the syntax −
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM second_table_name
[WHERE condition];
Example: insert into selected as select * from test_marks where marks>50;
The SQL DELETE Statement:
The DELETE statement is used to delete existing records in a table.
Syntax:
DELETE FROM table_name WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
Customer CustomerName ContactName Address City PostalCode Country
ID
1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8

Example
The following SQL statement deletes the customer "Alfreds Futterkiste" from the
"Customers" table:
Example
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
Select:
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If
you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
Customer CustomerName ContactName Address City PostalCode Country
ID
1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8

SELECT Column Example:


The following SQL statement selects the "CustomerName" and "City" columns from the
"Customers" table:
Example:
SELECT CustomerName, City FROM Customer
SELECT * Example
The following SQL statement selects all the columns from the "Customers" table:
Example
SELECT * FROM Customers;
The SQL SELECT DISTINCT Statement:
The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want
to list the different (distinct) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Demo Database:Below is a selection from the "Customers" table in the Northwind sample
database:
Customer CustomerName ContactName Address City PostalCode Country
ID
1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8
SELECT Example Without DISTINCT
The following SQL statement selects ALL (including the duplicates) values from the
"Country" column in the "Customers" table:
Example
SELECT Country FROM Customers;
From:
The FROM command is used to specify which table to select or delete data from.

The following SQL statement selects the "CustomerName" and "City" columns from the
"Customers" table:
Example
SELECT CustomerName, City FROM Customers;

The following SQL statement selects all the columns from the "Customers" table:
Example
SELECT * FROM Customers;Try it Yourself »
The following SQL statement deletes the customer "Alfreds Futterkiste" from the
"Customers" table:
Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';

The WHERE Clause:


The WHERE clause is used to filter records.
The WHERE clause is used to extract only those records that fulfill a specified condition.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: The WHERE clause is not only used in SELECT statement, it is also used in
UPDATE, DELETE statement, etc.!
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
Customer CustomerName ContactName Address City PostalCode Country
ID
1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8
WHERE Clause Example:
The following SQL statement selects all the customers from the country "Mexico", in the
"Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double
quotes).
However, numeric fields should not be enclosed in quotes:
Example
SELECT * FROM Customers
WHERE CustomerID=1;
Operators in The WHERE Clause:
The following operators can be used in the WHERE clause:
Operat Description
or
= Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

Not equal. Note: In some versions of SQL this


<>
operator may be written as !=

BETWEEN Between a certain range

LIKE Search for a pattern

IN To specify multiple possible values for a column

The SQL IN Operator:


The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.

IN Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

The following SQL statement selects all customers that are located in "Germany", "France"
or "UK":
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
The SQL BETWEEN Operator:
The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2

The following SQL statement selects all products with a price BETWEEN 10 and 20:
Example

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20;
The SQL LIKE Operator :

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters
• _ - The underscore represents a single character
• The percent sign and the underscore can also be used in combinations!
• LIKE Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Tip: You can also combine any number of conditions using AND or OR operators.

Here are some examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
characters in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
The following SQL statement selects all customers with a CustomerName starting with "a":
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

The following SQL statement selects all customers with a CustomerName ending with "a":
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
The following SQL statement selects all customers with a CustomerName that have "or" in
any position:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
The following SQL statement selects all customers with a CustomerName that starts with "a"
and are at least 3 characters in length:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';

Null / is Null:
What is a NULL Value?
A field with a NULL value is a field with no value.If a field in a table is optional, it is
possible to insert a new record or update a record without adding a value to this field. Then,
the field will be saved with a NULL value.
A NULL value is different from a zero value or a field that contains spaces. A field with a
NULL value is one that has been left blank during record creation!
How to Test for NULL Values?
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
The IS NULL Operator
The IS NULL operator is used to test for empty values (NULL values).
The following SQL lists all customers with a NULL value in the "Address" field:
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
The IS NOT NULL Operator
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
The following SQL lists all customers with a value in the "Address" field:
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;

Order by :
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.
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:
SELECT * FROM Customers
ORDER BY Country;
ORDER BY DESC Example
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
SELECT * FROM Customers
ORDER BY Country DESC;
ORDER BY Several Columns Example
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column. This means that it orders by Country, but if
some rows have the same Country, it orders them by CustomerName:
SELECT * FROM Customers
ORDER BY Country, CustomerName;
ORDER BY Several Columns Example 2
The following SQL statement selects all customers from the "Customers" table, sorted
ascending by the "Country" and descending by the "CustomerName" column:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
The SQL GROUP BY Statement:
The GROUP BY statement groups 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);
Demo table:
CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


5 snabbköp Berglund 8

The following SQL statement lists the number of customers in each country:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
The following SQL statement lists the number of customers in each country, sorted high to
low:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

The 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);
Below is a selection from the "Customers" table in the Northwind sample database:
CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 5023 Mexico
Taquería Moreno 2312 D.F.
4 Around the Thomas 120 Hanover London WA1 1DP UK
Horn Hardy Sq.

Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


5 snabbköp Berglund 8
SQL HAVING Examples

The following SQL statement lists the number of customers in each country. Only include
countries with more than 5 customers:

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
The following SQL statement lists the number of customers in each country, sorted high to
low (Only include countries with more than 5 customers):
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
SQL Aggregate functions: SUM ( ), AVG ( ), COUNT ( ), MAX ( ) and MIN ( ):
MySQL aggregate functions retrieve a single value after performing a calculation on a set of
values.
In general, aggregate functions ignore null values.
Often, aggregate functions are accompanied by the GROUP BY clause of the SELECT
statement.
List of MySQL aggregate functions and a hint of what they do
AVG()
MySQL AVG() retrieves the average value of the argument.
COUNT()
MySQL COUNT() retrieves a count of the number of rows returned.
MAX()
MySQL MAX() retrieves the maximum value.
MIN()
MySQL BIT_OR() retrieves the minimum value.
SUM()
MySQL SUM() retrieves the sum.
MySQL aggregate function examples

We will use the products and orderdetails tables from the sample database for demonstration:
MySQL aggregate function – AVG() function examples

The AVG() function calculates the average value of a set of values. It ignores NULL in the
calculation.
AVG(expression)
For example, you can use the AVG function to calculate the average buy price of all products
in the products table by using the following query:

SELECT
AVG(buyPrice) average_buy_price
FROM
products;

MySQL aggregate function – COUNT() function examples

The COUNT() function returns the number of the value in a set.


For example, you can use the COUNT() function to get the number of products in
the products table as shown in the following query:
SELECT
COUNT(*) AS total
FROM
products;

The following statement uses the COUNT() function with the GROUP BY clause to get the
number of products for each product line:

SELECT
productLine,
COUNT(*)
FROM
products
GROUP BY productLine
ORDER BY productLine;

SUM() function examples

The SUM() function returns the sum of values in a set. The SUM() function ignores NULL. If
no matching row found, the SUM() function returns NULL.
To get the total order value of each product, you can use the SUM() function in conjunction
with the GROUP BY clause as follows:
SELECT
productCode,
SUM(priceEach * quantityOrdered) total
FROM
orderDetails
GROUP BY productCode
ORDER BY total DESC;

MAX() function examples:

The MAX() function returns the maximum value in a set.


For example, you can use the MAX() function to get the highest buy price from
the products table as shown in the following query:
SELECT
MAX(buyPrice) highest_price
FROM
products;

The following statement uses the MAX() function with the GROUP BY clause to get the
highest price per product line:

SELECT
productLine, MAX(buyPrice)
FROM
products
GROUP BY productLine
ORDER BY MAX(buyPrice) DESC;

MIN() function examples

The MIN() function returns the minimum value in a set of values.


MIN(expression)
For example, the following query uses the MIN() function to find the lowest price from
the products table:
SELECT
MIN(buyPrice) lowest_price
FROM
products;
The following example uses the MIN() function with the GROUP BY clause to get the lowest
price per product line:
SELECT
productLine,
MIN(buyPrice)
FROM
products
GROUP BY productLine
ORDER BY MIN(buyPrice);

Joins: equi-join and natural join

A SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them.
What is Equi Join in SQL?

SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the
associated tables. An equal sign (=) is used as comparison operator in the where clause to
refer equality.
Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;

Example:
Here is an example of Equi Join in SQL.
Sample table: agents
AGENT_CODE ,AGENT_NAME , WORKING_AREA , COMMISSION, PHONE_NO ,
COUNTRY
Sample table: customer
,CUST_CODE , CUST_NAME , CUST_CITY , WORKING_AREA ,
CUST_COUNTRY , GRADE , OPENING_AMT , RECEIVE_AMT , PAYMENT_AMT
,OUTSTANDING_AMT , PHONE_NO , AGENT_CODE

To get agent name column from agents table and cust name and cust city columns from
customer table after joining said two tables with the following condition -

1. working area of agents and customer city of customer table must be same,
the following SQL statement can be used:
SQL Code:
SELECT agents.agent_name,customer.cust_name,
customer.cust_city
FROM agents,customer
WHERE agents.working_area=customer.cust_city;
Output:
AGENT_NAME CUST_NAME CUST_CITY
---------------------------------------- ---------------------------------------- ------------
Ravi Kumar Ravindran Bangalore
Ramasundar Ravindran Bangalore
Subbarao Ravindran Bangalore
Ravi Kumar Srinivas Bangalore
Ramasundar Srinivas Bangalore
Subbarao Srinivas Bangalore
Ravi Kumar Rangarappa Bangalore
Ramasundar Rangarappa Bangalore
Subbarao Rangarappa Bangalore
Ravi Kumar Venkatpati Bangalore
Ramasundar Venkatpati Bangalore
Subbarao Venkatpati Bangalore
Anderson Fleming Brisban
Anderson Jacks Brisban
Anderson Winston Brisban
Santakumar Yearannaidu Chennai

Natural Join
The NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns
with the same name of associated tables will appear once only. - The associated tables have
one or more pairs of identically named columns. - The columns must be the same data type.
Natural Joins: The join in which one of the identical columns exist, is called Natural Join.
The natural join is much similar to Equi-Join, records are joined on the equality condition of
joining column except that the common column appears one time.
(After Natural Join following output will be produced)
: MySQL offer two ways by which you may join two or more tables.
One is using multiple tables with FROM clause and using JOIN keyword with FROM clause.
Joining multiple tables with FROM clause
Select * from Student, Stream where student.scode= stream.scode;

Joining multiple tables with JOIN clause

You might also like