PostgreSQL Interview Questions and Answers for 10 years experience
-
What are the different data types supported by PostgreSQL? Give examples of when you'd use each.
- Answer: PostgreSQL supports a wide variety of data types, including:
- integer: For whole numbers (e.g., age, quantity). Use when you need precise integer representation and storage efficiency.
- bigint: For larger whole numbers than integer can handle.
- smallint: For smaller whole numbers, offering better storage efficiency than integer in certain cases.
- numeric/decimal: For precise decimal numbers (e.g., financial data). Avoid floating-point imprecision.
- real/double precision: For floating-point numbers (e.g., scientific data, where slight imprecision is acceptable). Offers speed advantages over numeric for calculations.
- character varying (varchar): For variable-length strings (e.g., names, addresses). More efficient than character for varying length text.
- character (char): For fixed-length strings. Use when you need strings of a specific length, padding with spaces if necessary.
- text: For large text strings (e.g., articles, descriptions). Offers unlimited length.
- boolean: For true/false values.
- date: For dates (e.g., birthdate, order date).
- time: For times (e.g., event time).
- timestamp: For date and time combined (e.g., transaction time).
- json/jsonb: For storing JSON data.
- uuid: Universally Unique Identifiers, useful for generating unique IDs.
- bytea: For binary data.
- Answer: PostgreSQL supports a wide variety of data types, including:
-
Explain the difference between `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, and `FULL OUTER JOIN`. Provide SQL examples.
- Answer: These are all relational join operations used to combine rows from two or more tables based on a related column.
- INNER JOIN: Returns rows only when there is a match in both tables.
SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
- LEFT JOIN: Returns all rows from the left table (table1), even if there is no match in the right table (table2). For unmatched rows in table1, the columns from table2 will have NULL values.
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
- RIGHT JOIN: Returns all rows from the right table (table2), even if there is no match in the left table (table1). For unmatched rows in table2, the columns from table1 will have NULL values.
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
- FULL OUTER JOIN: Returns all rows from both tables. If there is a match, the corresponding rows are combined. If there is no match, the columns from the other table will have NULL values. PostgreSQL doesn't directly support `FULL OUTER JOIN` using this syntax; it needs to be simulated using `UNION` of `LEFT JOIN` and `RIGHT JOIN`.
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id; --Not directly supported, requires UNION ALL of LEFT and RIGHT JOIN.
- INNER JOIN: Returns rows only when there is a match in both tables.
- Answer: These are all relational join operations used to combine rows from two or more tables based on a related column.
Thank you for reading our blog post on 'PostgreSQL Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!