0% found this document useful (0 votes)
45 views

Writeups - Assignment 4

The document discusses various SQL join types including inner join, outer join, left join, right join, full join, and cross join. Examples are provided to demonstrate each join type using sample tables.

Uploaded by

ganesh bagul
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)
45 views

Writeups - Assignment 4

The document discusses various SQL join types including inner join, outer join, left join, right join, full join, and cross join. Examples are provided to demonstrate each join type using sample tables.

Uploaded by

ganesh bagul
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/ 32

Assignment 4

Title: Write SQL queries to demonstrate the use of concepts like join, sub-query and views.

Theory:

SQL JOIN

As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".

The SQL JOIN clause takes records from two or more tables in a database and combines it
together.

ANSI standard SQL defines five types of JOIN :

1. inner join,

2. left outer join,

3. right outer join,

4. full outer join, and

5. cross join.

In the process of joining, rows of both tables are combined in a single table.

If you want to access more than one table through a select statement. If you want to combine two
or more table then SQL JOIN statement is used .it combines rows of that tables in one table and
one can retrieve the information by a SELECT statement. The joining of two or more tables is
based on common field between them. SQL INNER JOIN also known as simple join is the most
common type of join.

Let an example to deploy SQL JOIN process:

1.Staff table

ID Staff_NAME Staff_AGE STAFF_ADDRES Monthley_Package


S

1 ARYAN 22 MUMBAI 18000

2 SUSHIL 32 DELHI 20000

3 MONTY 25 MOHALI 22000

4 AMIT 20 ALLAHABAD 12000

2.Payment table

Payment_ID DATE Staff_ID AMOUNT

101 30/12/2009 1 3000.00

102 22/02/2010 3 2500.00

103 23/02/2010 4 3500.00

So if you follow this JOIN statement to join these two tables ?

SELECT Staff_ID, Staff_NAME, Staff_AGE, AMOUNT

FROM STAFF s, PAYMENT p

WHERE s.ID =p.STAFF_ID;

This will produce the result like this:


STAFF_ID NAME Staff_AGE AMOUNT

3 MONTY 25 2500

1 ARYAN 22 3000

4 AMIT 25 3500

1 ARYAN 22 3000

SQL OUTER JOIN

In the SQL outer JOIN all the content of the both tables are integrated together either they are
matched or not.

If you take an example of employee table

Outer join of two types:

1.Left outer join (also known as left join): this join returns all the rows from left table combine
with the matching rows of the right table. If you get no matching in the right table it returns
NULL values.

2.Right outer join (also known as right join): this join returns all the rows from right table are
combined with the matching rows of left table .If you get no column matching in the left table .it
returns null value.

SQL LEFT JOIN

The SQL left join returns all the values from the left table and it also includes matching values
from right table, if there are no matching join value it returns NULL.

BASIC SYNTAX FOR LEFT JOIN:

SELECT table1.column1, table2.column2....

FROM table1

LEFTJOIN table2
ON table1.column_field = table2.column_field;

let us take two tables in this example to elaborate all the things:

CUSTOMER TABLE:

ID NAME AGE SALARY

1 ARYAN 51 56000

2 AROHI 21 25000

3 VINEET 24 31000

4 AJEET 23 32000

5 RAVI 23 42000

This is second table

ORDER TABLE:

O_ID DATE CUSTOMER_ID AMOUNT

001 20-01-2012 2 3000

002 12-02-2012 2 2000

003 22-03-2012 3 4000

004 11-04-2012 4 5000

join these two tables with LEFT JOIN:


SQL SELECT ID, NAME, AMOUNT,DATE

FROM CUSTOMER

LEFT JOIN ORDER

ON CUSTOMER.ID = ORDER.CUSTOMER_ID;

This will produce the following result:

ID NAME AMOUNT DATE

1 ARYAN NULL NULL

2 AROHI 3000 20-01-2012

2 AROHI 2000 12-02-2012

3 VINEET 4000 22-03-2012

4 AJEET 5000 11-04-2012

5 RAVI NULL NULL

SQL RIGHT JOIN

The SQL right join returns all the values from the rows of right table. It also includes the
matched values from left table but if there is no matching in both tables, it returns NULL.

Basic syntax for right join:

SELECT table1.column1, table2.column2.....

