PostgreSQL Interview Master
The ultimate guide to 100+ expert PostgreSQL questions. Master the world's most advanced open-source relational database, from basics to internal architecture.
What is PostgreSQL?
BeginnerPostgreSQL is a powerful, open-source object-relational database system (ORDBMS) with over 35 years of active development. It is known for its reliability, feat...
Expert Postgres Solution
What are the key features of PostgreSQL?
BeginnerKey features include: 1. **ACID Compliance**: Ensures reliable transactions. 2. **Conjoined Scalability**: Support for complex queries and large data volumes. 3...
Expert Postgres Solution
What is the difference between PostgreSQL and MySQL?
Beginner- **MySQL**: Traditional RDBMS, focused on speed and simplicity, traditionally better for read-heavy web apps. - **PostgreSQL**: ORDBMS, focused on compliance a...
Expert Postgres Solution
What is a Schema in PostgreSQL?
BeginnerIn PostgreSQL, a schema is a namespace that contains named database objects such as tables, views, indexes, and functions. A database can contain one or more sc...
Expert Postgres Solution
How do you create a table in PostgreSQL?
BeginnerYou use the `CREATE TABLE` command.
Expert Postgres Solution
SQL Query
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
salary NUMERIC(10, 2)
);What is the SERIAL data type?
BeginnerThe `SERIAL` type is a shorthand for creating an integer column that automatically increments. It creates a sequence behind the scenes. `SERIAL` is 4 bytes, whi...
Expert Postgres Solution
How do you insert a row into a table?
BeginnerYou use the `INSERT INTO` command.
Expert Postgres Solution
SQL Query
INSERT INTO employees (name, email, salary) VALUES ('John Doe', 'john@example.com', 50000);What is the purpose of the 'psql' utility?
Beginner`psql` is the interactive terminal (command-line interface) for working with PostgreSQL. It allows you to run queries and manage the database directly.
Expert Postgres Solution
How do you list all tables in 'psql'?
BeginnerYou use the meta-command `\dt` (display tables).
Expert Postgres Solution
SQL Query
\dtHow do you see the structure of a table in 'psql'?
BeginnerYou use the meta-command `\d tableName` (describe).
Expert Postgres Solution
SQL Query
\d employeesWhat is a Primary Key?
BeginnerA primary key is a field or combination of fields that uniquely identifies each row in a table. It cannot be NULL and must be unique.
Expert Postgres Solution
What is a Foreign Key?
BeginnerA foreign key is a column or group of columns used to establish a link between two tables. It ensures referential integrity by pointing to the primary key of an...
Expert Postgres Solution
Explain the 'LIMIT' and 'OFFSET' clauses.
Beginner- **LIMIT**: Restricts the maximum number of rows returned. - **OFFSET**: Skips the first N rows before starting to return rows.
Expert Postgres Solution
SQL Query
SELECT * FROM employees LIMIT 10 OFFSET 20;How do you update data in a table?
BeginnerYou use the `UPDATE` command.
Expert Postgres Solution
SQL Query
UPDATE employees SET salary = salary * 1.1 WHERE id = 1;How do you delete data from a table?
BeginnerYou use the `DELETE` command.
Expert Postgres Solution
SQL Query
DELETE FROM employees WHERE id = 5;What is the 'TRUNCATE' command?
Beginner`TRUNCATE` quickly removes all rows from a table. It is generally faster than `DELETE` because it doesn't log individual row deletions and ignores most triggers...
Expert Postgres Solution
What is a View in PostgreSQL?
BeginnerA view is a virtual table based on the result-set of an SQL query. It does not store data itself but provides a way to simplify complex queries.
Expert Postgres Solution
What is an Index?
BeginnerAn index is a pointer that helps speed up data retrieval. PostgreSQL supports several index types like B-tree (default), Hash, GIN, and GIST.
Expert Postgres Solution
What is the 'WHERE' clause used for?
BeginnerThe `WHERE` clause is used to filter records that fulfill a specified condition.
Expert Postgres Solution
What is the 'GROUP BY' clause?
BeginnerThe `GROUP BY` clause groups rows that have the same values into summary rows, typically used with aggregate functions like `COUNT()`, `MAX()`, `MIN()`, `SUM()`...
Expert Postgres Solution
Explain 'INNER JOIN' vs 'LEFT JOIN'.
Beginner- **INNER JOIN**: Returns records that have matching values in both tables. - **LEFT JOIN**: Returns all records from the left table, and the matched records fr...
Expert Postgres Solution
What is the 'COALESCE' function?
Beginner`COALESCE(val1, val2, ...)` returns the first non-null value from a list of expressions. It is commonly used to provide default values for NULL columns.
Expert Postgres Solution
What is the purpose of 'VACUUM'?
Beginner`VACUUM` reclaims storage occupied by dead tuples (deleted or updated rows) to keep the database efficient. PostgreSQL has an 'Autovacuum' daemon that does this...
Expert Postgres Solution
What is 'COPY' command for?
BeginnerThe `COPY` command is used to move data between PostgreSQL tables and standard file-system files. It is much faster than `INSERT` for large data sets.
Expert Postgres Solution
SQL Query
COPY employees FROM '/tmp/data.csv' WITH (FORMAT csv);What is the default port for PostgreSQL?
BeginnerThe default port for PostgreSQL is **5432**.
Expert Postgres Solution
What is a Window Function in PostgreSQL?
IntermediateA window function performs a calculation across a set of table rows that are somehow related to the current row. Common functions include `ROW_NUMBER()`, `RANK(...
Expert Postgres Solution
SQL Query
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM employees;What is a CTE (Common Table Expression)?
IntermediateA CTE is a temporary result set that you can reference within another SQL statement. It is defined using the `WITH` clause and makes complex queries more readab...
Expert Postgres Solution
SQL Query
WITH high_earners AS (
SELECT * FROM employees WHERE salary > 100000
)
SELECT * FROM high_earners WHERE department = 'Sales';Explain the 'EXPLAIN' and 'EXPLAIN ANALYZE' commands.
Intermediate- **EXPLAIN**: Shows the execution plan that the PostgreSQL planner generates for a query. - **EXPLAIN ANALYZE**: Actually executes the query and shows the real...
Expert Postgres Solution
What is MVCC (Multi-Version Concurrency Control)?
IntermediateMVCC allows multiple users to access the same data at the same time without locking each other out. When data is modified, a new version is created rather than ...
Expert Postgres Solution
What are the differences between JSON and JSONB?
Intermediate- **JSON**: Stores an exact copy of the input text. Parsing is done on every query. - **JSONB**: Stores data in a decomposed binary format. It is slower to inse...
Expert Postgres Solution
What is a GIN Index?
IntermediateGIN stands for **Generalized Inverted Index**. It is designed for data that contains multiple components, such as arrays or JSONB documents. It is excellent for...
Expert Postgres Solution
What is a Partial Index?
IntermediateA partial index is built over a subset of a table, defined by a conditional expression (the `WHERE` clause of the index). This saves space and improves performa...
Expert Postgres Solution
SQL Query
CREATE INDEX active_orders_idx ON orders (id) WHERE status = 'active';How do you perform a recursive query in PostgreSQL?
IntermediateYou use the `WITH RECURSIVE` clause. This is commonly used for hierarchical data like organizational charts or bill of materials.
Expert Postgres Solution
SQL Query
WITH RECURSIVE sub_org AS (
SELECT * FROM org WHERE name = 'Leadership'
UNION
SELECT org.* FROM org JOIN sub_org ON org.parent_id = sub_org.id
)
SELECT * FROM sub_org;What is the 'UPSERT' (INSERT ON CONFLICT) operation?
IntermediateUpsert allows you to either insert a new row or update an existing one if a conflict (like a unique constraint violation) occurs.
Expert Postgres Solution
SQL Query
INSERT INTO users (email, last_login) VALUES ('test@test.com', NOW())
ON CONFLICT (email) DO UPDATE SET last_login = EXCLUDED.last_login;What are the different isolation levels in PostgreSQL?
IntermediatePostgreSQL supports four isolation levels: 1. **Read Uncommitted** (behaves like Read Committed) 2. **Read Committed** (default) 3. **Repeatable Read** 4. **Ser...
Expert Postgres Solution
What is an extension in PostgreSQL?
IntermediateExtensions allow you to add new features, data types, and functions to your database. Common extensions include `postgis` (spatial data), `pg_stat_statements` (...
Expert Postgres Solution
Explain the 'RETURNING' clause in PostgreSQL.
IntermediateThe `RETURNING` clause allows you to return values from rows that were impacted by an `INSERT`, `UPDATE`, or `DELETE` operation.
Expert Postgres Solution
SQL Query
INSERT INTO users (name) VALUES ('Bob') RETURNING id;What is a Materialized View?
IntermediateA materialized view stores the result-set of a query physically on disk. It provides much faster read performance for complex queries but must be manually or sc...
Expert Postgres Solution
SQL Query
REFRESH MATERIALIZED VIEW my_view;How do you generate a UUID in PostgreSQL?
IntermediateYou can use the `gen_random_uuid()` function (built-in for Postgres 13+) or install the `uuid-ossp` extension and use `uuid_generate_v4()`.
Expert Postgres Solution
SQL Query
SELECT gen_random_uuid();What is 'ILIKE'?
Intermediate`ILIKE` is a PostgreSQL-specific operator for case-insensitive pattern matching. `LIKE` is case-sensitive.
Expert Postgres Solution
Explain the 'OVER' clause.
IntermediateThe `OVER` clause defines the 'window' over which a window function operates. It can include `PARTITION BY` and `ORDER BY` sub-clauses.
Expert Postgres Solution
What are Savepoints in a transaction?
IntermediateSavepoints allow you to roll back parts of a transaction without canceling the entire transaction. They are useful for error handling within large transactions.
Expert Postgres Solution
SQL Query
BEGIN;
INSERT INTO t1 VALUES (1);
SAVEPOINT my_save;
INSERT INTO t1 VALUES (2); -- Oops!
ROLLBACK TO my_save;
COMMIT;What is a Sequence in PostgreSQL?
IntermediateA sequence is a special kind of table that generates a sequence of numbers. They are commonly used to generate primary key values.
Expert Postgres Solution
SQL Query
SELECT nextval('my_sequence');What is the difference between 'VARCHAR' and 'TEXT'?
IntermediateIn PostgreSQL, there is no performance difference between `VARCHAR(N)` and `TEXT`. Both are stored in the same way. The only difference is that `VARCHAR(N)` enf...
Expert Postgres Solution
What is 'hstore'?
Intermediate`hstore` is an extension that provides a data type for storing sets of (key, value) pairs within a single value. It is useful for semi-structured data, though `...
Expert Postgres Solution
How do you handle character encoding in PostgreSQL?
IntermediatePostgreSQL uses **UTF-8** by default. You can set the encoding at the database level when creating it.
Expert Postgres Solution
SQL Query
CREATE DATABASE mydb WITH ENCODING 'UTF8';What is the 'DISTINCT ON' clause?
Intermediate`DISTINCT ON (field)` is a PostgreSQL extension that allows you to keep only the first row for each distinct set of values in the target fields. It is usually c...
Expert Postgres Solution
SQL Query
SELECT DISTINCT ON (customer_id) * FROM orders ORDER BY customer_id, order_date DESC;What is a TRIGGER in PostgreSQL?
IntermediateA trigger is a specification that the database should automatically execute a particular function whenever a certain type of operation is performed (e.g., `BEFO...
Expert Postgres Solution
Explain 'CAST' and the '::' shorthand.
IntermediateBoth are used for type conversion. `CAST(val AS type)` is standard SQL, while `val::type` is a PostgreSQL shorthand.
Expert Postgres Solution
SQL Query
SELECT '100'::INTEGER;What is the 'pg_stat_activity' view?
Intermediate`pg_stat_activity` is a system view that shows information about currently active database processes, including the user, query, and start time.
Expert Postgres Solution
Explain PostgreSQL Partitioning.
AdvancedPartitioning is the split of one large logical table into several smaller physical pieces. PostgreSQL support Declarative Partitioning by Range, List, or Hash. ...
Expert Postgres Solution
What is BRIN (Block Range Index)?
AdvancedBRIN indexes store the minimum and maximum values for a block of pages. They are much smaller than B-tree indexes and are ideal for very large tables where data...
Expert Postgres Solution
Explain GIST (Generalized Search Tree) vs GIN.
Advanced- **GIST**: Good for spatial data, full text search, and nearest neighbor searches. Slower to query than GIN but faster to update. - **GIN**: Best for multi-val...
Expert Postgres Solution
What is Table Inheritance?
AdvancedTable inheritance allows a table to inherit the columns and constraints of a parent table. While powerful, it has been largely superseded by Declarative Partiti...
Expert Postgres Solution
What are Logical and Physical Replication?
Advanced- **Physical Replication**: Copies the entire data directory byte-for-byte. Used for high availability and read-only standbys. - **Logical Replication**: Copies...
Expert Postgres Solution
Explain 'Vacuum Full' vs 'Vacuum'.
Advanced- **VACUUM**: Marks old rows as reusable but doesn't return space to the OS (except at the end of the file). - **VACUUM FULL**: Compacts the table and returns a...
Expert Postgres Solution
What are Foreign Data Wrappers (FDW)?
AdvancedFDW allows PostgreSQL to connect to and query external data sources (like other Postgres servers, MySQL, MongoDB, or even Redis) as if they were local tables.
Expert Postgres Solution
SQL Query
CREATE EXTENSION postgres_fdw;Explain the 'WAL' (Write-Ahead Logging).
AdvancedWAL ensures that no data is lost if the system crashes. Changes are written to a log file on disk before they are applied to the data files, ensuring that the d...
Expert Postgres Solution
What is an Advisory Lock?
AdvancedAdvisory locks are application-defined locks that have no inherent meaning to the database. They provide a way for applications to coordinate actions using the ...
Expert Postgres Solution
What is JIT (Just-In-Time) compilation in Postgres?
AdvancedSince version 11, PostgreSQL can use LLVM to compile parts of complex queries into machine code at runtime. This can significantly speed up CPU-bound queries th...
Expert Postgres Solution
How do you handle Deadlocks in PostgreSQL?
AdvancedPostgreSQL automatically detects deadlocks and aborts one of the transactions involved to resolve the conflict. Developers should handle these retries in their ...
Expert Postgres Solution
What is 'Streaming Replication'?
AdvancedStreaming replication allows a standby server to stay up-to-date with a primary server by continuously receiving WAL logs over a network connection, providing a...
Expert Postgres Solution
Explain 'Work Mem' and 'Maintenance Work Mem'.
Advanced- **work_mem**: The amount of memory used for internal sort operations and hash tables. High values speed up complex queries but use more RAM. - **maintenance_w...
Expert Postgres Solution
What is a Bloom Index?
AdvancedA Bloom index is based on Bloom filters. It is useful for tables where you have many columns and any subset of them might be used in a query. It is much smaller...
Expert Postgres Solution
Explain 'HOT' (Heap Only Tuple) updates.
AdvancedHOT updates allow PostgreSQL to update a row without updating its indexes, provided the change fits on the same disk page and no indexed columns were modified. ...
Expert Postgres Solution
What is 'pg_dump' vs 'pg_dumpall'?
Advanced- **pg_dump**: Backs up a single database. - **pg_dumpall**: Backs up all databases in a cluster, including global objects like roles and groups.
Expert Postgres Solution
How to find slow queries in PostgreSQL?
AdvancedEnable the `pg_stat_statements` extension. It tracks execution statistics for all SQL statements executed on the server, allowing you to find the queries that t...
Expert Postgres Solution
What is 'Postgis'?
AdvancedPostGIS is an extension that adds support for geographic objects to PostgreSQL, allowing it to be used as a spatial database for Geographic Information Systems ...
Expert Postgres Solution
Explain 'Declarative Partitioning'.
AdvancedIntroduced in Postgres 10, it allows you to define partitions directly using `PARTITION BY` in the table definition, eliminating the need for complex triggers a...
Expert Postgres Solution
What is 'Index Scan' vs 'Bitmap Index Scan' vs 'Sequential Scan'?
Advanced- **Sequential Scan**: Reads every row in the table (Slow for large datasets). - **Index Scan**: Follows the index directly to the row (Fast for small row count...
Expert Postgres Solution
What is 'Checkpointer' process?
AdvancedThe checkpointer periodically flushes 'dirty' data pages (changes made in RAM) to the disk. This ensures that the WAL logs can be reused and keeps the recovery ...
Expert Postgres Solution
What is 'Schema Search Path'?
AdvancedThe search path is a list of schemas that PostgreSQL looks through when a table is referenced without a schema name. It usually defaults to `"$user", public`.
Expert Postgres Solution
How to optimize queries with JSONB data?
Advanced1. Use GIN indexes for general containment queries. 2. Use expression indexes for specific nested fields. 3. Use the containment operator `@>` for best index pe...
Expert Postgres Solution
Explain 'LISTEN' and 'NOTIFY'.
AdvancedThis is a simple messaging system within PostgreSQL. A client can `LISTEN` to a channel, and another client can `NOTIFY` that channel with a message, which is d...
Expert Postgres Solution
What is 'Parallel Query' execution?
AdvancedPostgres can use multiple CPU cores to execute a single query. It splits the work (e.g., table scans, joins) across multiple background workers to speed up exec...
Expert Postgres Solution
Scenario: How to find the second highest salary without using LIMIT?
ScenarioUse a subquery with the `MAX()` function.
Expert Postgres Solution
SQL Query
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);Scenario: How to delete duplicate rows from a table while keeping the one with the lowest ID?
ScenarioUse a subquery with `MIN(id)` and a `GROUP BY` clause.
Expert Postgres Solution
SQL Query
DELETE FROM employees WHERE id NOT IN (
SELECT MIN(id) FROM employees GROUP BY email
);Scenario: How to find the average salary by department, and only show departments with an average > 50000?
ScenarioUse `GROUP BY` and `HAVING`.
Expert Postgres Solution
SQL Query
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;Scenario: How to perform a case-insensitive search for a username?
ScenarioUse the `ILIKE` operator or the `LOWER()` function.
Expert Postgres Solution
SQL Query
SELECT * FROM users WHERE username ILIKE 'john%';
-- OR
SELECT * FROM users WHERE LOWER(username) = 'john';Scenario: How to query a JSONB column to find rows where a specific key exists?
ScenarioUse the `?` operator.
Expert Postgres Solution
SQL Query
SELECT * FROM logs WHERE data ? 'error_code';Scenario: How to query a JSONB column to find rows where a key has a specific value?
ScenarioUse the `->>` operator or the containment operator `@>`.
Expert Postgres Solution
SQL Query
SELECT * FROM orders WHERE info ->> 'status' = 'shipped';
-- OR
SELECT * FROM orders WHERE info @> '{"status": "shipped"}';Scenario: How to combine first_name and last_name with a space in between?
ScenarioUse the `||` operator or the `CONCAT()` function.
Expert Postgres Solution
SQL Query
SELECT first_name || ' ' || last_name AS full_name FROM users;Scenario: How to get the first 5 characters of a string?
ScenarioUse the `LEFT()` or `SUBSTRING()` function.
Expert Postgres Solution
SQL Query
SELECT LEFT(description, 5) FROM products;Scenario: How to prevent a division by zero error in a query?
ScenarioUse the `NULLIF()` function.
Expert Postgres Solution
SQL Query
SELECT amount / NULLIF(quantity, 0) FROM sales;Scenario: How to find the nth most recent login for each user?
ScenarioUse the `ROW_NUMBER()` window function within a CTE.
Expert Postgres Solution
SQL Query
WITH RankedLogins AS (
SELECT user_id, login_date, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date DESC) as rn
FROM logins
)
SELECT * FROM RankedLogins WHERE rn = 2; -- 2nd most recentScenario: How to update multiple rows with different values in a single query?
ScenarioUse the `UPDATE ... FROM` syntax with a values list or a temporary table.
Expert Postgres Solution
SQL Query
UPDATE employees AS e SET salary = v.new_salary
FROM (VALUES (1, 60000), (2, 70000)) AS v(id, new_salary)
WHERE e.id = v.id;Scenario: How to copy only the structure of a table without any data?
ScenarioUse `CREATE TABLE ... AS` with a `WHERE false` condition.
Expert Postgres Solution
SQL Query
CREATE TABLE employees_backup AS SELECT * FROM employees WHERE false;Scenario: How to find rows where a column's value is between 10 and 20 (inclusive)?
ScenarioUse the `BETWEEN` operator.
Expert Postgres Solution
SQL Query
SELECT * FROM products WHERE price BETWEEN 10 AND 20;Scenario: How to handle auto-incrementing IDs for a table created from a CSV with existing IDs?
ScenarioAfter inserting, you must manually sync the sequence associated with the SERIAL column.
Expert Postgres Solution
SQL Query
SELECT setval(pg_get_serial_sequence('employees', 'id'), MAX(id)) FROM employees;Scenario: How to find the current database name?
ScenarioUse the `current_database()` function.
Expert Postgres Solution
SQL Query
SELECT current_database();Scenario: How to get the size of a database?
ScenarioUse the `pg_database_size()` function combined with `pg_size_pretty()` for readability.
Expert Postgres Solution
SQL Query
SELECT pg_size_pretty(pg_database_size('mydb'));Scenario: How to find which processes are currently blocking a specific table?
ScenarioQuery the `pg_locks` and `pg_stat_activity` views.
Expert Postgres Solution
SQL Query
SELECT pid, usename, query FROM pg_stat_activity
WHERE pid IN (SELECT pid FROM pg_locks WHERE relation = 'employees'::regclass);Scenario: How to kill a slow-running query process?
ScenarioUse the `pg_cancel_backend(pid)` or `pg_terminate_backend(pid)` functions.
Expert Postgres Solution
SQL Query
SELECT pg_terminate_backend(1234); -- pid 1234Scenario: How to generate a range of numbers (1 to 10)?
ScenarioUse the `generate_series()` function.
Expert Postgres Solution
SQL Query
SELECT generate_series(1, 10);Scenario: How to convert a comma-separated string into rows?
ScenarioUse `string_to_array()` and `unnest()`.
Expert Postgres Solution
SQL Query
SELECT unnest(string_to_array('apple,banana,cherry', ','));Scenario: How to get the difference between two timestamps in hours?
ScenarioSubtract the timestamps to get an interval, then use `EXTRACT(EPOCH ...)` / 3600.
Expert Postgres Solution
SQL Query
SELECT EXTRACT(EPOCH FROM (end_time - start_time)) / 3600 FROM tasks;Scenario: How to find all columns in a specific table using SQL?
ScenarioQuery the `information_schema.columns` view.
Expert Postgres Solution
SQL Query
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'employees';Scenario: How to find the running total of sales over time?
ScenarioUse a window function with `SUM()`.
Expert Postgres Solution
SQL Query
SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) as running_total FROM sales;Scenario: You need to store multiple IP addresses in a single column and query them efficiently. Solution?
ScenarioUse the `INET` or `CIDR` data types and create a GIST index. These types provide built-in validation and allow for efficient network overlap queries.
Expert Postgres Solution
How to export a table to a CSV file?
ScenarioUse the `COPY` command (requires server-side access) or the `\copy` meta-command in `psql` (client-side).
Expert Postgres Solution
SQL Query
\copy employees TO 'employees.csv' CSV HEADER;