Create table from query with constraints afterward
SQL
Hard
2 views
Problem Description
Create a new table TopCustomers from Orders that stores customer_id and spend, then add a primary key on customer_id.
Output Format
DDL statement(s)
Sample Test Case
Input:
Orders(customer_id, total_amount)
Constraints
Create-as-select then alter
Official Solution
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);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!