# WITH
## Definition(s)
### Context
#### Sub-context
>[! Definition]
>Creates a [[Temporary table]] in SQL
## Examples
```sql
WITH
longest_used_bike as (
SELECT
bike_id,
SUM(duration_minutes) AS trip_duration
FROM
bigquery-public-data.austin_bikeshare.bikeshare_trips
GROUP BY
bike_id
ORDER BY
trip_duration DESC
LIMIT 1)
/* The above sets up the table called `longest_used_bike` and
by design, it has one row, This is a key for when we want to JOIN the temp table
with the main dataset */
SELECT
trips.start_station_id,
count(*) AS trip_cnt
FROM longest_used_bike as longest
JOIN
bigquery-public-data.austin_bikeshare.bikeshare_trips AS trips
ON
trips.bike_id = longest.bike_id
GROUP BY
trips.start_station_id
ORDER BY
trip_cnt DESC;
/* the second section of the query then utilizes the output of the `longest_used_bike` table. Though not necessary, I think could be run as a subquery for this specific use case */
```
## Related
[[Temporary table]]
## Resources
## Flashcards
The below code are generated for use with [Spaced Repetition plugin](https://github.com/st3v3nmw/obsidian-spaced-repetition/) [docs](https://www.stephenmwangi.com/obsidian-spaced-repetition/)