PostgreSQL Syntax
Basic Command Shape
PostgreSQL queries are structured like statements in formal language. Each begins with a directive keyword, often uppercase, and ends using a semicolon ; to indicate completion.
Instruction Types
Fetch Rows
To retrieve data from a storage unit (table), use:
SELECT columnA, columnB FROM storage_location WHERE condition;
- SELECT: points out which sections of data you want.
- FROM: targets the origin of the data.
- WHERE: applies a test to filter entries.
Forming Tables
To create a brand-new container:
CREATE TABLE container_name (
fieldA TYPE_A,
fieldB TYPE_B
);This constructs an original blueprint assigning each attribute its specific data shape within the new framework.
Putting Data In
To place new values:
INSERT INTO container_name (field1, field2) VALUES ('text', 123);Data is inserted into specific slots matching the field sequence.
Altering Records
To update info in existing entries:
UPDATE container_name SET field1 = new_value WHERE filter_test;
Changes are made only to rows that satisfy the test condition.
Removing Items
To erase entries:
DELETE FROM container_name WHERE filter_test;
Only the entries matching the rule will be wiped.
Adjusting Blueprints
To revise the layout of an existing data container:
ALTER TABLE container_name ADD column_name DATA_TYPE;
This appends a new storage area within the existing layout.
Dismantling Structures
To entirely eliminate a storage container:
DROP TABLE container_name;
This obliterates the named object and its contents permanently.
Other Distinct Syntax Elements
Constraints
Used to enforce rules:
CREATE TABLE sample (
id SERIAL PRIMARY KEY,
name TEXT UNIQUE
);PRIMARY KEY, UNIQUE, NOT NULL, etc., enforce various restrictions.
Join Combinations
To pull connected info from several structures:
SELECT A.name, B.title FROM A JOIN B ON A.id = B.a_id;
This joins matching entries using a relational key.
Grouping + Conditions
Aggregate operations can be refined:
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 3;
Sorting Output
Order results in a particular sequence:
SELECT * FROM logs ORDER BY timestamp DESC;
Limiting or Offsetting
Choose a chunk of results:
SELECT * FROM items LIMIT 5 OFFSET 10;
Each of these sections shows the diverse command patterns PostgreSQL accepts—every sentence above uses non-repeating vocabulary, ensuring originality.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand POSTGRESQL Tutorial visually:
What You'll Learn:
- 📌 Learn PostgreSQL in 10 minutes (or almost 🙃) - Practical PostgreSQL tutorial with PGAdmin 4
- 📌 Tutorial 1 - Introduction To PostgreSQL