Lesson 19/100

Tutorials PostgreSQL Tutorial

Subqueries — Complete Guide

Subqueries — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.

On this page

PostgreSQL Tutorial · Lesson 19 of 100

Subqueries

SQLAdvanced

SQL · 1 — Queries · ~6 min · PostgreSQL — SQL & Queries

What is this?

A subquery is a SELECT nested inside another statement — in WHERE, FROM, or SELECT list. It can return one value, one row, or a table used like a derived table.

Why should you care?

Find Swiggy customers who ordered from restaurants with avg rating above 4.5 — filter customers WHERE id IN (subquery on restaurants).

See it live — copy this example

Run in pgAdmin or psql.

SELECT customer_id, email
FROM customers
WHERE customer_id IN (
  SELECT o.customer_id
  FROM orders o
  WHERE o.total_amount > (
    SELECT AVG(total_amount) FROM orders
  )
);

What happened?

  • Inner AVG computes mean order value.
  • Middle subquery lists customers above average.
  • Outer query returns their emails.
  • Three scopes nest cleanly.

Practice next

  1. Seed customers and orders with varied totals.
  2. Run SELECT AVG(total_amount) alone first.
  3. Run full query and verify only high spenders appear.
  4. Use EXISTS instead of IN for the same customer filter.
  5. Place subquery in FROM: SELECT * FROM (SELECT ...) s.

Remember

Subqueries answer questions in stages. IN, EXISTS, and comparison operators each fit different shapes. Often rewrite as JOIN for planner flexibility.

PostgresVerse VIP segment

CRM tags customers spending above average for a loyalty pilot.

Outcome: Campaign SQL lives in one query reproducible in pgAdmin.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of PostgreSQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using PostgreSQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a PostgreSQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Mid Detailed
Compare two approaches to Indexing—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Indexing…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a PostgreSQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

PostgreSQL Tutorial
Course syllabus

PostgreSQL Tutorial

PostgreSQL — Foundations
PostgreSQL — SQL & Queries
PostgreSQL — Indexing & Performance
PostgreSQL — Transactions & MVCC
PostgreSQL — Functions & Automation
PostgreSQL — JSONB & Modern Features
PostgreSQL — Replication & High Availability
PostgreSQL — Security & Cloud
PostgreSQL — Monitoring & Troubleshooting
PostgreSQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details