
Syntax in PostgreSql
Syntax in PostgreSQL
PostgreSQL follows a specific syntax structure for various SQL commands. Here's an overview of the common syntax used in PostgreSQL queries.
1. Basic SELECT
Syntax
SELECT column1, column2, ...FROM table_nameWHERE conditionORDER BY columnLIMIT number_of_rows;
SELECT
: Specifies which columns to retrieve.FROM
: Specifies the table from which to retrieve the data.WHERE
: Filters records based on a condition.ORDER BY
: Sorts the results.LIMIT
: Limits the number of rows returned.
Example:
SELECT first_name, last_nameFROM employeesWHERE department = 'IT'ORDER BY salary DESCLIMIT 5;
2. INSERT INTO
Syntax
The INSERT INTO
statement is used to insert data into a table.
INSERT INTO table_name (column1, column2, ...)VALUES (value1, value2, ...);
table_name
: The name of the table.(column1, column2, ...)
: The columns you want to insert data into.VALUES
: The values to insert into the respective columns.
Example:
INSERT INTO employees (first_name, last_name, department, salary)VALUES ('John', 'Doe', 'IT', 60000);
3. UPDATE
Syntax
The UPDATE
statement is used to modify existing records.
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;
SET
: Specifies the columns to update and their new values.WHERE
: Filters which records to update.
Example:
UPDATE employeesSET salary = 65000WHERE department = 'IT';
4. DELETE
Syntax
The DELETE
statement removes records from a table.
DELETE FROM table_nameWHERE condition;
WHERE
: Specifies which rows to delete. Without this clause, all records will be deleted.
Example:
DELETE FROM employeesWHERE department = 'HR';
5. CREATE TABLE
Syntax
The CREATE TABLE
statement is used to create a new table.
CREATE TABLE table_name ( column1 datatype constraints, column2 datatype constraints, ...);
column_name
: The name of each column.datatype
: The type of data the column will store (e.g.,VARCHAR
,INTEGER
,DATE
).constraints
: Optional constraints (e.g.,PRIMARY KEY
,NOT NULL
).
Example:
CREATE TABLE employees ( id SERIAL PRIMARY KEY, first_name VARCHAR(100), last_name VARCHAR(100), department VARCHAR(50), salary DECIMAL(10, 2));
6. ALTER TABLE
Syntax
The ALTER TABLE
statement is used to modify the structure of an existing table.
Add a column:
ALTER TABLE table_nameADD COLUMN column_name datatype;
Drop a column:
ALTER TABLE table_nameDROP COLUMN column_name;
Rename a column:
ALTER TABLE table_nameRENAME COLUMN old_column TO new_column;
Example:
ALTER TABLE employeesADD COLUMN hire_date DATE;
7. CREATE INDEX
Syntax
The CREATE INDEX
statement creates an index on one or more columns of a table.
CREATE INDEX index_nameON table_name (column1, column2, ...);
index_name
: The name of the index.column_name
: The column(s) for which the index will be created.
Example:
CREATE INDEX idx_salaryON employees (salary);
8. DROP
Syntax
The DROP
statement is used to remove objects such as tables, columns, or indexes.
Drop a table:
DROP TABLE table_name;
Drop a column:
ALTER TABLE table_nameDROP COLUMN column_name;
Drop an index:
DROP INDEX index_name;
Example:
DROP TABLE employees;
9. JOIN
Syntax
The JOIN
clause is used to combine rows from two or more tables, based on a related column.
Inner Join: Returns rows where there is a match in both tables.
SELECT column1, column2FROM table1INNER JOIN table2ON table1.column = table2.column;
Left Join: Returns all rows from the left table, and the matched rows from the right table.
SELECT column1, column2FROM table1LEFT JOIN table2ON table1.column = table2.column;
Example:
SELECT e.first_name, e.last_name, d.department_nameFROM employees eINNER JOIN departments dON e.department_id = d.id;
10. GROUP BY
and Aggregates
The GROUP BY
clause groups rows that have the same values into summary rows, often used with aggregate functions.
SELECT column, COUNT(*) FROM table_nameGROUP BY column;
Example:
SELECT department, AVG(salary)FROM employeesGROUP BY department;
11. Subqueries Syntax
A subquery is a query nested inside another query.
SELECT columnFROM tableWHERE column IN (SELECT column FROM table WHERE condition);
Example:
SELECT first_nameFROM employeesWHERE department_id IN (SELECT id FROM departments WHERE location = 'New York');
12. Transaction Control Syntax
You can manage transactions with commands like BEGIN
, COMMIT
, and ROLLBACK
.
Start a transaction:
BEGIN;
Commit the transaction:
COMMIT;
Rollback the transaction:
ROLLBACK;
Example:
BEGIN;UPDATE employees SET salary = 70000 WHERE department = 'IT';COMMIT;
13. EXPLAIN
Syntax
The EXPLAIN
statement is used to display the execution plan of a query, useful for query optimization.
EXPLAIN SELECT * FROM employees WHERE salary > 50000;
Best Practices
- Indentation and formatting improve readability.
- Always use
WHERE
clauses to avoid accidental updates or deletes of all rows. - Transactions help ensure data integrity, especially with complex operations.