FROM table1

RIGHT JOIN table2

ON table1.column_field = table2.column_field;

let us take an example with 2 tables table1 is CUSTOMERS table and table2 is ORDERS table.
CUSTOMER TABLE:

ID NAME AGE SALARY

1 ARYAN 51 56000

2 AROHI 21 25000

3 VINEET 24 31000

4 AJEET 23 32000

5 RAVI 23 42000

and this is the second table:

ORDER TABLE:

DATE O_ID CUSTOMER_ID AMOUNT

20-01-2012 001 2 3000

12-02-2012 002 2 2000

22-03-2012 003 3 4000

11-04-2012 004 4 5000

Here we will join these two tables with SQL RIGHT JOIN:

SQL> SELECT ID,NAME,AMOUNT,DATE

FROM CUSTOMER

RIGHT JOIN ORDER

ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
ID NAME AMOUNT DATE

2 AROHI 3000 20-01-2012

2 AROHI 2000 12-02-2012

3 VINEET 4000 22-03-2012

4 AJEET 5000 11-04-2012

SQL FULL JOIN

The SQL full join is the result of combination of both left and right outer join and the join tables
have all the records from both tables. It puts NULL on the place of matches not found.

SQL full outer join and SQL join are same. generally it is known as SQL FULL JOIN.

SQL full outer join:

What is SQL full outer join?

