# SQL CREATE TABLE Challenge: Choose the Correct SQL Statement with Solution

When designing a database, creating a table is one of the first and most important steps. A well-designed table helps maintain accurate data, enforce business rules, and avoid unnecessary data errors.

![SQL CREATE TABLE challenge showing an e-commerce orders table, SQL statement, correct solution, and sample output with PRIMARY KEY, NOT NULL, DECIMAL, CHECK, and DEFAULT constraints. Practical SQL interview question for beginners and database developers.](https://cdn.hashnode.com/uploads/covers/67cafe24d4e6389fe6b14920/24a3c6f9-f969-4fac-992c-1f88065bb3b7.png align="center")

Let's understand this with a practical SQL challenge.

Imagine you are developing a database for an e-commerce application. You need to create an "orders" table to store customer orders, order dates, payment amounts, and delivery status.

Your challenge: Which SQL CREATE TABLE statement correctly defines the table with appropriate data types and constraints?

Before looking at the solution, consider these important SQL concepts:

- PRIMARY KEY: Uniquely identifies every order in the table.
- NOT NULL: Ensures that required columns cannot contain NULL values.
- DECIMAL: Stores monetary values with fixed decimal precision.
- CHECK: Prevents invalid values, such as negative order amounts.
- DEFAULT: Automatically assigns an initial order status when no value is provided.

Complete SQL Solution

A correct SQL CREATE TABLE statement for this requirement is:

CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT NOT NULL, order_date DATE NOT NULL, order_amount DECIMAL(10,2) NOT NULL CHECK (order_amount >= 0), order_status VARCHAR(20) DEFAULT 'Pending' );

Understanding the SQL Statement

Let's understand each column and constraint.

Column| Data Type| Purpose order_id| INT| Unique order identifier customer_id| INT| Customer identification order_date| DATE| Date of the order order_amount| DECIMAL(10,2)| Order amount order_status| VARCHAR(20)| Current order status

1. PRIMARY KEY

order_id INT PRIMARY KEY

The PRIMARY KEY constraint ensures that every order has a unique identifier. It does not allow duplicate or NULL order IDs.

2. NOT NULL

customer_id INT NOT NULL

The NOT NULL constraint ensures that a customer ID must be provided when inserting an order.

3. DECIMAL(10,2)

order_amount DECIMAL(10,2)

DECIMAL is suitable for storing monetary values because it supports fixed-point precision.

Here:

- 10 represents the total number of digits.
- 2 represents the number of digits after the decimal point.

For example, "12500.75" is a valid value.

4. CHECK Constraint

CHECK (order_amount >= 0)

The CHECK constraint ensures that an order amount cannot be negative.

For example:

- 1500.00 – Valid
- 0.00 – Valid
- \-500.00 – Invalid

5. DEFAULT Constraint

order_status VARCHAR(20) DEFAULT 'Pending'

If an order status is not supplied during insertion, SQL automatically assigns "Pending".

Test the Table with INSERT Statements

Let's insert some sample records.

INSERT INTO orders VALUES (101, 501, DATE '2026-11-26', 2500.50, 'Confirmed');

INSERT INTO orders VALUES (102, 502, DATE '2026-11-26', 1500.00, DEFAULT);

INSERT INTO orders VALUES (103, 503, DATE '2026-11-26', 3200.75, 'Delivered');

Notice that the second INSERT statement uses "DEFAULT". The order status will automatically become "Pending".

Check the Output

Execute the following query:

SELECT \* FROM orders;

Expected output:

| order_id | customer_id | order_date | order_amount | order_status |
| --- | --- | --- | --- | --- |
| 101 | 501 | 2026-11-26 | 2500.50 | Confirmed |
| 102 | 502 | 2026-11-26 | 1500.00 | Pending |
| 103 | 503 | 2026-11-26 | 3200.75 | Delivered |

Why Is This SQL Statement Correct?

The CREATE TABLE statement correctly defines the table structure and applies the required constraints.

- PRIMARY KEY prevents duplicate order identifiers.
- NOT NULL ensures required information is provided.
- DECIMAL(10,2) supports accurate monetary values.
- CHECK prevents negative order amounts.
- DEFAULT automatically assigns the Pending status.

Important: A CREATE TABLE statement defines the table structure, while INSERT INTO adds records to the table.

Practice More SQL DDL Commands

Understanding CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE TABLE, and SQL constraints is essential for database development and SQL interviews.

Read the complete tutorial with practical examples:

"SQL DDL Commands – CREATE, ALTER, DROP, TRUNCATE and Table Relationships" (<https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html>)

Final Challenge

Can you modify this table to ensure that "order_status" accepts only these values?

- Pending
- Confirmed
- Shipped
- Delivered
- Cancelled

Try writing the appropriate CHECK constraint yourself.

Share your SQL solution in the comments!

Follow Sankalan Data Tech for practical SQL challenges, SQL interview questions, database tutorials, Python programming, and data analytics concepts.

#SQL #LearnSQL #SQLChallenge #SQLInterviewQuestions #CreateTable #SQLConstraints #DatabaseDesign #SQLTutorial #DataAnalytics #DatabaseManagement
