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);
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.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: Inserting the result of a query in another table
Next: Update statement
SQL: Tips of the Day
What is the most efficient/elegant way to parse a flat table into a tree?
WITH RECURSIVE MyTree AS ( SELECT * FROM MyTable WHERE ParentId IS NULL UNION ALL SELECT m.* FROM MyTABLE AS m JOIN MyTree AS t ON m.ParentId = t.Id ) SELECT * FROM MyTree;
Ref: https://bit.ly/3FPYFFF
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook