Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL inserting records using nested subqueries with any operator

In this page, we are going to discuss, how two or more subqueries can be implemented in an INSERT INTO statement to insert rows into a table.

Example:

Sample table: agent1


Sample table: agents


Sample table: customer


Sample table: orders


To insert records into 'agent1' table from 'agents' table with following conditions -

1. 'agent_code' of agents table must be any 'agent_code' from 'customer' table which satisfies the condition bellow :

2. 'agent_code' of customer table must be any 'agent_code' from 'orders' table which satisfies the condition bellow :

3. 'advance_amount' of 'orders' table must be more than 600,

the following SQL statement can be used:

SQL Code:

INSERT INTO agent1
SELECT * FROM  agents
WHERE agent_code=ANY(
SELECT agent_code FROM customer
WHERE agent_code =ANY(
SELECT agent_code FROM orders
WHERE  advance_amount>600));

SQL insert using nested subqueries with any operator and group by

In the following we are going to discuss, how an ANY operator with GROUP BY can be used in an INSERT INTO statement to insert records into a table.

Example:

Sample table: agent1


Sample table: agents


Sample table: customer


Sample table: orders


To insert records into 'agent1' table from 'agents' table with following conditions -

1. 'agent_code' of agents table must be any 'agent_code' from 'customer' table which satisfies the condition bellow :

2. 'agent_code' of customer table must be any 'agent_code' from 'orders' table which satisfies the condition bellow :

3. 'ord_amount' of 'orders' table must be more than 1000,

4. 'ord_amount' of 'orders' table should arrange in a group,

the following SQL statement can be used:

SQL Code:

INSERT INTO agent1
SELECT * FROM  agents
WHERE agent_code=ANY(
SELECT agent_code FROM customer
WHERE agent_code =ANY(
SELECT agent_code FROM orders
WHERE  ord_amount>1000
GROUP BY ord_amount));

SQL insert using nested subqueries with any operator and group by and order by

In the following we are going to discuss, how an ANY operator with ORDER BY and GROUP BY can be used in an INSERT INTO statement to insert records into a table.

Example:

Sample table: agent1


Sample table: agents


Sample table: customer


Sample table: orders


To insert records into 'agent1' table from 'agents' table with following conditions -

1. 'agent_code' of agents table must be any 'agent_code' from 'customer' table which satisfies the condition bellow :

2. 'agent_code' of customer table must be any 'agent_code' from 'orders' table which satisfies the condition bellow :

3. 'advance_amount' of 'orders' table must be more than 600,

4. same 'advance_amount' of 'orders' table should come in a group,

5. 'advance_amount' of 'orders' table should be arranged in ascending order,

the following SQL statement can be used:

SQL Code:

INSERT INTO agent1
SELECT * FROM  agents
WHERE agent_code=ANY(
SELECT agent_code FROM customer
WHERE agent_code =ANY(
SELECT agent_code FROM orders
WHERE  advance_amount>600
GROUP BY advance_amount
ORDER BY advance_amount));

SQL insert using subqueries with max() function

In the following we are going to discuss, how an MAX() function in a subquery can be used in an INSERT INTO statement to insert records into a table.

Example:

Sample table: highorder


Sample table: orders


To add values against 'ord_amount','ord_date','cust_code' columns in 'highorder' table from 'orders' table with following conditions -

1. 'orders' table have defined as alias 'a' and alias 'b',

2. 'ord_amount' of alias 'a' must equal to maximum 'ord_amount' of alias 'b' which satisfies the condition bellow :

3. 'ord_date' of alias 'a' and alias 'b' must be same,

the following SQL statement can be used:

SQL Code:

INSERT INTO  highorder
SELECT ord_amount,ord_date,cust_code
FROM orders a
WHERE ord_amount=
(SELECT MAX(ord_amount)
FROM orders b
WHERE a.ord_date=b.ord_date);

See our Model Database

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: Inserting the result of a query in another table
Next: Update statement



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3