W3. SQL Select Into Flashcards
Q: What does the SQL SELECT INTO statement do?
A: It copies data from one table into a new table.
Q: Write the syntax for copying all columns into a new table using SELECT INTO.
SELECT *
INTO newtable
FROM oldtable
WHERE condition;
Q: How do you copy only specific columns into a new table using SELECT INTO?
SELECT column1, column2, …
INTO newtable
FROM oldtable
WHERE condition;
Q: Can you use AS to rename columns when copying with SELECT INTO?
A: Yes, AS can be used to create new column names in the copied table.
Q: Write a query to create a backup copy of the Customers table.
SELECT * INTO CustomersBackup2017
FROM Customers;
Q: How do you use IN with SELECT INTO to copy data to another database?
A: Use IN ‘externaldb’, e.g.,
SELECT * INTO CustomersBackup2017 IN ‘Backup.mdb’
FROM Customers;
Q: Write a query to copy only CustomerName and ContactName into a new table.
SELECT CustomerName, ContactName
INTO CustomersBackup2017
FROM Customers;
Q: Write a query to copy only German customers into a new table.
SELECT * INTO CustomersGermany
FROM Customers
WHERE Country = ‘Germany’;
Q: Can SELECT INTO be used with JOIN to copy data from multiple tables?
A: Yes, JOIN can be used to select data from multiple tables into a new table.
Q: Write a query to copy data from Customers and Orders into a new table.
SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2017
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Q: How can you create a new, empty table with the schema of another table using SELECT INTO?
A: Use a WHERE clause that returns no data, e.g., WHERE 1 = 0.
Q: Write a query to create a new, empty table with the schema of an old table.
SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;