Outerbase SDK
  • Outerbase SDK
  • Get Started
    • Overview
    • Playground
  • Introspection
    • Overview
    • Universal data language
    • Connect a data source
    • Generate models
      • Limit table scope
    • Remote queries
  • Query Builder
    • Overview
    • Select
      • Single table
      • Multiple tables
      • Where clauses
      • Join tables
      • Limit & Offset
      • Order By
    • Insert
    • Update
    • Delete
    • Returning
    • Operators
    • .toString()
    • .asClass()
    • .query()
    • .queryRaw()
  • Data Sources
    • Overview
    • Outerbase
    • Cloudflare D1
    • Neon
Powered by GitBook
On this page
  1. Query Builder
  2. Select

Multiple tables

A common use case for SQL statements and database interactions requires the ability to fetch data from multiple tables in a single call. Let's take a look at how that's possible using the query builder.

Basic multi-table query

Retrieving data from more than one table is the same as it would be for a single table. The input property of the selectFrom function chain accepts an array, where each item in the array is a reference to a single table. You can add as many as you need to with this method.

const { data, error } = db
    .selectFrom([
        { table: 'table_1', columns: ['id'] },
        { table: 'table_2', columns: ['column_a'] }
    ])
    .leftJoin('table_1', equalsColumn('table_2.column_a', 'table_1.id'))
    .query()
PreviousSingle tableNextWhere clauses

Last updated 11 months ago