From d6642928a5cb25b3e79a9fd1a5d10d5925c7cec3 Mon Sep 17 00:00:00 2001 From: Divyansh Garg Date: Thu, 2 Oct 2025 19:36:56 +0530 Subject: [PATCH 1/2] Fix README formatting, links, and minor grammar --- README.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6cca149..94cfec3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # Learn PostgreSQL Welcome to the **Learn PostgreSQL** repository! This guide provides comprehensive resources to help you understand and master **PostgreSQL**, a powerful open-source relational database management system known for its reliability, scalability, and extensive feature set. PostgreSQL supports a wide variety of data types and offers advanced functionality that makes it suitable for high-performance, data-intensive applications. @@ -17,16 +16,16 @@ Welcome to the **Learn PostgreSQL** repository! This guide provides comprehensiv - [SQL Clauses](psql/clauses.md) - [Advanced Concepts](psql/advance.md) - [Functions & Procedures](psql/functions.md) - - [ Practice & Examples](#-practice--examples) - - [ Contributing](#contributing) - - [License](#license) - - [Acknowledgements](#acknowledgements) +- [💪 Practice & Examples](#-practice--examples) +- [Contributing](#contributing) +- [License](#license) +- [Acknowledgements](#acknowledgements) ## Introduction **PostgreSQL**, often referred to as **Postgres**, has been in active development since 1989 and has evolved into one of the most robust relational database management systems available today. Known for its **ACID compliance** (Atomicity, Consistency, Isolation, Durability), it ensures the reliability and integrity of your data. Its advanced features like **multi-version concurrency control (MVCC)**, **triggers**, **foreign keys**, and **stored procedures** make PostgreSQL a top choice for a wide range of applications, from small web apps to large enterprise systems. -Whether you're a beginner or an experienced developer, this guide will walk you through the essentials of PostgreSQL, from installation to advanced database management techniques. +Whether you're a beginner or an experienced developer, this guide **walks you through** the essentials of PostgreSQL, from installation to advanced database management techniques. ## Key Features @@ -34,7 +33,7 @@ PostgreSQL offers numerous features that make it stand out as a leading database - **ACID Compliance**: Guarantees data reliability and consistency in all transactions. - **JSON Support**: Offers robust support for handling JSON data, allowing for hybrid relational/NoSQL models. -- **High Performance**: Optimized for speed, handling complex queries efficiently, even under heavy load. +- **High Performance & Scalability**: Optimized for speed, handling complex queries efficiently, even under heavy load. - **Security**: Provides secure authentication methods, encryption, and access control for data protection. - **Open Source**: Free to use, modify, and distribute under the PostgreSQL license. - **Replication & Clustering**: Supports replication for high availability and horizontal scaling. @@ -49,7 +48,7 @@ PostgreSQL offers numerous features that make it stand out as a leading database 🟢 **Absolute Beginner** (8-12 hours) - Start with [Installation](psql/installation.md) -- Learn [Basic Queries](psql/query.md) +- Learn [Basic Queries](psql/query.md) - Practice with [Beginner Exercises](exercises/beginner-exercises.md) 🟡 **Some SQL Experience** (12-16 hours) @@ -100,5 +99,4 @@ This project is licensed under the [MIT License](LICENSE), allowing you the free ## Acknowledgements -This documentation was created with assistance from tools like **ChatGPT**, **Google Bard**, and various online resources. For more details, see the [declaration page](declaration.md). - +This documentation was created with assistance from tools like **ChatGPT**, **Google Bard**, and various online resources. For more details, see the [declaration page](declaration.md). \ No newline at end of file From 74f6352a6fad9806b6588bb4a273771373b2bc80 Mon Sep 17 00:00:00 2001 From: Divyansh Garg Date: Fri, 3 Oct 2025 19:30:25 +0530 Subject: [PATCH 2/2] Finish merge with remote main --- my-sql-query.md | 74 ++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/my-sql-query.md b/my-sql-query.md index 864ebdb..6560691 100644 --- a/my-sql-query.md +++ b/my-sql-query.md @@ -1,42 +1,42 @@ ```sql -- Create a table named department CREATE TABLE department ( - dept_name VARCHAR(100), + dept_name VARCHAR(100) PRIMARY KEY, building VARCHAR(120) NOT NULL, - budget NUMERIC(12,2) NOT NULL DEFAULT 'undecided', - num_staff INT, - PRIMARY KEY (dept_name) + budget NUMERIC(12,2) NOT NULL DEFAULT 0.00, + num_staff INT ); -- Insert values into the department table +-- Removed duplicates because dept_name is a PRIMARY KEY INSERT INTO department (dept_name, building, budget, num_staff) -VALUES ('cse', 'ma wazed', 12.20, 10), - ('cse', 'ma wazed', 12.20, 10), - ('cse', 'ma wazed', 12.20, 10); +VALUES ('cse', 'ma wazed', 12.20, 10); -- Add a column named dept_code to the existing department table -ALTER TABLE department ADD dept_code VARCHAR(6) NOT NULL; +ALTER TABLE department ADD COLUMN dept_code VARCHAR(6) NOT NULL DEFAULT 'NA'; --- Add a column named dep_code to the existing department table after the dept_name column -ALTER TABLE department ADD dep_code VARCHAR(6) NOT NULL AFTER dept_name; +-- Add a column named dep_code to the existing department table +-- PostgreSQL does not support 'AFTER', so we just add it +ALTER TABLE department ADD COLUMN dep_code VARCHAR(6) NOT NULL DEFAULT 'NA'; -- Remove the dept_code column from the department table ALTER TABLE department DROP COLUMN dept_code; --- Modify the data type of the dept_code column to VARCHAR(10) in the department table -ALTER TABLE department MODIFY COLUMN dept_code VARCHAR(10); - --- Delete the department table along with its structure -DROP TABLE department; +-- Modify the data type of the dep_code column to VARCHAR(10) in the department table +ALTER TABLE department ALTER COLUMN dep_code TYPE VARCHAR(10); -- Delete all data from the department table, keeping the table structure DELETE FROM department; -- OR TRUNCATE department; +-- Delete the department table along with its structure +DROP TABLE department; + -- Insert values into a table (TABLE2) from another table (TABLE1) based on a condition +-- Replace CONDITION with an actual condition INSERT INTO TABLE2 (employeeId, employeeName) -SELECT id, firstName FROM TABLE1 WHERE CONDITION; +SELECT id, firstName FROM TABLE1 WHERE 1=1; -- Update the salary column of the instructor table by increasing it by 5% for instructors with salary below the average salary UPDATE instructor @@ -63,7 +63,7 @@ LIMIT n; -- Retrieve a limited number of rows from a table, starting from the mth row SELECT * FROM TABLE_NAME -LIMIT m, n; +LIMIT n OFFSET m; -- Retrieve records from the instructor table where the name is not 'pranto' or 'Zahid' SELECT * @@ -113,10 +113,11 @@ SELECT dept_name, COUNT(id) AS num_of_teacher FROM instructor GROUP BY dept_name; --- Perform a natural join between the employee and branch tables +-- Perform a join between the employee and branch tables +-- Replaced [LEFT|RIGHT] with LEFT JOIN SELECT branch_name, first_name, last_name FROM employee -[LEFT|RIGHT] JOIN branch +LEFT JOIN branch ON employee.emp_id = branch.mgr_id; -- Perform a nested query to retrieve employee names based on their IDs in the works_with table @@ -129,33 +130,38 @@ WHERE employee.emp_id IN ( ); -- Create a trigger that inserts values into another table when a new row is inserted into table1 -DELIMITER | -CREATE OR REPLACE TRIGGER triggertest -BEFORE INSERT -ON table1 -FOR EACH ROW +CREATE OR REPLACE FUNCTION triggertest_func() +RETURNS TRIGGER AS $$ BEGIN INSERT INTO table2 VALUES (NEW.id, NEW.name); + RETURN NEW; END; -| -DELIMITER ; +$$ LANGUAGE plpgsql; --- Create a trigger with conditional statements that inserts values into another table based on the sex column in the employee table -DELIMITER | -CREATE TRIGGER my_trigger BEFORE INSERT -ON employee +CREATE TRIGGER my_trigger1 +BEFORE INSERT ON table1 FOR EACH ROW +EXECUTE FUNCTION triggertest_func(); + +-- Create a trigger with conditional statements that inserts values into another table based on the sex column in the employee table +CREATE OR REPLACE FUNCTION my_trigger_func() +RETURNS TRIGGER AS $$ BEGIN IF NEW.sex = 'M' THEN INSERT INTO trigger_test VALUES ('added male employee'); - ELSEIF NEW.sex = 'F' THEN + ELSIF NEW.sex = 'F' THEN INSERT INTO trigger_test VALUES ('added female'); ELSE INSERT INTO trigger_test VALUES ('added other employee'); END IF; + RETURN NEW; END; -| -DELIMITER ; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER my_trigger2 +BEFORE INSERT ON employee +FOR EACH ROW +EXECUTE FUNCTION my_trigger_func(); -- Example of a complex query involving multiple tables and joins SELECT t.eName @@ -168,5 +174,3 @@ INNER JOIN employee AS e ON t.mName = e.eName AND t.city = e.city AND t.street = e.street; - -``` \ No newline at end of file