---
title: "SQL at Depth: Subqueries, Set Operations and Views"
description: "A query can use another query as a table, as a value, or as a test of existence, and set operations combine results that share a shape. A view names a query so the rest of the system can treat it as a"
canonical: https://lightmysky.com/learn/computing/sql-at-depth-subqueries-set-operations-and-views-mt_SLKUlC-7or
source: https://lightmysky.com/learn/computing/sql-at-depth-subqueries-set-operations-and-views-mt_SLKUlC-7or.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# SQL at Depth: Subqueries, Set Operations and Views

A query can use another query as a table, as a value, or as a test of existence, and set operations combine results that share a shape. A view names a query so the rest of the system can treat it as a table.

Subject: Computing · Area: Data & Databases · Ages 18 to 20
Page: https://lightmysky.com/learn/computing/sql-at-depth-subqueries-set-operations-and-views-mt_SLKUlC-7or

## Ready when they can

- Rewrite a correlated subquery as a join and say when the two differ
- Use exists and not exists to express a question about absence
- Define a view and explain what it does and does not store

## Lesson: Asking about rows that are missing

A query can nest inside another as a table, as a single value, or as an existence test. EXISTS asks whether matching rows occur, and NOT EXISTS asks whether none occur. Set words like UNION stack results that share the same shape.

**Example.** To list customers who never ordered, you write: SELECT name FROM Customers c WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.customer_id = c.id). The same ask runs a second way: LEFT JOIN Orders and keep rows where the order side is NULL.

A view names a query so later queries can treat it as a table. It stores the query text, not the rows, so each use runs fresh against live data. That keeps logic in one place without copying data.

**Tip.** A join and a subquery can differ. Joins multiply rows on many matches and NOT IN breaks on NULLs, while NOT EXISTS stays steady. Pick the form whose edge behavior you can defend.

**Recap.** Test absence with NOT EXISTS, stack shapes with set words, and name repeats with views.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [SQL: Selecting, Filtering and Ordering Rows](https://lightmysky.com/learn/computing/sql-selecting-filtering-and-ordering-rows-mt_lDVVG2AKzs)
- [Functional Dependencies and Boyce-Codd Normal Form](https://lightmysky.com/learn/computing/functional-dependencies-and-boyce-codd-normal-form-mt_PfWLq1aEQz)

## Opens up

- [Window Functions and Running Calculations](https://lightmysky.com/learn/computing/window-functions-and-running-calculations-mt_aaVxFPjWQv)
