Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
* [Home](README.md)
* [Declaration](declaration.md)
* [Learning Path](LEARNING-PATH.md)
* **Installation**
* [Installation](psql/installation.md)
* **Basics**
* [Data Types](psql/data-types.md)
* [Database](psql/database.md)
* [Functions](psql/functions.md)
* [Queries](psql/query.md)
* [Table & Schema](psql/table-schema.md)
* [Window Functions](psql/Window.md)
* **Advanced**
* [Clauses](psql/clauses.md)
* [CTE](psql/CTE.md)
* [Advance](psql/advance.md)
* **JSON / APIs**
* [JSON Guide](psql/json-jsonb-guide.md)
* [psql in ExpressJS](psql/psql-in-expressJS-guide.md)
* [my-sql-query](my-sql-query.md)
* [psql-query](psql-query.md)
* **Exercises**
* [Beginner Exercises](exercises/beginner-exercises.md)
16 changes: 16 additions & 0 deletions assets/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Example styling for branding */
body::before {
content: "Learn PostgreSQL";
display: block;
text-align: center;
font-size: 1.2rem;
font-weight: bold;
padding: 10px;
background-color: #3F51B5;
color: white;
}

a[href$=".pdf"] {
color: #ff5722;
font-weight: bold;
}
278 changes: 278 additions & 0 deletions exercises/beginner-exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
# PostgreSQL Beginner Interview Questions and Answers

This document contains a set of 30 beginner-friendly PostgreSQL questions along with detailed answers. These questions are suitable for interview preparation.

---

## 1. What is PostgreSQL?
**Answer:**
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.

---

## 2. How do you create a new database in PostgreSQL?
**Answer:**
```sql
CREATE DATABASE my_database;
```

---

## 3. How do you list all databases in PostgreSQL?
**Answer:**
```sql
\l
-- or using SQL
SELECT datname FROM pg_database;
```

---

## 4. How do you connect to a specific database?
**Answer:**
```sql
\c my_database
```

---

## 5. How do you create a table in PostgreSQL?
**Answer:**
```sql
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age > 0),
department VARCHAR(50)
);
```
- `SERIAL` automatically generates a unique number for `id`.
- `CHECK` enforces constraints on data integrity.

---

## 6. How do you insert data into a table?
**Answer:**
```sql
INSERT INTO employees (name, age, department)
VALUES ('John Doe', 30, 'IT');
```

---

## 7. How do you retrieve all data from a table?
**Answer:**
```sql
SELECT * FROM employees;
```

---

## 8. How do you retrieve specific columns from a table?
**Answer:**
```sql
SELECT name, department FROM employees;
```

---

## 9. How do you update data in a table?
**Answer:**
```sql
UPDATE employees
SET department = 'HR'
WHERE name = 'John Doe';
```
- `WHERE` clause ensures only the intended row(s) are updated.

---

## 10. How do you delete data from a table?
**Answer:**
```sql
DELETE FROM employees
WHERE name = 'John Doe';
```

---

## 11. How do you add a new column to an existing table?
**Answer:**
```sql
ALTER TABLE employees
ADD COLUMN salary NUMERIC(10,2);
```
- `NUMERIC(10,2)` stores numbers with 10 digits total and 2 decimal places.

---

## 12. How do you remove a column from a table?
**Answer:**
```sql
ALTER TABLE employees
DROP COLUMN salary;
```

---

## 13. How do you rename a column?
**Answer:**
```sql
ALTER TABLE employees
RENAME COLUMN department TO dept;
```

---

## 14. How do you rename a table?
**Answer:**
```sql
ALTER TABLE employees
RENAME TO staff;
```

---

## 15. How do you create an index on a column?
**Answer:**
```sql
CREATE INDEX idx_name ON employees(name);
```
- Indexes improve query performance on frequently searched columns.

---

## 16. What is the difference between `CHAR`, `VARCHAR`, and `TEXT`?
**Answer:**
- `CHAR(n)`: Fixed-length string, pads with spaces if less than n characters.
- `VARCHAR(n)`: Variable-length string with a maximum limit of n characters.
- `TEXT`: Variable-length string with no maximum limit.

---

## 17. How do you find all employees older than 25?
**Answer:**
```sql
SELECT * FROM employees
WHERE age > 25;
```

---

## 18. How do you sort query results by a column in ascending order?
**Answer:**
```sql
SELECT * FROM employees
ORDER BY age ASC;
```

---

## 19. How do you sort query results in descending order?
**Answer:**
```sql
SELECT * FROM employees
ORDER BY age DESC;
```

---

## 20. How do you count the number of rows in a table?
**Answer:**
```sql
SELECT COUNT(*) FROM employees;
```

---

## 21. How do you find the maximum and minimum age?
**Answer:**
```sql
SELECT MAX(age) AS max_age, MIN(age) AS min_age
FROM employees;
```

---

## 22. How do you calculate the average age?
**Answer:**
```sql
SELECT AVG(age) AS average_age FROM employees;
```

---

## 23. How do you find unique departments?
**Answer:**
```sql
SELECT DISTINCT department FROM employees;
```

---

## 24. How do you filter records using multiple conditions?
**Answer:**
```sql
SELECT * FROM employees
WHERE age > 25 AND department = 'IT';
```

---

## 25. How do you use the `LIKE` operator?
**Answer:**
```sql
SELECT * FROM employees
WHERE name LIKE 'J%'; -- Names starting with J
```

---

## 26. How do you use `IN` to filter specific values?
**Answer:**
```sql
SELECT * FROM employees
WHERE department IN ('IT', 'HR');
```

---

## 27. How do you use `BETWEEN` to filter a range?
**Answer:**
```sql
SELECT * FROM employees
WHERE age BETWEEN 25 AND 35;
```

---

## 28. How do you join two tables?
**Answer:**
```sql
SELECT e.name, d.name AS dept_name
FROM employees e
JOIN departments d ON e.department_id = d.id;
```
- `JOIN` combines rows from two tables based on a related column.

---

## 29. How do you group data?
**Answer:**
```sql
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
```
- `GROUP BY` aggregates rows with the same column value.

---

## 30. How do you delete a table?
**Answer:**
```sql
DROP TABLE employees;
```

---

32 changes: 32 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Learn PostgreSQL</title>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify/themes/vue.css" />
<link rel="stylesheet" href="assets/custom.css" />
</head>
<body>
<!-- Docsify container -->
<div id="app"></div>

<!-- Docsify Core -->
<script src="//cdn.jsdelivr.net/npm/docsify@4/lib/docsify.min.js"></script>

<!-- Docsify Configuration -->
<script>
window.$docsify = {
name: 'Learn PostgreSQL',
repo: 'fahimahammed/learn-postgresql',
loadSidebar: '_sidebar.md', // Ensure _sidebar.md exists in the repo root
subMaxLevel: 2,
search: 'auto',
themeColor: '#3F51B5'
}
</script>

<!-- Docsify Plugins -->
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/search.min.js"></script>
</body>
</html>