What is the purpose of the DEFAULT value in a SQL table column?
The DEFAULT value sets a preset value for a column when no value is provided during data insertion. It helps avoid NULLs and ensures consistent data.
Click to reveal answer
beginner
How do you define a DEFAULT value for a column in SQL?
You use the DEFAULT keyword followed by the value when creating or altering a table column, for example: age INT DEFAULT 18.
Click to reveal answer
intermediate
Can a DEFAULT value be a function in SQL? Give an example.
Yes, a DEFAULT value can be a function. For example, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP sets the current time automatically.
Click to reveal answer
beginner
What happens if you insert a row without specifying a column that has a DEFAULT value?
The column automatically gets the DEFAULT value specified in the table definition instead of NULL or an error.
Click to reveal answer
intermediate
Is it possible to change the DEFAULT value of an existing column? How?
Yes, you can change it using the ALTER TABLE statement with ALTER COLUMN and SET DEFAULT, for example: ALTER TABLE users ALTER COLUMN age SET DEFAULT 21;.
Click to reveal answer
What does the DEFAULT keyword do in SQL?
ASets a preset value for a column when no value is provided
BDeletes a column from a table
CChanges the data type of a column
DCreates a new table
✗ Incorrect
DEFAULT sets a preset value for a column if no value is given during insertion.
Which of the following is a valid DEFAULT value for a TIMESTAMP column?
ADEFAULT 0
BDEFAULT TRUE
CDEFAULT CURRENT_TIMESTAMP
DDEFAULT 'hello'
✗ Incorrect
CURRENT_TIMESTAMP is a function that returns the current date and time, valid as a DEFAULT for TIMESTAMP.
If a column has DEFAULT 100 and you insert a row without specifying that column, what value will it have?
A100
B0
CNULL
DError
✗ Incorrect
The column will automatically get the DEFAULT value 100.
How do you change the DEFAULT value of an existing column in SQL?
AYou cannot change DEFAULT values
BDROP COLUMN and add a new one
CUPDATE the table
DALTER TABLE ... ALTER COLUMN ... SET DEFAULT ...
✗ Incorrect
You use ALTER TABLE with ALTER COLUMN and SET DEFAULT to change the default value.
Can DEFAULT values be expressions or functions?
ANo, only constants
BYes, for example CURRENT_TIMESTAMP
COnly strings
DOnly numbers
✗ Incorrect
DEFAULT values can be functions like CURRENT_TIMESTAMP to set dynamic defaults.
Explain what DEFAULT values are in SQL and why they are useful.
Think about what happens if you forget to give a value for a column.
You got /4 concepts.
Describe how to set and change DEFAULT values for columns in SQL tables.
Consider both table creation and modifying existing tables.
You got /3 concepts.
Practice
(1/5)
1. What does the DEFAULT keyword do in a SQL table column definition?
easy
A. Deletes the column if no value is provided
B. Prevents any value from being inserted into the column
C. Sets a value automatically if no value is provided during insert
D. Requires the user to always provide a value
Solution
Step 1: Understand the purpose of DEFAULT
The DEFAULT keyword assigns a preset value to a column when no value is given during data insertion.
Step 2: Compare options with this purpose
Only Sets a value automatically if no value is provided during insert correctly describes this automatic assignment behavior.
Final Answer:
Sets a value automatically if no value is provided during insert -> Option C
2. Which of the following is the correct syntax to set a default value of 100 for a column named score in a table?
easy
A. score INT DEFAULT 100
B. score INT SET DEFAULT 100
C. score INT DEFAULT = 100
D. score INT DEFAULT TO 100
Solution
Step 1: Recall standard SQL syntax for DEFAULT
The correct syntax is: column_name data_type DEFAULT value, without extra keywords or symbols.
Step 2: Check each option
score INT DEFAULT 100 matches the correct syntax exactly. Options B, C, and D add incorrect keywords or symbols.
Final Answer:
score INT DEFAULT 100 -> Option A
Quick Check:
DEFAULT syntax = column type DEFAULT value [OK]
Hint: DEFAULT syntax: column type DEFAULT value [OK]
Common Mistakes:
Adding extra keywords like SET or TO
Using equals sign (=) after DEFAULT
Placing DEFAULT before data type
3. Given the table users with column status VARCHAR(10) DEFAULT 'active', what will be the result of this query?
INSERT INTO users (id, name) VALUES (1, 'Alice'); SELECT id, name, status FROM users WHERE id = 1;
medium
A. [1, 'Alice', NULL]
B. [1, 'Alice', 'active']
C. [1, 'Alice', 'inactive']
D. Syntax error
Solution
Step 1: Understand DEFAULT behavior on insert
Since status has a default of 'active' and no value was provided, SQL inserts 'active' automatically.
Step 2: Check the SELECT output
The SELECT query will show id=1, name='Alice', and status='active' as inserted or defaulted.
Final Answer:
[1, 'Alice', 'active'] -> Option B
Quick Check:
DEFAULT fills missing column with 'active' [OK]
Hint: Missing column uses DEFAULT value automatically [OK]
Common Mistakes:
Assuming missing columns get NULL instead of DEFAULT
Thinking query causes syntax error
Confusing default value with another string
4. Consider this table creation statement:
CREATE TABLE orders ( id INT PRIMARY KEY, quantity INT DEFAULT 1, price DECIMAL(5,2) DEFAULT 0.00 );
Which of the following INSERT statements will cause an error?
medium
A. INSERT INTO orders (id, price) VALUES (12, 'free');
B. INSERT INTO orders (id, quantity) VALUES (11, DEFAULT);
C. INSERT INTO orders (id) VALUES (10);
D. INSERT INTO orders (id, quantity, price) VALUES (13, 5, 9.99);
Solution
Step 1: Analyze each INSERT statement
INSERT INTO orders (id) VALUES (10); inserts id only; quantity and price get defaults (valid). INSERT INTO orders (id, quantity) VALUES (11, DEFAULT); uses DEFAULT keyword explicitly (valid). INSERT INTO orders (id, price) VALUES (12, 'free'); tries to insert 'free' into price (decimal), causing type error. INSERT INTO orders (id, quantity, price) VALUES (13, 5, 9.99); provides all valid values.
Step 2: Identify the error
Only INSERT INTO orders (id, price) VALUES (12, 'free'); has a data type mismatch causing an error.
Final Answer:
INSERT INTO orders (id, price) VALUES (12, 'free'); -> Option A
Quick Check:
Wrong data type in INSERT causes error [OK]
Hint: Check data types when inserting values [OK]
Common Mistakes:
Assuming DEFAULT keyword is invalid in VALUES
Ignoring data type mismatch errors
Thinking missing columns cause errors
5. You have a table products with columns id INT, name VARCHAR(50), and stock INT DEFAULT 0. You want to insert multiple rows but only specify id and name. Which SQL statement correctly inserts two products using DEFAULT for stock?
hard
A. INSERT INTO products (id, name, stock) VALUES (1, 'Pen', 0), (2, 'Pencil', 0);
B. INSERT INTO products VALUES (1, 'Pen'), (2, 'Pencil');
C. INSERT INTO products (id, name, stock) VALUES (1, 'Pen', DEFAULT), (2, 'Pencil', DEFAULT);
D. INSERT INTO products (id, name) VALUES (1, 'Pen'), (2, 'Pencil');
Solution
Step 1: Understand requirement for column specification
To only specify id and name, list only those columns; omitted columns like stock use their DEFAULT value automatically.
Step 2: Evaluate each option
INSERT INTO products (id, name) VALUES (1, 'Pen'), (2, 'Pencil'); specifies only id and name, stock uses DEFAULT (correct). INSERT INTO products VALUES (1, 'Pen'), (2, 'Pencil'); no column list with 2 values (vs 3 columns) causes mismatch error. INSERT INTO products (id, name, stock) VALUES (1, 'Pen', DEFAULT), (2, 'Pencil', DEFAULT); specifies stock column (violates 'only id and name'). INSERT INTO products (id, name, stock) VALUES (1, 'Pen', 0), (2, 'Pencil', 0); specifies stock explicitly with value (not relying on DEFAULT).
Final Answer:
INSERT INTO products (id, name) VALUES (1, 'Pen'), (2, 'Pencil'); -> Option D
Quick Check:
Omit column with DEFAULT = auto-use [OK]
Hint: Omit columns with DEFAULT to use them automatically [OK]
Common Mistakes:
No column list + fewer values = mismatch error
Including stock column even with DEFAULT keyword
Hardcoding default value instead of omitting column
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.