SQL full outer join is used to combine the result of both left and right outer join and returns all
rows (don't care its matched or unmatched) from the both participating tables.

Syntax for full outer join:

SELECT *

FROM table1

FULL OUTER JOIN table2

ON table1.column_name = table2.column_name;

Note:here table1 and table2 are the name of the tables participating in joining and column_name
is the column of the participating tables.

Let us take two tables to demonstrate full outer join:

table_A
A M

1 m

2 n

4 o

table_B

A N

2 p

3 q

5 r

Resulting table

A M A N

2 n 2 p

1 m - -

4 o - -

- - 3 q

- - 5 r
Because this is a full outer join so all rows (both matching and non-matching) from both tables
are included in the output. Here only one row of output displays values in all columns because
there is only one match between table_A and table_B.

SQL Cross Join

o Join operation in SQL is used to combine multiple tables together into a single table.
o If we use the cross join to combine two different tables, then we will get the Cartesian
product of the sets of rows from the joined table. When each row of the first table is
combined with each row from the second table, it is known as Cartesian join or cross
join.
o After performing the cross join operation, the total number of rows present in the final
table will be equal to the product of the number of rows present in table 1 and the number
of rows present in table 2.
o For example:
If there are two records in table 1 and three records in table 2, then after performing cross
join operation, we will get six records in the final table.
o Let us take a look at the syntax of writing a query to perform the cross join operation in
SQL.

SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1 CROS


S JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;

Now let us see take a deeper dive into the cross join in SQL with the help of examples. All the
queries in the examples will be written using the MySQL database.

Consider we have the following tables with the given data:

Table 1: MatchScore

Player Department_id Goals

Franklin 1 2

Alan 1 3

Priyanka 2 2

Rajesh 3 5
Table 2: Departments

Department_id Department_name

1 IT

2 HR

3 Marketing

Table 3: employee

EmployeeID Employee_Name Employee_Salary

1 Arun Tiwari 50000

2 Sachin Rathi 64000

3 Harshal Pathak 48000

4 Arjun Kuwar 46000

5 Sarthak Gada 62000

Table 4: department

DepartmentID Department_Name Employee_ID

1 Production 1

2 Sales 3

3 Marketing 4
4 Accounts 5

Table 5: loan

LoanID Branch Amount

1 B1 15000

2 B2 10000

3 B3 20000

4 B4 100000

Table 6: borrower

CustID CustName LoanID

1 Sonakshi Dixit 1

2 Shital Garg 4

3 Swara Joshi 5

4 Isha Deshmukh 2

Table 7: customer

Customer_ID Name Age Salary

1 Aryan Jain 51 56000

2 Arohi Dixit 21 25000


3 Vineet Garg 24 31000

Table 8: orders

Order_ID Order_Date Cutomer_ID Amount

1 2012-01-20 2 3000

2 2012-05-18 2 2000

3 2012-06-28 3 4000

Example 1:

Write a query to perform the cross join operation considering the MatchScore table as the left
table and the Departments table as the right table.

Query:

SELECT * FROM MatchScore CROSS JOIN Departments;

We have used the SELECT command with the asterisk to retrieve all the columns present in the
MatchScore and Departments table. Then we have used the CROSS JOIN keyword to perform
the cross join operation on the MatchScore and Departments table. Since there are 4 records in
the MatchScore and 3 records in the Departments table, after performing the cross join operation,
we will get 12 rows.

After executing this query, you will find the following result:

Player Department_id Goals Depatment_id Department_name

Franklin 1 2 1 IT

Alan 1 3 1 IT

Priyanka 2 2 1 IT
Rajesh 3 5 1 IT

Franklin 1 2 2 HR

Alan 1 3 2 HR

Priyanka 2 2 2 HR

Rajesh 3 5 2 HR

Franklin 1 2 3 Marketing

Alan 1 3 3 Marketing

Priyanka 2 2 3 Marketing

Rajesh 3 5 3 Marketing

Each row from the MatchScore table is combined with each row of the Departments table. Since
there are four records in the MatchScore and three records in the Departments table, we have got
12 rows in the final table after performing the cross join operation.

Example 2:

Write a query to perform the cross join operation considering the employee table as the left table
and the department table as the right table.

Query:

SELECT *FROM employee CROSS JOIN department;

We have used the SELECT command with the asterisk to retrieve all the columns present in the
employee and department table. Then we have used the CROSS JOIN keyword to perform the
cross join operation on the employee and department table. Since there are five records in the
employee and four records in the department table, after performing the cross join operation, we
will get 20 rows.
After executing this query, you will find the following result:

Employ Employee_ Employee_ Departme Department_ Employe


eeID Name Salary ntID Name e_ID

1 Arun Tiwari 50000 1 Production 1

1 Arun Tiwari 50000 2 Sales 3

1 Arun Tiwari 50000 3 Marketing 4

1 Arun Tiwari 50000 4 Accounts 5

2 Sachin Rathi 64000 1 Production 1

2 Sachin Rathi 64000 2 Sales 3

2 Sachin Rathi 64000 3 Marketing 4

2 Sachin Rathi 64000 4 Accounts 5

3 Harshal 48000 1 Production 1


Pathak

3 Harshal 48000 2 Sales 3


Pathak

3 Harshal 48000 3 Marketing 4


Pathak

3 Harshal 48000 4 Accounts 5


Pathak

4 Arjun Kuwar 46000 1 Production 1


4 Arjun Kuwar 46000 2 Sales 3

4 Arjun Kuwar 46000 3 Marketing 4

4 Arjun Kuwar 46000 4 Accounts 5

5 Sarthak Gada 62000 1 Production 1

5 Sarthak Gada 62000 2 Sales 3

5 Sarthak Gada 62000 3 Marketing 4

5 Sarthak Gada 62000 4 Accounts 5

Each row from the employee table is combined with each row of the department table. Since
there are five records in the employee table and four records in the department table, we have got
20 rows in the final table after performing the cross join operation.

Example 3:

Write a query to perform the cross join operation considering the loan table as the left table and
the borrower table as the right table.

Query:

1. mysql> SELECT *FROM loan CROSS JOIN borrower;

We have used the SELECT command with the asterisk to retrieve all the columns present in the
loan and the borrower table. Then we have used the CROSS JOIN keyword to perform the cross
join operation on the loan and the borrower table. Since there are four records in the loan table
and four records in the borrower table, after performing the cross join operation, we will get 16
rows.

After executing this query, you will find the following result:
LoanID Branch Amount CustID CustName LoanID

1 B1 15000 1 Sonakshi Dixit 1

2 B2 10000 1 Sonakshi Dixit 1

3 B3 20000 1 Sonakshi Dixit 1

4 B4 100000 1 Sonakshi Dixit 1

1 B1 15000 2 Shital Garg 4

2 B2 10000 2 Shital Garg 4

3 B3 20000 2 Shital Garg 4

4 B4 100000 2 Shital Garg 4

1 B1 15000 3 Swara Joshi 5

2 B2 10000 3 Swara Joshi 5

3 B3 20000 3 Swara Joshi 5

4 B4 100000 3 Swara Joshi 5

1 B1 15000 4 Isha Deshmukh 2

2 B2 10000 4 Isha Deshmukh 2

3 B3 20000 4 Isha Deshmukh 2

4 B4 100000 4 Isha Deshmukh 2


Each row from the loan table is combined with each row of the borrower table. Since there are
four records in the loan table and four records in the borrower table, after performing the cross
join operation, we have got 16 rows.

Example 4:

Write a query to perform the cross join operation considering the customer table as the left table
and the orders table as the right table.

Query:

SELECT *FROM customer CROSS JOIN orders;

We have used the SELECT command with the asterisk to retrieve all the columns present in the
customer and orders table. Then we have used the CROSS JOIN keyword to perform the cross
join operation on the customer table and the orders table. Since there are three records in the loan
table and three records in the orders table, after performing the cross join operation, we will get 9
rows.

After executing this query, you will find the following result:

Customer_I Nam Ag Salar Order_I Order_Dat Customer_I Amoun


D e e y D e D t

1 Aryan 51 56000 1 2012-01-20 2 3000


Jain

2 Arohi 21 25000 1 2012-01-20 2 3000


Dixit

3 Vineet 24 31000 1 2012-01-20 2 3000


Garg

1 Aryan 51 56000 2 2012-05-18 2 2000


Jain

2 Arohi 21 25000 2 2012-05-18 2 2000


Dixit

3 Vineet 24 31000 2 2012-05-18 2 2000


Garg

1 Aryan 51 56000 3 2012-06-28 3 4000


Jain

2 Arohi 21 25000 3 2012-06-28 3 4000


Dixit

3 Vineet 24 31000 3 2012-06-28 3 4000


Garg

Each row from the customers' table is combined with each row of the orders table. Since there
are three records in the loan table and three records in the orders table, after performing the cross
join operation, we will get 9 rows.

SQL View

SQL provides the concept of VIEW, which hides the complexity of the data and restricts
unnecessary access to the database. It permits the users to access only a particular column rather
than the whole data of the table.

The View in the Structured Query Language is considered as the virtual table, which depends on
the result-set of the predefined SQL statement.

Like the SQL tables, Views also store data in rows and columns, but the rows do not have any
physical existence in the database.

Any database administrator and user can easily create the View by selecting the columns from
one or more database tables. They can also delete and update the views according to their needs.

A view can store either all the records of the table or a particular record from the table using the
WHERE clause.

Create a SQL View


You can easily create a View in Structured Query Language by using the CREATE VIEW
statement. You can create the View from a single table or multiple tables.

Syntax to Create View from Single Table

1. CREATE VIEW View_Name AS

2. SELECT Column_Name1, Column_Name2, ....., Column_NameN

3. FROM Table_Name

4. WHERE condition;

In the syntax, View_Name is the name of View you want to create in SQL. The SELECT
command specifies the rows and columns of the table, and the WHERE clause is optional, which
is used to select the particular record from the table.

Syntax to Create View from Multiple Tables

You can create a View from multiple tables by including the tables in the SELECT statement.

1. REATE VIEW View_Name AS

2. SELECT Table_Name1.Column_Name1, Table_Name1.Column_Name2, Table_Name2


.Column_Name2, ....., Table_NameN.Column_NameN

3. FROM Table_Name1, Table_Name2, ....., Table_NameN

4. WHERE condition;

Example to Create a View from Single table


Let's consider the Student_Details table, which consists of Stu_ID, Stu_Name, Stu_Subject, and
Stu_Marks columns. The data of the Student_Details is shown in the following table:

Student_ID Stu_Name Stu_Subject Stu_Marks

1001 Akhil Math 85

1002 Balram Science 78

1003 Bheem Math 87

1004 Chetan English 95

1005 Diksha Hindi 99

1006 Raman Computer 90

1007 Sheetal Science 68

Table: Student_Details

Suppose, you want to create a view with Stu_ID, Stu_Subject, and Stu_Marks of those students
whose marks are greater than 85. For this issue, you have to type the following query:

1. CREATE VIEW Student_View AS

2. SELECT Student_ID, Stu_Subject, Stu_Marks

3. FROM Student_Details

4. WHERE Stu_Marks > 85;


1. Select * FROM Student_View;

Output:

Student_ID Stu_Subject Stu_Marks

1001 Math 85

1003 Math 87

1004 English 95

1005 Hindi 99

1006 Computer 90

View: Student_View

Example to Create a View from Multiple tables

Let's consider two tables, Student_Details and Teacher_Details. The Student_Details table
consists of Stu_ID, Stu_Name, Stu_Subject, and Stu_Marks columns. And, the Teacher_Details
table consists of Teacher_ID, Teacher_Name, Teacher_Subject, Teacher_City columns. The data
of the Student_Details and Teacher_Details is shown in the following two tables:

Student_ID Stu_Name Stu_Subject Stu_Marks

1001 Akhil Math 85

1002 Balram Science 78

1003 Bheem Math 87


1004 Chetan English 95

1005 Diksha Hindi 99

1006 Raman Computer 90

1007 Sheetal Science 68

Table: Student_Details

Teacher_ID Teacher_Name Teacher_Subject Teacher_C

2001 Arun Math Gurgaon

2002 Manoj Science Delhi

2003 Reena SST Noida

2004 Parul English Gurgaon

2005 Nishi Hindi Noida

2006 Anuj Computer Delhi

2007 Ram Physical Education Delhi

Table: Teacher_Details

Suppose, you want to create a view with Stu_ID, Stu_Name, Teacher_ID, and Teacher_Subject
columns from the Student_Details and Teacher_Details tables.
1. CREATE VIEW Student_Teacher_View AS

2. SELECT Student_Details.Student_ID, Student_Details.Stu_Name, Teacher_Details.Tea


cher_ID, Teacher_Details.Teacher_Subject

3. FROM Student_Details, Teacher_Details

4. WHERE Student_Details.Stu_Subject = Teacher_Details.Teacher_Subject;

To display the data of Student_Teacher_View, you have to type the following SELECT query:

1. Select * FROM Student_Teacher_View;

Output:

Student_ID Stu_Name Teacher_ID Teacher_Subject

1001 Akhil 2001 Math

1002 Balram 2002 Science

1004 Chetan 2004 English

1005 Diksha 2005 Hindi

1006 Raman 2006 Computer

View: Student_Teacher_View

Update an SQL View

We can also modify existing data and insert the new record into the view in the Structured Query
Language. A view in SQL can only be modified if the view follows the following conditions:

1. You can update that view which depends on only one table. SQL will not allow updating
the view which is created more than one table.
2. The fields of view should not contain NULL values.
3. The view does not contain any subquery and DISTINCT keyword in its definition.
4. The views cannot be updatable if the SELECT statement used to create a View contains
JOIN or HAVING or GROUP BY clause.
5. If any field of view contains any SQL aggregate function, you cannot modify the view.
Syntax to Update a View

CREATE OR REPLACE VIEW View_Name AS

SELECT Column_Name1, Column_Name2, ....., Column_NameN

FROM Table_Name

WHERE condition;

Example to Update a View

If we want to update the above Student_View and add the Stu_Name attribute from the Student
table in the view, you have to type the following Replace query in SQL:

CREATE OR REPLACE VIEW Student_View AS

SELECT Student_ID, Stu_Name, Stu_Subject, Stu_Marks

FROM Student_Details

WHERE Stu_Subject = 'Math';

The above statement updates the existing Student_View and updates the data based on the
SELECT statement.

Now, you can see the virtual table by typing the following query:

SELECT * FROM Student_View;

Output:

Student_ID Stu_Name Stu_Subject Stu_Marks

1001 Akhil Math 85

1003 Bheem Math 87

View: Student_View
Insert the new row into the existing view

Just like the insertion process of database tables, we can also insert the record in the views. The
following SQL INSERT statement is used to insert the new row or record in the view:

INSERT INTO View_Name(Column_Name1, Column_Name2 , Column_Name3, ....., Column


_NameN) VALUES(value1, value2, value3, ...., valueN);

Example to Insert new record in the view

Suppose, you want to insert the record of a new student in the Student_View, then you have to
write the following query in SQL:

INSERT INTO Student_View (Student_ID, Stu_Subject, Stu_Marks) VALUES (1007, Hin


di, 89);

Now, you can check that the new record is inserted in the Student_View or not by using the
following query:

SELECT * FROM Student_View;

Output:

Student_ID Stu_Subject Stu_Marks

1001 Math 85

1003 Math 87

1004 English 95

1005 Hindi 99

1006 Computer 90

1007 Hindi 89

View: Student_View
Delete the existing row from the view

Just like the deletion process of database tables, we can also delete the record from the views.
The following SQL DELETE statement is used to delete the existing row or record from the
view:

DELETE FROM View_Name WHERE Condition;

Example to Delete the record from the View

Suppose, you want to delete the record of those students from the Student_View whose Subject
is Math, then you have to type the following SQL query:

DELETE FROM Student_View WHERE Stu_Subject = 'Math' ;

Now, you can check that the record is removed from the Student_View or not by using the
following query:

SELECT * FROM Student_View;

Output:

Student_ID Stu_Subject Stu_Marks

1004 English 95

1005 Hindi 99

1006 Computer 90

1007 Hindi 89

View: Student_View

Drop a View

We can also delete the existing view from the database if it is no longer needed. The following
SQL DROP statement is used to delete the view:

DROP VIEW View_Name;

Example to Drop a View


Suppose, you want to delete the above Student_View, then you have to type the following query
in the Structured Query Language:

DROP VIEW Student_View;

SQL Sub Query

A Subquery is a query within another SQL query and embedded within the WHERE clause.

Important Rule:

o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.

o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

o A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.

o Subqueries are on the right side of the comparison operator.

o A subquery is enclosed in parentheses.

o In the Subquery, ORDER BY command cannot be used. But GROUP BY command can
be used to perform the same function as ORDER BY command.

1. Subqueries with the Select Statement

SQL subqueries are most frequently used with the Select statement.

Syntax

SELECT column_name

FROM table_name

WHERE column_name expression operator

( SELECT column_name from table_name WHERE ... );

Example

Consider the EMPLOYEE table have the following records:


ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

6 Harry 42 China 4500.00

7 Jackson 25 Mizoram 10000.00

The subquery with a SELECT statement will be:

SELECT *

FROM EMPLOYEE

WHERE ID IN (SELECT ID

FROM EMPLOYEE

WHERE SALARY > 4500);

This would produce the following result:

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00


2. Subqueries with the INSERT Statement

o SQL subquery can also be used with the Insert statement. In the insert statement, data
returned from the subquery is used to insert into another table.

o In the subquery, the selected data can be modified with any of the character, date
functions.

Syntax:

INSERT INTO table_name (column1, column2, column3....)

SELECT *

FROM table_name

WHERE VALUE OPERATOR

Example

Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.

Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.

INSERT INTO EMPLOYEE_BKP

SELECT * FROM EMPLOYEE

WHERE ID IN (SELECT ID

FROM EMPLOYEE);

3. Subqueries with the UPDATE Statement

The subquery of SQL can be used in conjunction with the Update statement. When a subquery is
used with the Update statement, then either single or multiple columns in a table can be updated.

Syntax

UPDATE table

SET column_name = new_value


WHERE VALUE OPERATOR

(SELECT COLUMN_NAME

FROM TABLE_NAME

WHERE condition);

Example

Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE


table. The given example updates the SALARY by .25 times in the EMPLOYEE table for all
employee whose AGE is greater than or equal to 29.

UPDATE EMPLOYEE

SET SALARY = SALARY * 0.25

WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP

WHERE AGE >= 29);

This would impact three rows, and finally, the EMPLOYEE table would have the following
records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 1625.00

5 Kathrin 34 Bangalore 2125.00

6 Harry 42 China 1125.00

7 Jackson 25 Mizoram 10000.00


4. Subqueries with the DELETE Statement

The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.

Syntax

DELETE FROM TABLE_NAME

WHERE VALUE OPERATOR

(SELECT COLUMN_NAME

FROM TABLE_NAME

WHERE condition);

Example

Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE


table. The given example deletes the records from the EMPLOYEE table for all EMPLOYEE
whose AGE is greater than or equal to 29.

DELETE FROM EMPLOYEE

WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP

WHERE AGE >= 29 );

This would impact three rows, and finally, the EMPLOYEE table would have the following
records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

7 Jackson 25 Mizoram 10000.00


Conclusion: Thus we have studies SQL join, views and sub-query.

Note : use all the above commands in mysql take screen


shots of all the above commands and attach printouts.

You might also like