What if your database could guess the most common answers for you?
Why DEFAULT values in SQL? - Purpose & Use Cases
Imagine you are filling out a long form for every new customer in a store. Each time, you have to write down the same country name, currency, or status because most customers share these details.
Manually typing the same information over and over is slow and tiring. It's easy to make mistakes or forget to fill in some fields, causing confusion later.
DEFAULT values let the database fill in common or expected information automatically when you don't provide it. This saves time and avoids errors.
INSERT INTO customers (name, country) VALUES ('Alice', 'USA'); INSERT INTO customers (name, country) VALUES ('Bob', 'USA');
CREATE TABLE customers (name TEXT, country TEXT DEFAULT 'USA'); INSERT INTO customers (name) VALUES ('Alice'); INSERT INTO customers (name) VALUES ('Bob');
It makes adding new data faster and more reliable by automatically filling in common details.
A shop's database automatically sets the default currency to USD for new sales records, so cashiers don't have to enter it every time.
DEFAULT values save time by auto-filling common data.
They reduce errors from missing or inconsistent entries.
They make data entry simpler and more consistent.
Practice
DEFAULT keyword do in a SQL table column definition?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 CQuick Check:
DEFAULT = automatic value assignment [OK]
- Thinking DEFAULT deletes or restricts data
- Confusing DEFAULT with NOT NULL constraint
- Assuming DEFAULT requires manual input
score in a table?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 AQuick Check:
DEFAULT syntax = column type DEFAULT value [OK]
- Adding extra keywords like SET or TO
- Using equals sign (=) after DEFAULT
- Placing DEFAULT before data type
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;
Solution
Step 1: Understand DEFAULT behavior on insert
Sincestatushas 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 BQuick Check:
DEFAULT fills missing column with 'active' [OK]
- Assuming missing columns get NULL instead of DEFAULT
- Thinking query causes syntax error
- Confusing default value with another string
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?
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 AQuick Check:
Wrong data type in INSERT causes error [OK]
- Assuming DEFAULT keyword is invalid in VALUES
- Ignoring data type mismatch errors
- Thinking missing columns cause errors
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?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 DQuick Check:
Omit column with DEFAULT = auto-use [OK]
- No column list + fewer values = mismatch error
- Including stock column even with DEFAULT keyword
- Hardcoding default value instead of omitting column
Ready to prove it?
Pass the test and this pattern moves unknown
