PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - SELECT

The SELECT statement is foundational in SQL and PostgreSQL. It is used to query data from one or more tables in a database. The SELECT statement retrieves zero or more rows based on the criteria you specify.

Basic Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • column1, column2, ...: List of columns you want to retrieve. Use * to retrieve all columns from the table.

  • table_name: The name of the table from which to retrieve data.

  • WHERE condition: Optional. Specifies which rows to retrieve based on a condition.

Examples:

  1. Select all columns from a table:

    SELECT * FROM employees;
    
  2. Select specific columns:

    SELECT first_name, last_name FROM employees;
    
  3. Using the WHERE clause:

    Filter results based on specific criteria.

    SELECT first_name, last_name FROM employees WHERE department = 'HR';
    
  4. Distinct values:

    To get distinct values from a column:

    SELECT DISTINCT department FROM employees;
    
  5. Ordering the results:

    SELECT first_name, last_name FROM employees ORDER BY last_name ASC;
    

    The ORDER BY clause sorts the results based on the specified column(s). Use ASC for ascending (default) and DESC for descending order.

  6. Limiting the results:

    SELECT first_name, last_name FROM employees LIMIT 10;
    

    The LIMIT clause restricts the number of rows returned.

  7. Combining conditions:

    Using AND, OR, and NOT operators:

    SELECT first_name, last_name FROM employees WHERE department = 'HR' AND salary > 50000;
    
  8. Aggregation:

    Combine multiple rows of data into a single row based on common values:

    SELECT department, COUNT(*) FROM employees GROUP BY department;
    

    Here, the GROUP BY clause groups results by the department, and the COUNT(*) function counts the number of employees in each department.

Advanced Features:

PostgreSQL's SELECT also supports more advanced features like:

  • Joins: Combine rows from two or more tables based on related columns.

  • Subqueries: Embed one query within another.

  • Functions: Use built-in functions (like SUM, AVG, etc.) or user-defined functions.

  • Window functions: Perform calculations across a set of rows that are related to the current row.

  • Common Table Expressions (CTEs): Create temporary result sets for complex queries using the WITH clause.

The SELECT statement is at the heart of SQL. Its flexibility and power in PostgreSQL, combined with extensions and additional features, allow for retrieving data in virtually any format or arrangement you might need.

  1. How to use SELECT in PostgreSQL: The SELECT statement retrieves data from one or more tables.

    SELECT column1, column2
    FROM your_table;
    
  2. SELECTing specific columns in PostgreSQL:

    SELECT column1, column2
    FROM your_table;
    
  3. Using WHERE clause in PostgreSQL SELECT:

    SELECT column1, column2
    FROM your_table
    WHERE condition;
    
  4. Sorting results with ORDER BY in PostgreSQL SELECT:

    SELECT column1, column2
    FROM your_table
    ORDER BY column1 ASC, column2 DESC;
    
  5. Limiting results with LIMIT in PostgreSQL SELECT:

    SELECT column1, column2
    FROM your_table
    LIMIT 10;
    
  6. Skipping rows with OFFSET in PostgreSQL SELECT:

    SELECT column1, column2
    FROM your_table
    OFFSET 5;
    
  7. Filtering results with WHERE and LIKE in PostgreSQL SELECT:

    SELECT column1, column2
    FROM your_table
    WHERE column1 LIKE 'prefix%';
    
  8. JOIN operations in PostgreSQL SELECT:

    SELECT t1.column1, t2.column2
    FROM table1 t1
    INNER JOIN table2 t2 ON t1.id = t2.id;
    
  9. Aggregating data with GROUP BY in PostgreSQL SELECT:

    SELECT column1, COUNT(*)
    FROM your_table
    GROUP BY column1;
    
  10. HAVING clause in PostgreSQL SELECT:

    SELECT column1, COUNT(*)
    FROM your_table
    GROUP BY column1
    HAVING COUNT(*) > 10;
    
  11. Subqueries in PostgreSQL SELECT:

    SELECT column1, (SELECT AVG(column2) FROM another_table) AS avg_value
    FROM your_table;
    
  12. Using DISTINCT in PostgreSQL SELECT:

    SELECT DISTINCT column1
    FROM your_table;
    
  13. Combining multiple SELECT statements in PostgreSQL:

    SELECT column1 FROM table1
    UNION
    SELECT column1 FROM table2;
    
  14. Window functions in PostgreSQL SELECT:

    SELECT column1, column2, SUM(column2) OVER (PARTITION BY column1 ORDER BY column2) AS running_total
    FROM your_table;
    
  15. Common table expressions (CTE) in PostgreSQL SELECT:

    WITH your_cte AS (
       SELECT column1, column2
       FROM your_table
    )
    SELECT * FROM your_cte;