PostgreSQL SELECT Statement with Outputs

1. Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column ASC|DESC
LIMIT number;

2. Suppose We Have This Table: employees

idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000.00
2Bob Smithbob.smith@example.com2021-08-2255000.00
3Carol Leecarol.lee@example.com2024-03-1070000.00
4David Kimdavid.kim@example.com2022-11-0565000.00
5Eve Turnereve.turner@example.com2023-06-3062000.00

3. Examples and Expected Output

a) Select All Columns for All Rows

SELECT * FROM employees;
idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000.00
2Bob Smithbob.smith@example.com2021-08-2255000.00
3Carol Leecarol.lee@example.com2024-03-1070000.00
4David Kimdavid.kim@example.com2022-11-0565000.00
5Eve Turnereve.turner@example.com2023-06-3062000.00

b) Select Specific Columns

SELECT name, email, salary FROM employees;
nameemailsalary
Alice Johnsonalice.johnson@example.com60000.00
Bob Smithbob.smith@example.com55000.00
Carol Leecarol.lee@example.com70000.00
David Kimdavid.kim@example.com65000.00
Eve Turnereve.turner@example.com62000.00

c) Select Rows with Condition

SELECT * FROM employees WHERE salary > 60000;
idnameemailhire_datesalary
3Carol Leecarol.lee@example.com2024-03-1070000.00
4David Kimdavid.kim@example.com2022-11-0565000.00
5Eve Turnereve.turner@example.com2023-06-3062000.00

d) Select and Order Results

SELECT * FROM employees ORDER BY hire_date DESC;
idnameemailhire_datesalary
3Carol Leecarol.lee@example.com2024-03-1070000.00
5Eve Turnereve.turner@example.com2023-06-3062000.00
1Alice Johnsonalice.johnson@example.com2023-01-1560000.00
4David Kimdavid.kim@example.com2022-11-0565000.00
2Bob Smithbob.smith@example.com2021-08-2255000.00

e) Select Limited Rows

SELECT * FROM employees LIMIT 3;
idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000.00
2Bob Smithbob.smith@example.com2021-08-2255000.00
3Carol Leecarol.lee@example.com2024-03-1070000.00

Reference: Official PostgreSQL Documentation: SELECT

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top