Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Tuesday, 6 February 2024

PostgreSQL Query Language Basics

PostgreSQL Query Language (SQL) serves as a powerful tool for interacting with PostgreSQL databases. Below are the basics of writing SQL queries in PostgreSQL:


 1. SELECT Statement:


The SELECT statement is used to retrieve data from one or more tables.



SELECT column1, column2 FROM table_name;



You can also use `*` to select all columns:



SELECT * FROM table_name;



 2. WHERE Clause:


The WHERE clause is used to filter records based on specified conditions.



SELECT column1, column2 FROM table_name WHERE condition;



 3. ORDER BY Clause:


The ORDER BY clause is used to sort the result set in ascending or descending order.



SELECT column1, column2 FROM table_name ORDER BY column1 ASC;



 4. LIMIT and OFFSET:


LIMIT is used to limit the number of rows returned, while OFFSET is used to skip a specified number of rows before starting to return rows.



SELECT column1, column2 FROM table_name LIMIT 10 OFFSET 5;



 5. Aggregate Functions:


Aggregate functions perform calculations on a set of values and return a single value.



SELECT COUNT(*), AVG(column1), MAX(column2) FROM table_name;



 6. Group By:


The GROUP BY clause is used to group rows that have the same values into summary rows.



SELECT column1, COUNT(*)

FROM table_name

GROUP BY column1;



 7. HAVING Clause:


The HAVING clause is used to filter groups returned by the GROUP BY clause.



SELECT column1, COUNT(*)

FROM table_name

GROUP BY column1

HAVING COUNT(*) > 1;



 8. Joins:


Joins are used to combine rows from two or more tables based on a related column.



SELECT column1, column2

FROM table1

INNER JOIN table2 ON table1.column_id = table2.column_id;



 9. Subqueries:


A subquery is a query nested within another query.



SELECT column1, column2

FROM table1

WHERE column_id IN (SELECT column_id FROM table2 WHERE condition);



 10. 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,

    ...

);



 11. INSERT INTO Statement:


The INSERT INTO statement is used to insert new records into a table.



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



 12. UPDATE Statement:


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



UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;



 13. DELETE Statement:


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



DELETE FROM table_name WHERE condition;



These are the basic SQL commands in PostgreSQL. As you become more proficient with SQL, you can explore more advanced topics such as transactions, views, triggers, and stored procedures. Always refer to the PostgreSQL documentation for detailed information and best practices.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.