Write a pseudo SQL query to copy data from one table to another. Data is provided for reference.
💡 Model Answer
To copy data from a source table to a destination table in SQL, you typically use an INSERT INTO ... SELECT statement. The basic syntax is:
INSERT INTO destination_table (col1, col2, col3)
SELECT col1, col2, col3
FROM source_table
WHERE <optional_conditions>;
This statement inserts rows into the destination table by selecting the same columns from the source table. If you need to transform data, you can add expressions in the SELECT clause, e.g., SELECT col1, UPPER(col2) AS col2, col3 + 1 AS col3.
Complexity: The operation runs in O(n) time, where n is the number of rows being copied, assuming the source table is scanned once and the destination has appropriate indexes. Ensure that the destination table has the same or compatible column types and that any constraints (primary key, unique, foreign key) are satisfied to avoid errors. If you need to copy only a subset of rows, add a WHERE clause to filter the source data. If you want to replace existing data, you might first DELETE or TRUNCATE the destination table before inserting.
This approach works in most relational databases such as MySQL, PostgreSQL, SQL Server, and Oracle.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500