0
0
PostgreSQLquery~20 mins

Serial and identity columns in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serial and Identity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of inserting rows with SERIAL column
Consider the table users created as:
CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);
After inserting three rows:
INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie');
SELECT * FROM users ORDER BY id;
What is the output?
PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie');
SELECT * FROM users ORDER BY id;
A
1 | Alice
2 | Bob
3 | Charlie
B
0 | Alice
1 | Bob
2 | Charlie
C
NULL | Alice
NULL | Bob
NULL | Charlie
D
1 | Alice
1 | Bob
1 | Charlie
Attempts:
2 left
💡 Hint
Remember that SERIAL creates an auto-incrementing integer starting at 1.
📝 Syntax
intermediate
2:00remaining
Correct syntax to create an IDENTITY column
Which of the following SQL statements correctly creates a table products with an id column as an IDENTITY column starting at 1000?
ACREATE TABLE products (id SERIAL START 1000, name TEXT);
BCREATE TABLE products (id INT GENERATED ALWAYS AS IDENTITY (START 1000), name TEXT);
CCREATE TABLE products (id INT IDENTITY START 1000, name TEXT);
DCREATE TABLE products (id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 1000), name TEXT);
Attempts:
2 left
💡 Hint
Check the exact syntax for IDENTITY columns in PostgreSQL.
🧠 Conceptual
advanced
2:00remaining
Difference between SERIAL and IDENTITY columns
Which statement best describes the difference between SERIAL and IDENTITY columns in PostgreSQL?
ASERIAL is a pseudo-type that creates a sequence and sets a default; IDENTITY is a SQL standard feature that defines auto-increment behavior within the column definition.
BSERIAL columns cannot be primary keys; IDENTITY columns can only be used as primary keys.
CSERIAL columns are deprecated and no longer supported; IDENTITY columns are the only way to auto-increment.
DSERIAL columns automatically update on row update; IDENTITY columns only increment on insert.
Attempts:
2 left
💡 Hint
Think about how SERIAL and IDENTITY are implemented internally.
🔧 Debug
advanced
2:00remaining
Why does this INSERT fail with IDENTITY column?
Given the table:
CREATE TABLE orders (order_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, product TEXT);

Which INSERT statement will cause an error?
AINSERT INTO orders (product) VALUES ('Book');
BINSERT INTO orders DEFAULT VALUES;
CINSERT INTO orders (order_id, product) VALUES (5, 'Pen');
DINSERT INTO orders (product) VALUES ('Notebook');
Attempts:
2 left
💡 Hint
Check the behavior of GENERATED ALWAYS AS IDENTITY when inserting explicit values.
optimization
expert
3:00remaining
Best practice for large tables needing auto-increment keys
You have a very large table that requires a unique auto-incrementing primary key. Which approach is best for performance and future-proofing in PostgreSQL?
AUse SERIAL with default INT type and no caching.
BUse BIGINT GENERATED BY DEFAULT AS IDENTITY with CACHE to reduce sequence calls.
CManually create a sequence and increment it in application code before insert.
DUse UUID columns generated by the application instead of numeric auto-increment.
Attempts:
2 left
💡 Hint
Consider sequence caching and data type size for large tables.