PostgreSQL Select Data

PostgreSQL Select Data (Beginner → Advanced)
In PostgreSQL Select Data is used to retrieve (read) data from one or more tables.
It is the most frequently used SQL command and the foundation of database querying.
What is SELECT in PostgreSQL?
SELECTis used to fetch data from database tables.
Select All Data
- Fetches all columns and all rows
- Avoid in large tables (performance issue)
Select Specific Columns (Recommended)
- Faster
- Cleaner output
- Best practice
SELECT with WHERE (Filtering Data)
Common Operators
| Operator | Meaning |
|---|---|
= | Equal |
!= / <> | Not equal |
> < | Greater / Less |
>= <= | Greater / Less equal |
BETWEEN | Range |
IN | Multiple values |
LIKE | Pattern matching |
SELECT with AND, OR, NOT
SELECT DISTINCT (Remove Duplicates)
- Returns unique values only
SELECT with ORDER BY
- Default is
ASC
SELECT with LIMIT and OFFSET
- Used for pagination
SELECT with Aggregate Functions
| Function | Purpose |
|---|---|
COUNT() | Count rows |
SUM() | Total |
AVG() | Average |
MIN() | Minimum |
MAX() | Maximum |
SELECT with GROUP BY
- Groups rows by column
- Used with aggregate functions
HAVING Clause (Filter Groups)
WHERE→ filters rowsHAVING→ filters groups
Column & Table Aliases (AS)
- Improves readability
ASkeyword is optional
Using LIKE for Pattern Matching
| Pattern | Meaning |
|---|---|
% | Any number of characters |
_ | Exactly one character |
Filtering Data with IN and BETWEEN Clauses
Understanding JOIN in SQL (Very Important Concept)
- Retrieves data from multiple tables
Order of Execution in PostgreSQL SELECT
- Frequently asked in interviews
Common Mistakes
- Using
WHEREafterGROUP BY ForgettingORDER BYbeforeLIMITUsing=instead ofLIKEMisusingHAVINGUsingSELECT *everywhere
Interview Questions (Must Prepare)
How does
SELECTwork in PostgreSQL?Difference between
WHEREandHAVINGSELECT DISTINCTvsGROUP BYExecution order of
SELECTHow pagination works using
LIMITandOFFSETHow to fetch data from multiple tables?
Summary
SELECTis used to read data- Supports filtering, sorting, grouping
- PostgreSQL supports
LIMIT&OFFSET Aggregates +GROUP BYare powerful- Joins allow multi-table queries
- Core skill for PostgreSQL, backend & interviews
