Interview Questions

Get ready for your next interview with our comprehensive question library

PostgreSQL Interview Questions

Filter by Difficulty

1.

Explain the ACID properties in PostgreSQL.

beginner

ACID properties ensure database reliability:

  • Atomicity: Transactions are all-or-nothing. If any part fails, the entire transaction rolls back
  • Consistency: Database remains in a valid state before and after transactions
  • Isolation: Concurrent transactions don't interfere with each other
  • Durability: Committed changes persist even after system failures

Example:

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- Both operations succeed or both fail
2.

What are the main PostgreSQL data types?

beginner

PostgreSQL supports various data types:

  • Numeric: INTEGER, BIGINT, DECIMAL, NUMERIC, REAL, DOUBLE PRECISION
  • Character: CHAR, VARCHAR, TEXT
  • Date/Time: DATE, TIME, TIMESTAMP, INTERVAL
  • Boolean: BOOLEAN
  • Binary: BYTEA
  • JSON: JSON, JSONB
  • Arrays: Any data type can be an array
  • UUID: Universally Unique Identifiers
  • Network: INET, CIDR for IP addresses
3.

What is the difference between PRIMARY KEY and UNIQUE constraints?

beginner

Both ensure uniqueness, but with key differences:

PRIMARY KEY:

  • Cannot contain NULL values
  • Only one per table
  • Automatically creates a unique index
  • Used for table relationships

UNIQUE:

  • Can contain NULL values (multiple NULLs allowed)
  • Multiple unique constraints per table
  • Creates a unique index
CREATE TABLE users (
    id SERIAL PRIMARY KEY,        -- Cannot be NULL
    email VARCHAR(255) UNIQUE,    -- Can have one NULL
    username VARCHAR(50) UNIQUE   -- Multiple unique constraints allowed
);
4.

Explain the difference between DELETE, TRUNCATE, and DROP.

beginner
  • DELETE: Removes specific rows based on conditions, can be rolled back, triggers fire
  • TRUNCATE: Removes all rows quickly, can be rolled back, triggers don't fire
  • DROP: Removes the entire table structure and data, cannot be rolled back
DELETE FROM users WHERE age < 18;  -- Removes specific rows
TRUNCATE TABLE users;              -- Removes all rows
DROP TABLE users;                  -- Removes table entirely
5.

What is a foreign key and how does it work in PostgreSQL?

beginner

A foreign key establishes and enforces a link between data in two tables. It prevents actions that would destroy links between tables.

CREATE TABLE departments (
    dept_id SERIAL PRIMARY KEY,
    dept_name VARCHAR(100)
);

CREATE TABLE employees (
    emp_id SERIAL PRIMARY KEY,
    emp_name VARCHAR(100),
    dept_id INTEGER REFERENCES departments(dept_id)
);

Foreign key constraints ensure referential integrity by preventing:

  • Insertion of records with non-existent foreign key values
  • Deletion of referenced records (unless CASCADE is specified)
6.

What are indexes and why are they important?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
7.

Explain different types of JOINs in PostgreSQL with examples.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
8.

What are Common Table Expressions (CTEs) and when would you use them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
9.

Explain window functions and provide practical examples.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
10.

What is the difference between a view and a materialized view?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

Explain PostgreSQL transaction isolation levels.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What are stored procedures and functions in PostgreSQL?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

Explain triggers and provide a practical example.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

What is MVCC and how does it work in PostgreSQL?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

Explain the difference between UNION and UNION ALL.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What is PostgreSQL connection pooling and how does PgBouncer work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

Explain PostgreSQL's query execution plan and how to optimize queries.

expert

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is table partitioning and when should you use it?

expert

Upgrade to Premium to see the answer

Upgrade to Premium
19.

Explain different types of indexes in PostgreSQL and their use cases.

expert

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What is connection pooling and why is it important?

expert

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 36 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime