> For the complete documentation index, see [llms.txt](https://metis-montessori-lyceum.gitbook.io/sql-0/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://metis-montessori-lyceum.gitbook.io/sql-0/6-subqueries/untitled.md).

# Uitleg

Stel dat je de naam van het oudste medewerker wilt laten afdrukken, dan kan dat met een subquery.

Die gaat als volgt:

```sql
select Firstname, Lastname 
from Employee
where BirthDate = (
    select min(BirthDate) 
    from Employee);
```

Als deze query wordt uitgevoerd dan wordt eerst de subquery uitgevoer (de query tussen haakjes), dus:

```sql
select min(BirthDate) 
from Employee;
```

Als je die uitvoert verschijnt er: `1947-09-19 00:00:00`

Daarna wordt de hoofdquery uitgevoerd, en in plaats van de subquery wordt `1947-09-19 00:00:00` ingevuld.

Er staat dan dus eigenlijk:

```sql
select Firstname, Lastname 
from Employee
where BirthDate = "1947-09-19 00:00:00";
```

### Subquery met IN

Stel dat je al de tracknamen wilt die verkocht zijn.&#x20;

Dan kan dat door een join te gebruiken:

```sql
select * 
from InvoiceLine as I, Track as T 
where I.TrackID = T.TrackID;
```

> Let op dat hier met `AS` een afkorting maken voor de join!

Maar dit kan dus ook met een subquery:

```sql
Select *
from InvoiceLine
Where TrackID IN (
    select TrackID
    from Track);
```

Omdat er nu uit een lijstje gekozen worden moet je IN gebruiken, en geen =

**Je mag alleen = gebruiken als je er zeker van bent dat het resultaat van de subquery maar één exemplaar oplevert!**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://metis-montessori-lyceum.gitbook.io/sql-0/6-subqueries/untitled.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
