Description
The SQL keyword CREATE TABLE is used to create a new table.
- It can be used to simply create an empty table by defining its structure.
- It can be used to copy columns and their data from one or more tables.
CREATE TABLE
The below SQL statement creates a new table, with four columns as mentioned below.
CREATE TABLE users (
id INT,
name VARCHAR(255),
address VARCHAR(255),
city VARCHAR(255)
);
CREATE TABLE Using Another Table
The below SQL statement creates a new table, with data from specific columns of another table.
CREATE TABLE test AS
SELECT customer_name, address, city
FROM customers;
The below SQL statement creates a new table, with data from all columns of another table.
CREATE TABLE test AS
SELECT *
FROM customers;
CREATE TABLE Using Multiple Tables
The below SQL statement creates a new table, with data from multiple tables.
CREATE TABLE test AS
SELECT order_id, order_date, order_value, customer_name
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.customer_id;