📖 Scenario: You work in a company database where customer orders are stored in one table, and you want to create a summary table that stores only orders with a total amount greater than 100. This helps the sales team focus on big orders.
🎯 Goal: Create a new table called big_orders and insert data into it by selecting orders from the orders table where the total amount is greater than 100.
📋 What You'll Learn
Create a table called big_orders with columns order_id, customer_name, and total_amount.
Insert data into big_orders by selecting from orders where total_amount is greater than 100.
Use the INSERT INTO ... SELECT ... syntax.
💡 Why This Matters
🌍 Real World
In real companies, copying filtered data from one table to another helps create reports or summaries for specific needs, like focusing on big orders.
💼 Career
Database administrators and developers often use INSERT with SELECT to move or archive data efficiently without manual re-entry.
Progress0 / 4 steps
1
Create the orders table with sample data
Create a table called orders with columns order_id (integer), customer_name (varchar 50), and total_amount (decimal 10,2). Then insert these exact rows: (1, 'Alice', 120.50), (2, 'Bob', 75.00), (3, 'Charlie', 200.00).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO with multiple rows to add data.
2
Create the big_orders table
Create a table called big_orders with the same columns as orders: order_id (integer), customer_name (varchar 50), and total_amount (decimal 10,2).
MySQL
Hint
Use CREATE TABLE with the same column definitions as orders.
3
Insert data into big_orders using SELECT
Write an INSERT INTO big_orders statement that selects order_id, customer_name, and total_amount from orders where total_amount is greater than 100.
MySQL
Hint
Use INSERT INTO ... SELECT ... FROM ... WHERE ... to copy filtered rows.
4
Verify the data in big_orders
Write a SELECT statement to get all rows from big_orders to check that only orders with total_amount greater than 100 were inserted.
MySQL
Hint
Use SELECT * FROM big_orders; to see all rows in the table.
Practice
(1/5)
1. What does the INSERT INTO ... SELECT statement do in MySQL?
easy
A. Copies rows from one table to another matching column structure
B. Deletes rows from a table based on a condition
C. Updates existing rows in a table
D. Creates a new table with selected columns
Solution
Step 1: Understand the purpose of INSERT INTO ... SELECT
This statement copies data from one table to another by selecting rows from the source table.
Step 2: Recognize the matching column requirement
The columns selected must match the target table's columns in number and type to insert correctly.
Final Answer:
Copies rows from one table to another matching column structure -> Option A
Quick Check:
INSERT with SELECT copies rows [OK]
Hint: Think of copying rows from one table to another [OK]
Common Mistakes:
Confusing INSERT with SELECT as a delete or update operation
Thinking it creates a new table
Assuming it modifies existing rows instead of inserting
2. Which of the following is the correct syntax to insert rows from table1 into table2 using INSERT INTO ... SELECT?
easy
A. INSERT INTO table1 SELECT * FROM table2;
B. INSERT table2 INTO SELECT * FROM table1;
C. INSERT INTO table2 SELECT * FROM table1;
D. SELECT * INTO table2 FROM table1;
Solution
Step 1: Identify correct INSERT INTO ... SELECT syntax
The correct syntax is INSERT INTO target_table SELECT ... FROM source_table;.
Step 2: Match the option to syntax
INSERT INTO table2 SELECT * FROM table1; matches this syntax exactly, inserting rows from table1 into table2.
Final Answer:
INSERT INTO table2 SELECT * FROM table1; -> Option C
Quick Check:
Correct INSERT INTO ... SELECT syntax = INSERT INTO table2 SELECT * FROM table1; [OK]
Hint: Remember: INSERT INTO target SELECT ... FROM source [OK]
Common Mistakes:
Swapping table names in INSERT and SELECT
Using SELECT INTO which is not standard in MySQL
Incorrect keyword order like INSERT table INTO
3. Given two tables: employees(id, name, department) managers(id, name, department) What will be the result of this query?
INSERT INTO managers (id, name, department) SELECT id, name, department FROM employees WHERE department = 'Sales';
medium
A. Only employees from Sales department are copied to managers
B. All employees are copied to managers
C. No rows are copied because of syntax error
D. Only managers from Sales department are updated
Solution
Step 1: Analyze the SELECT part with WHERE clause
The SELECT picks only employees where department equals 'Sales'.
Step 2: Understand INSERT INTO effect
These selected rows are inserted into the managers table, adding only Sales department employees.
Final Answer:
Only employees from Sales department are copied to managers -> Option A
Quick Check:
WHERE filters rows inserted [OK]
Hint: WHERE filters rows to insert [OK]
Common Mistakes:
Assuming all employees are copied ignoring WHERE
Thinking it updates existing rows instead of inserting
Confusing syntax causing no rows inserted
4. What is wrong with this query?
INSERT INTO products (id, name) SELECT id, name, price FROM items;
medium
A. Missing WHERE clause causes syntax error
B. Number of columns in INSERT and SELECT do not match