Create table from query with constraints afterward

Create table from query with constraints afterward

Hard SQL DDL 12 views
Explanation Complexity

Problem Statement

Create a new table TopCustomers from Orders that stores customer_id and spend, then add a primary key on customer_id.

Input Format

SQL DDL

Output Format

DDL statement(s)

Example

Orders(customer_id, total_amount)
Table created

Constraints

Create-as-select then alter

Input / Output Format

Input Format
SQL DDL
Output Format
DDL statement(s)
Constraints
Create-as-select then alter

Examples

Input:
Orders(customer_id, total_amount)
Output:
Table created

Example Solution (Public)

SQL
CREATE TABLE TopCustomers AS SELECT customer_id, SUM(total_amount) AS spend FROM Orders GROUP BY customer_id; ALTER TABLE TopCustomers ADD CONSTRAINT pk_topcustomers PRIMARY KEY (customer_id);

Official Solution Code

CREATE TABLE TopCustomers AS SELECT customer_id, SUM(total_amount) AS spend FROM Orders GROUP BY customer_id; ALTER TABLE TopCustomers ADD CONSTRAINT pk_topcustomers PRIMARY KEY (customer_id);
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.