Chapter 8: PostgreSQL Select Data

PostgreSQL SELECT Data ☕🔍

You’ve already:

  • Created databases & tables (CREATE TABLE)
  • Put data inside (INSERT)
  • Now we learn how to ask questions and get data back — that’s what SELECT is all about.

This is the most used command in PostgreSQL (and in any relational database). Almost every app, report, dashboard, API — they all start with some form of SELECT.

1. What does SELECT actually do? (Teacher’s plain-English explanation)

SELECT tells PostgreSQL: “Go look in these table(s), apply my filters & calculations, and return me a result set — which is basically a temporary table of rows & columns that match what I asked for.”

  • It never changes the data in the database (read-only).
  • It can be super simple (SELECT * FROM students) or extremely complex (joins + windows + CTEs + JSON + aggregates).
  • PostgreSQL processes it in a very specific logical order (FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT).

Official PostgreSQL 18 docs (right now) start with:

SELECT retrieves rows from zero or more tables.

But in real life: SELECT is how you read data.

2. Basic syntax skeleton (the shapes you’ll see every day)

SQL

We’ll build from simplest → advanced.

3. Real example setup — using our college_db

Assume we have these tables with some data (from previous INSERT classes):

students

id first_name last_name email gpa date_of_birth is_active address (JSONB)
1 Aarav Patel aarav.patel@hyduni.edu 8.95 2003-07-14 true {“city”: “Hyderabad”, “pin”: “500081”}
2 Priya Reddy priya.reddy22@gmail.com 9.10 2002-11-03 true {“city”: “Secunderabad”}
3 Rahul Sharma rahul.sharma.2004@yahoo.com 7.85 2004-02-28 true null
4 Sneha Kumar sneha.kumar@outlook.com 8.60 2003-05-19 true {“city”: “Hyderabad”, “area”: “Gachibowli”}
5 Meera Iyer meera.iyer@gmail.com 9.75 null true {“city”: “Hyderabad”}

(We also have courses and enrollments — we’ll use them later.)

4. Level 1: Super basic SELECT (your first queries)

SQL

5. Level 2: Filtering with WHERE (the real power starts here)

SQL

6. Level 3: Sorting & limiting (ORDER BY + LIMIT / OFFSET)

SQL

7. Level 4: Removing duplicates (DISTINCT)

SQL

8. Level 5: Aggregates (COUNT, SUM, AVG, MIN, MAX)

SQL

9. Level 6: JOINs — combining tables (the relational magic)

Assume we have some enrollments:

SQL

Variants:

  • INNER JOIN = only matching rows (default JOIN)
  • LEFT JOIN = all students, even if no enrollment (shows NULL for course/grade)
  • RIGHT JOIN / FULL JOIN = less common but useful

10. Level 7: Quick look at advanced SELECT features in PostgreSQL 18 (2026)

  • Window functions (running totals, ranks)
SQL
  • Common Table Expressions (CTE / WITH)
SQL
  • LATERAL subqueries, JSON aggregation, etc. — we can go deeper later.

Summary cheat-sheet — SELECT building blocks

Clause Purpose Example feeling
SELECT What columns / calculations first_name, gpa * 10 AS percent
FROM Which table(s) students s JOIN enrollments e …
WHERE Filter rows gpa > 8.5 AND city = ‘Hyderabad’
GROUP BY Aggregate groups GROUP BY city
HAVING Filter after GROUP BY HAVING COUNT(*) > 1
ORDER BY Sort result ORDER BY gpa DESC
LIMIT / OFFSET Pagination / top-N LIMIT 10 OFFSET 20
DISTINCT Remove duplicates DISTINCT city

Your mini practice right now

  1. Run SELECT first_name, last_name, gpa FROM students WHERE gpa > 8.8 ORDER BY gpa DESC;
  2. Try the JSONB city filter
  3. Count how many students per city (use GROUP BY)
  4. In pgAdmin: use the Query Tool, highlight & F5 — see the nice grid!

Next class?

Tell me:

  • Want deeper JOINs (LEFT, self-join, many-to-many)?
  • Aggregates + GROUP BY + HAVING in detail?
  • Window functions (RANK, ROW_NUMBER, running total)?
  • Or UPDATE / DELETE to change/remove data?

Your guru is still here — what’s the next topic? 🚀

You may also like...

Leave a Reply

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