A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-select/ below:

PostgreSQL - SELECT - GeeksforGeeks

PostgreSQL - SELECT

Last Updated : 12 Jul, 2025

PostgreSQL SELECT statement is an command for retrieving data from tables within a PostgreSQL database. It enables users to specify which columns to fetch and apply filters using the WHERE clause for targeted results.

In this article, We will learn about the PostgreSQL SELECT in detail by understanding various examples and so on.

PostgreSQL SELECT
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Explanation:

Examples of the SELECT Statement

Let’s consider a simple example. Assume we have a table called employees with the following structure:

id name age department salary 1 Alice 30 HR 60000 2 Bob 25 IT 75000 3 Charlie 35 Finance 80000 4 David 40 IT 95000 5 Eva 29 HR 50000 Example 1: Selecting All Columns

To retrieve all columns from the employees table, you can use the * wildcard:

SELECT * FROM employees;

Output:

id name age department salary 1 Alice 30 HR 60000 2 Bob 25 IT 75000 3 Charlie 35 Finance 80000 4 David 40 IT 95000 5 Eva 29 HR 50000 Example 2: Selecting Specific Columns

If we only want to retrieve specific columns, you can list them explicitly:

SELECT name, salary FROM employees;

Output:

name salary Alice 60000 Bob 75000 Charlie 80000 David 95000 Eva 50000 Example 3: Using the WHERE Clause

To filter results based on specific conditions, we can use the WHERE clause. For example, to retrieve employees in the IT department:

SELECT * FROM employees WHERE department = 'IT';

Output:

id name age department salary 2 Bob 25 IT 75000 4 David 40 IT 95000 Example 4: Using the ORDER BY Clause

We can sort the results using the ORDER BY clause. For example, to retrieve employees sorted by salary in descending order:

SELECT * FROM employees ORDER BY salary DESC;

Output:

id name age department salary 4 David 40 IT 95000 3 Charlie 35 Finance 80000 2 Bob 25 IT 75000 1 Alice 30 HR 60000 5 Eva 29 HR 50000 Example 5: Using the LIMIT Clause

To limit the number of rows returned, use the LIMIT clause. For instance, to retrieve only the top 3 highest-paid employees:

SELECT * FROM employees ORDER BY salary DESC LIMIT 3;

Output:

id name age department salary 4 David 40 IT 95000 3 Charlie 35 Finance 80000 2 Bob 25 IT 75000 Example 6: Using GROUP BY

The GROUP BY clause groups rows that have the same values in specified columns into summary rows. For example, to find the average salary by department:

SELECT department, AVG(salary) AS average_salary 
FROM employees
GROUP BY department;

Output:

department average_salary HR 55000 IT 85000 Finance 80000 Conclusion

In summary, understanding the PostgreSQL SELECT statement and its associated clauses is vital for efficient data retrieval and analysis. By utilizing features like WHERE, ORDER BY, LIMIT and GROUP BY users can perform complex queries and derive meaningful insights from their data. Mastering these concepts enhances the overall effectiveness of working with PostgreSQL.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4