Open In App

PostgreSQL - SELECT

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • The SELECT statement is a PostgreSQL command used to fetch data from one or more tables in a database.
  • It allows us to specify which columns to retrieve, filter results using conditions, and sort the output in various ways.
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Explanation:

  • column1, column2, ...: The columns you want to retrieve from the table.
  • table_name: The name of the table from which to retrieve the data.
  • condition: Optional. A condition that must be met for the rows to be selected.

Examples of the SELECT Statement

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

idnameagedepartmentsalary
1Alice30HR60000
2Bob25IT75000
3Charlie35Finance80000
4David40IT95000
5Eva29HR50000

Example 1: Selecting All Columns

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

SELECT * FROM employees;

Output:

idnameagedepartmentsalary
1Alice30HR60000
2Bob25IT75000
3Charlie35Finance80000
4David40IT95000
5Eva29HR50000

Example 2: Selecting Specific Columns

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

SELECT name, salary FROM employees;

Output:

namesalary
Alice60000
Bob75000
Charlie80000
David95000
Eva50000

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:

idnameagedepartmentsalary
2Bob25IT75000
4David40IT95000

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:

idnameagedepartmentsalary
4David40IT95000
3Charlie35Finance80000
2Bob25IT75000
1Alice30HR60000
5Eva29HR50000

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:

idnameagedepartmentsalary
4David40IT95000
3Charlie35Finance80000
2Bob25IT75000

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:

departmentaverage_salary
HR55000
IT85000
Finance80000

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.


Next Article
Practice Tags :

Similar Reads