SQL Reference

This chapter covers how to use SQL to access, retrieve and manipulate data in DolphinDB. DolphinDB's SQL syntax is very similar to the ANSI SQL language in a Relational Database Management System (RDBMS) such as MySQL, Oracle, SQL Server, etc.

Syntax

select [top_clause] column_expressions
from table_name | table_expression
[where filtering_conditions]
[grouping_clause [having_clause] | order_clause]

Common features with ANSI SQL

  • Support select, insert, update and delete statement for retrieving, inserting, updating and deleting records in a table, respectively. Since 1.30.17/2.00.5, DolphinDB supports the create statement to create a database (table), and the alter statement to add columns to a table. Starting in version 1.30.22/2.00.10, all DolphinDB SQL keywords can be written in all-capital letters (e.g., SELECT, FROM, WHERE).

  • Support the where clause.

  • Support group by and order by clauses.

  • Support table join, including inner join, left join, left semijoin, full join.

  • Starting from version 2.00.10, line breaks are supported for SQL statements. Note that:

    • Keywords with multiple words (such as ORDER BY, GROUP BY, UNION ALL, INNER JOIN) cannot be split into two lines.

    • If an alias for a column or table is not specified with keyword as, it must follow the original name without a line break.

What are the differences?

  • Most functions can be directly called in SQL queries.

  • Other differences as listed below.

ANSI SQL syntax DolphinDB syntax Explanation
N/A context by context by is an innovation of DolphinDB. It simplifies processing time-series data withineach group. With group by, each group returns a scalar; with context by, each group returnsa vector of the same size as the group's records.
N/A pivot by pivot by transforms a vector into a matrix or table.
N/A cgroup by Perform cumulative grouping calculations
N/A map Execute the SQL statement on each partition separately, then merge the results.
N/A aj Asof join. It takes each record in the left table as a reference and checks if there isa match in the right table. If there is no match, the most recent observation will be chosen.If there are more than one match, the last one will be chosen.
N/A wj, pwj Window join and prevailing window join. They are a generalization of asof join. For each rowin the left table, window join applies aggregate functions on a window of rows in the right table.If the right table doesn't have a matching value for the window, prevailing window join willfill it with the last value before the window and then apply the aggregate functions.

Compatibility for SQL Dialects

Since version 1.30.22/2.00.10, DolphinDB has enhanced the compatibility for Oracle and MySQL dialects. In addition to supporting ANSI SQL syntax, DolphinDB deals with the inconsistent behaviors of functions with the same name due to dialect-specific features: You can select a dialect mode in a session and run scripts written in that dialect. Currently, three dialect modes are available: DolphinDB, Oracle and MySQL.

Note:

  • Scripts written in DolphinDB language can be correctly parsed in Oracle or MySQL dialect mode.

  • Only part of the functions or features of Oracle/MySQL are supported:

    SQL Dialect Features Functions (case insensitive)
    Oracle

    Comment symbols: --, /**/

    Concatenation operator:||

    asciistr, concat, decode, instr, nvl, nvl2, rank, regexp_like, to_char, to_date, to_number, and trunc

    Note: to_char only accepts numeric, DATE, DATEHOUR, and DATETIME types.

    For syntax references of Oracle SQL functions, see SQL Language Reference.

    MySQL sysdate

    For syntax references of MySQL functions, see MySQL :: MySQL 8.0 Reference Manual :: 12 Functions and Operators.