Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Database
  • Getting started
    • Overview
    • Create databases
    • Examples of YQL queries
    • Examples of operations in the YDB CLI
    • Launch a test app
    • Document API
    • Developing in NodeJS through the Document API
  • Step-by-step instructions
    • Database management
    • How to connect to a database
    • Table management
    • Reading and writing data
    • Working with secondary indexes
  • Working with the SDK
  • Concepts
    • Overview
    • Data model and schema
    • Serverless and Dedicated operation modes
    • Data types
    • Transactions
    • Secondary indexes
    • Time to Live (TTL)
    • Terms and definitions
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
    • Dedicated mode
  • Recommendations
    • Schema design
    • Partitioning tables
    • Secondary indexes
    • Paginated output
    • Loading large data volumes
    • Using timeouts
  • YDB API and API reference
    • Database limits
    • Handling errors in the API
  • Amazon DynamoDB-compatible HTTP API
    • API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
      • Common errors
  • YQL reference guide
    • Overview
    • Data types
      • Simple
      • Optional
      • Containers
      • Special
    • Syntax
      • Unsupported statements
      • For text representation of data types
      • Expressions
      • CREATE TABLE
      • DROP TABLE
      • INSERT INTO
      • UPSERT INTO
      • REPLACE INTO
      • UPDATE
      • DELETE
      • SELECT
      • GROUP BY
      • JOIN
      • FLATTEN
      • ACTION
      • DISCARD
      • PRAGMA
      • DECLARE
      • OVER, PARTITION BY, and WINDOW
    • Built-in functions
      • Basic
      • Aggregate
      • Window
      • For lists
      • For dictionaries
      • For JSON
      • For structures
      • For types
    • Preset user-defined functions
      • HyperScan
      • Pcre
      • Pire
      • Re2
      • String
      • Unicode
      • Datetime
      • Url
      • Ip
      • Digest
      • Math
      • Histogram
    • For text representation of data types
  • YQL tutorial
    • Overview
    • Creating a table
    • Adding data to a table
    • Selecting data from all columns
    • Selecting data from specific columns
    • Sorting and filtering
    • Data aggregation
    • Additional selection criteria
    • Joining tables by JOIN
    • Data insert and update by REPLACE
    • Data insert and update by UPSERT
    • Data insert by INSERT
    • Data update by UPDATE
    • Deleting data
    • Adding and deleting columns
    • Deleting a table
  • Maintenance
    • Backups
  • Diagnostics
    • System views
  • Questions and answers
    • General questions
    • Errors
    • YQL
    • All questions on the same page
  • Public materials
  1. YQL reference guide
  2. Syntax
  3. JOIN

JOIN

  • JOIN types

It combines the data from two sources (subqueries or tables) by equality of the values of the specified columns or expressions.

CROSS JOIN builds a Cartesian product: each element of the first data source is combined with each element of the second data source. The condition applied to the Cartesian product is set by various JOIN types. INNER JOIN is used by default.

Syntax

Columns for JOIN are specified using:

  • USING: If the column name is the same in all the merged tables. Suitable for simple queries.

    Example

    SELECT
      a.value, b.value
    FROM a_table AS a
    FULL JOIN b_table AS b
    USING (key);
    
  • ON: If you need to combine tables by columns with different names. Lets you build more complex queries, including the queries with the WHERE clause.

    Example

    SELECT
      a.value, b.value
    FROM a_table AS a
    FULL JOIN b_table AS b
    ON a.key == b.key;
    

Note

If the statement filters data in addition to JOIN, we recommend that you wrap the criteria that would return true for most of the rows, inside the LIKELY(...) function call. If your assumption that true values prevail for the criteria is correct, the query will run faster.

JOIN types

YQL provides the following logical JOIN types:

  • INNER (default): The result contains only rows where keys matched, the other rows are discarded.
  • FULL, LEFT and RIGHT: If the key is missing from both or one of the tables, the row is included in the result and filled by NULL values.
  • LEFT SEMI/RIGHT SEMI: One side of the query is a whitelist of keys, its values are not available. The result includes columns from one table only.
  • LEFT ONLY/RIGHT ONLY: Subtracting the sets by keys (blacklist). It's almost equivalent to adding IS NULL to the key on the opposite side in the regular LEFT/RIGHT JOIN, but with no access to values, like in SEMI JOIN.
  • CROSS: Cartesian product of two tables. Each row of the first table is combined with each row of the second table. ON/USING is not specified explicitly.
  • EXCLUSION: Both sides minus the intersection.

image

Note

NULL is a special value to denote nothing. Therefore, NULL values from two sources are not considered equal. This eliminates ambiguity and resource-intensive computing in some types of JOIN.

Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC