WikiGalaxy

Personalize

PostgreSQL Syntax: SELECT Statement

SELECT Statement:

The SELECT statement is used to query the database and retrieve data from one or more tables. It is the most commonly used command in SQL.


SELECT column1, column2 FROM table_name WHERE condition;
    

Example:

This query selects the columns 'name' and 'age' from the 'users' table where the age is greater than 30.


SELECT name, age FROM users WHERE age > 30;
    

Console Output:

John, 35

Alice, 32

PostgreSQL Syntax: INSERT INTO Statement

INSERT INTO Statement:

The INSERT INTO statement is used to add new rows of data to a table in the database.


INSERT INTO table_name (column1, column2) VALUES (value1, value2);
    

Example:

This query inserts a new user into the 'users' table with the name 'Bob' and age '28'.


INSERT INTO users (name, age) VALUES ('Bob', 28);
    

Console Output:

INSERT 0 1

PostgreSQL Syntax: UPDATE Statement

UPDATE Statement:

The UPDATE statement is used to modify existing records in a table.


UPDATE table_name SET column1 = value1 WHERE condition;
    

Example:

This query updates the age of the user 'Alice' to '33' in the 'users' table.


UPDATE users SET age = 33 WHERE name = 'Alice';
    

Console Output:

UPDATE 1

PostgreSQL Syntax: DELETE Statement

DELETE Statement:

The DELETE statement is used to remove existing records from a table.


DELETE FROM table_name WHERE condition;
    

Example:

This query deletes the user 'Bob' from the 'users' table.


DELETE FROM users WHERE name = 'Bob';
    

Console Output:

DELETE 1

PostgreSQL Syntax: CREATE TABLE Statement

CREATE TABLE Statement:

The CREATE TABLE statement is used to create a new table in the database.


CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  PRIMARY KEY (column1)
);
    

Example:

This query creates a new table 'employees' with columns 'id' and 'name'.


CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100)
);
    

Console Output:

CREATE TABLE

PostgreSQL Syntax: ALTER TABLE Statement

ALTER TABLE Statement:

The ALTER TABLE statement is used to modify an existing table structure, such as adding or dropping columns.


ALTER TABLE table_name ADD column_name datatype;
    

Example:

This query adds a new column 'email' to the 'employees' table.


ALTER TABLE employees ADD email VARCHAR(255);
    

Console Output:

ALTER TABLE

PostgreSQL Syntax: DROP TABLE Statement

DROP TABLE Statement:

The DROP TABLE statement is used to remove a table and all its data from the database.


DROP TABLE table_name;
    

Example:

This query deletes the 'employees' table from the database.


DROP TABLE employees;
    

Console Output:

DROP TABLE

PostgreSQL Syntax: CREATE INDEX Statement

CREATE INDEX Statement:

The CREATE INDEX statement is used to create an index on a table for faster retrieval of records.


CREATE INDEX index_name ON table_name (column1, column2);
    

Example:

This query creates an index on the 'name' column of the 'users' table.


CREATE INDEX idx_name ON users (name);
    

Console Output:

CREATE INDEX

PostgreSQL Syntax: GRANT Statement

GRANT Statement:

The GRANT statement is used to provide specific privileges to users or roles on database objects.


GRANT privilege_name ON object_name TO user_name;
    

Example:

This query grants SELECT privilege on the 'users' table to the user 'john'.


GRANT SELECT ON users TO john;
    

Console Output:

GRANT

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025