|
| 1 | +# PostgreSQL Beginner Interview Questions and Answers |
| 2 | + |
| 3 | +This document contains a set of 30 beginner-friendly PostgreSQL questions along with detailed answers. These questions are suitable for interview preparation. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. What is PostgreSQL? |
| 8 | +**Answer:** |
| 9 | +PostgreSQL is an open-source, object-relational database management system (ORDBMS) that supports both SQL (relational) and JSON (non-relational) querying. It is highly extensible, reliable, and supports advanced data types. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 2. How do you create a new database in PostgreSQL? |
| 14 | +**Answer:** |
| 15 | +```sql |
| 16 | +CREATE DATABASE my_database; |
| 17 | +``` |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 3. How do you list all databases in PostgreSQL? |
| 22 | +**Answer:** |
| 23 | +```sql |
| 24 | +\l |
| 25 | +-- or using SQL |
| 26 | +SELECT datname FROM pg_database; |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 4. How do you connect to a specific database? |
| 32 | +**Answer:** |
| 33 | +```sql |
| 34 | +\c my_database |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 5. How do you create a table in PostgreSQL? |
| 40 | +**Answer:** |
| 41 | +```sql |
| 42 | +CREATE TABLE employees ( |
| 43 | + id SERIAL PRIMARY KEY, |
| 44 | + name VARCHAR(50) NOT NULL, |
| 45 | + age INT CHECK (age > 0), |
| 46 | + department VARCHAR(50) |
| 47 | +); |
| 48 | +``` |
| 49 | +- `SERIAL` automatically generates a unique number for `id`. |
| 50 | +- `CHECK` enforces constraints on data integrity. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 6. How do you insert data into a table? |
| 55 | +**Answer:** |
| 56 | +```sql |
| 57 | +INSERT INTO employees (name, age, department) |
| 58 | +VALUES ('John Doe', 30, 'IT'); |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 7. How do you retrieve all data from a table? |
| 64 | +**Answer:** |
| 65 | +```sql |
| 66 | +SELECT * FROM employees; |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 8. How do you retrieve specific columns from a table? |
| 72 | +**Answer:** |
| 73 | +```sql |
| 74 | +SELECT name, department FROM employees; |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## 9. How do you update data in a table? |
| 80 | +**Answer:** |
| 81 | +```sql |
| 82 | +UPDATE employees |
| 83 | +SET department = 'HR' |
| 84 | +WHERE name = 'John Doe'; |
| 85 | +``` |
| 86 | +- `WHERE` clause ensures only the intended row(s) are updated. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## 10. How do you delete data from a table? |
| 91 | +**Answer:** |
| 92 | +```sql |
| 93 | +DELETE FROM employees |
| 94 | +WHERE name = 'John Doe'; |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## 11. How do you add a new column to an existing table? |
| 100 | +**Answer:** |
| 101 | +```sql |
| 102 | +ALTER TABLE employees |
| 103 | +ADD COLUMN salary NUMERIC(10,2); |
| 104 | +``` |
| 105 | +- `NUMERIC(10,2)` stores numbers with 10 digits total and 2 decimal places. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## 12. How do you remove a column from a table? |
| 110 | +**Answer:** |
| 111 | +```sql |
| 112 | +ALTER TABLE employees |
| 113 | +DROP COLUMN salary; |
| 114 | +``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## 13. How do you rename a column? |
| 119 | +**Answer:** |
| 120 | +```sql |
| 121 | +ALTER TABLE employees |
| 122 | +RENAME COLUMN department TO dept; |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## 14. How do you rename a table? |
| 128 | +**Answer:** |
| 129 | +```sql |
| 130 | +ALTER TABLE employees |
| 131 | +RENAME TO staff; |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 15. How do you create an index on a column? |
| 137 | +**Answer:** |
| 138 | +```sql |
| 139 | +CREATE INDEX idx_name ON employees(name); |
| 140 | +``` |
| 141 | +- Indexes improve query performance on frequently searched columns. |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 16. What is the difference between `CHAR`, `VARCHAR`, and `TEXT`? |
| 146 | +**Answer:** |
| 147 | +- `CHAR(n)`: Fixed-length string, pads with spaces if less than n characters. |
| 148 | +- `VARCHAR(n)`: Variable-length string with a maximum limit of n characters. |
| 149 | +- `TEXT`: Variable-length string with no maximum limit. |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## 17. How do you find all employees older than 25? |
| 154 | +**Answer:** |
| 155 | +```sql |
| 156 | +SELECT * FROM employees |
| 157 | +WHERE age > 25; |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 18. How do you sort query results by a column in ascending order? |
| 163 | +**Answer:** |
| 164 | +```sql |
| 165 | +SELECT * FROM employees |
| 166 | +ORDER BY age ASC; |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## 19. How do you sort query results in descending order? |
| 172 | +**Answer:** |
| 173 | +```sql |
| 174 | +SELECT * FROM employees |
| 175 | +ORDER BY age DESC; |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## 20. How do you count the number of rows in a table? |
| 181 | +**Answer:** |
| 182 | +```sql |
| 183 | +SELECT COUNT(*) FROM employees; |
| 184 | +``` |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## 21. How do you find the maximum and minimum age? |
| 189 | +**Answer:** |
| 190 | +```sql |
| 191 | +SELECT MAX(age) AS max_age, MIN(age) AS min_age |
| 192 | +FROM employees; |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## 22. How do you calculate the average age? |
| 198 | +**Answer:** |
| 199 | +```sql |
| 200 | +SELECT AVG(age) AS average_age FROM employees; |
| 201 | +``` |
| 202 | + |
| 203 | +--- |
| 204 | + |
| 205 | +## 23. How do you find unique departments? |
| 206 | +**Answer:** |
| 207 | +```sql |
| 208 | +SELECT DISTINCT department FROM employees; |
| 209 | +``` |
| 210 | + |
| 211 | +--- |
| 212 | + |
| 213 | +## 24. How do you filter records using multiple conditions? |
| 214 | +**Answer:** |
| 215 | +```sql |
| 216 | +SELECT * FROM employees |
| 217 | +WHERE age > 25 AND department = 'IT'; |
| 218 | +``` |
| 219 | + |
| 220 | +--- |
| 221 | + |
| 222 | +## 25. How do you use the `LIKE` operator? |
| 223 | +**Answer:** |
| 224 | +```sql |
| 225 | +SELECT * FROM employees |
| 226 | +WHERE name LIKE 'J%'; -- Names starting with J |
| 227 | +``` |
| 228 | + |
| 229 | +--- |
| 230 | + |
| 231 | +## 26. How do you use `IN` to filter specific values? |
| 232 | +**Answer:** |
| 233 | +```sql |
| 234 | +SELECT * FROM employees |
| 235 | +WHERE department IN ('IT', 'HR'); |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## 27. How do you use `BETWEEN` to filter a range? |
| 241 | +**Answer:** |
| 242 | +```sql |
| 243 | +SELECT * FROM employees |
| 244 | +WHERE age BETWEEN 25 AND 35; |
| 245 | +``` |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +## 28. How do you join two tables? |
| 250 | +**Answer:** |
| 251 | +```sql |
| 252 | +SELECT e.name, d.name AS dept_name |
| 253 | +FROM employees e |
| 254 | +JOIN departments d ON e.department_id = d.id; |
| 255 | +``` |
| 256 | +- `JOIN` combines rows from two tables based on a related column. |
| 257 | + |
| 258 | +--- |
| 259 | + |
| 260 | +## 29. How do you group data? |
| 261 | +**Answer:** |
| 262 | +```sql |
| 263 | +SELECT department, COUNT(*) AS total_employees |
| 264 | +FROM employees |
| 265 | +GROUP BY department; |
| 266 | +``` |
| 267 | +- `GROUP BY` aggregates rows with the same column value. |
| 268 | + |
| 269 | +--- |
| 270 | + |
| 271 | +## 30. How do you delete a table? |
| 272 | +**Answer:** |
| 273 | +```sql |
| 274 | +DROP TABLE employees; |
| 275 | +``` |
| 276 | + |
| 277 | +--- |
| 278 | + |
0 commit comments