# SQL in KDB-X Quickstart

_This quickstart walks you through creating, inserting into, querying, and dropping a table in KDB-X using SQL. You will use basic Data Definition Language (DDL) and Data Manipulation Language (DML) commands._

## Before you begin

To follow this SQL quickstart, ensure you have the following ready:

- A running KDB‑X session
- The SQL interface (`s.k_`). The KDB-X installer installs it by default. To install or update it manually, refer to [Install and update SQL](install.md).
- Basic familiarity with SQL or q
- (Optional) KDB-X Python installed if you plan to run the Python equivalents of the SQL examples
- Access to a KDB‑X console or any IDE (for example, VS Code or Jupyter Notebook) that can run q commands.

This guide shows each operation in SQL, q, and Python to help you understand how the same action maps across interfaces:
 
- You can run SQL and q statements in the same q console.  
- To run the Python equivalents, load the KDB-X Python library by running [`import pykx as kx`](https://code.kx.com/pykx/getting-started/quickstart.html#1-import-kdb-x-python).

## Start a SQL-enabled session

Before running SQL queries, initialize SQL support in the current process using `.s.init[]`:
```q
q).s.init[]
```

## 1. Create a table

Use `CREATE TABLE` to define a new table in memory. The example below creates an empty table (`tripsFare`) with two columns: `vendor` (string) and `fare` (float).

> **Note — Running SQL in q**
>
>
> In KDB-X, SQL statements are executed from the q prompt using the `s)` SQL execution prompt.
>
=== "SQL"
    ```q
    s)CREATE TABLE tripsFare (vendor varchar, fare float)
    `tripsFare
    ```
=== "q"
    ```q
    // varchar -> symbol, float -> float
    tripsFare:([] vendor:`symbol$(); fare:`float$())
    ```
=== "Python"
    ```python
    tripsFare = kx.schema.builder({
        'vendor': kx.SymbolAtom,
        'fare': kx.FloatAtom,
    })
    ```
> **Note — Case-insensitive identifiers**
>
> SQL identifiers are case‑insensitive. Visit the [SQL Reference](reference.md#create-table) for details.
>
You can also create a more complex table (`cars`) with multiple data types and constraints:

=== "SQL"
    ```q
    // NOTE: Some SQL constraints are not enforced.
    // See SQL Reference → CREATE TABLE for details.
    s)CREATE TABLE cars (
      id int,
      Name varchar(250),
      Miles_per_Gallon numeric,
      Cylinders int,
      Displacement real,
      Horsepower int,
      Weight_in_lbs int NOT NULL,
      Acceleration numeric,
      Year date NOT NULL,
      Origin varchar(60)
    );
    ```
=== "q"
    ```q
    cars:([] 
      id:`int$();
      Name:`symbol$();
      Miles_per_Gallon:`float$();
      Cylinders:`int$();
      Displacement:`real$();
      Horsepower:`int$();
      Weight_in_lbs:`int$();
      Acceleration:`float$();
      Year:`date$();
      Origin:`symbol$()
      ) / make sure there is a whitespace before ')'
    ```
=== "Python"
    ```python
    cars = kx.Table(data={
        'id':               kx.IntVector([]),
        'Name':             kx.SymbolVector([]),
        'Miles_per_Gallon': kx.FloatVector([]),
        'Cylinders':        kx.IntVector([]),
        'Displacement':     kx.RealVector([]),
        'Horsepower':       kx.IntVector([]),
        'Weight_in_lbs':    kx.IntVector([]),
        'Acceleration':     kx.FloatVector([]),
        'Year':             kx.DateVector([]),
        'Origin':           kx.SymbolVector([]),
    })
    ```
> **Note — String columns**
>
> Columns declared as `varchar` map to q `symbol`. Visit the [SQL Reference](reference.md#data-type-conversions) for details.
>
## 2. Insert data

Use `INSERT` to add rows to an existing table. The following example inserts a single row into the `tripsFare` table.

=== "SQL"
    ```q
    / The column list (vendor, fare) is included for clarity, but it can be omitted only 
    / if values are provided for ALL table columns in the correct schema order.
    s)INSERT INTO tripsFare(vendor,fare) VALUES ('CMT',100)
    ```
=== "q"
    ```q
    // upsert a single row (symbol; float)
    `tripsFare insert (`CMT;100f)
    ```
=== "Python"
    ```python
    tripsFare.insert(['CMT', 100.])
    ```

You can also insert multiple rows at once:

=== "SQL"
    ```q
    s)INSERT INTO tripsFare(vendor,fare) VALUES ('DDS',301),('CMT',589)
    ```
=== "q"
    ```q
    `tripsFare insert (`DDS`CMT; 301 589f)
    ```
=== "Python"
    ```python
    tripsFare.insert([['DDS','CMT'], [301.0, 589.0]])
    ```

## 3. Query data

Use `SELECT` to retrieve rows. You can return all data or filter it with conditions.

To filter rows with a condition, run:

=== "SQL"
    ```q
    s)SELECT * FROM tripsFare WHERE fare > 20;
    vendor fare
    -----------
    CMT    100 
    DDS    301 
    CMT    589
    ```
=== "q"
    ```q
    select from tripsFare where fare > 20
    ```
=== "Python"
    ```python
    tripsFare.select(where=kx.Column('fare') > 20)
    ```

Aggregate data with `GROUP BY`. The following example calculates the total fare per vendor:

=== "SQL"
    ```q
    s)SELECT vendor, SUM(fare) AS totalfare FROM tripsFare GROUP BY vendor

    vendor totalfare
    ----------------
    CMT    689
    DDS    301
    ```
=== "q"
    ```q
    q)select totalfare:sum fare by vendor from tripsFare
    vendor totalfare
    ----------------
    CMT    689
    DDS    301
    ```
=== "Python"
    ```python
    >>> tripsFare.select(
    ...     columns=kx.Column('fare').sum().name('totalfare'),
    ...     by=kx.Column('vendor'),
    ... )
    pykx.KeyedTable(pykx.q('
    vendor| totalfare
    ------| ---------
    CMT   | 689
    DDS   | 301
    '))
    ```

You can also use other aggregate functions, such as `COUNT`:

=== "SQL"
    ```q
    s)SELECT COUNT(*) as farenr FROM tripsFare
    farenr
    ------
    3
    ```
=== "q"
    ```q
    q)select farenr:count i from tripsFare
    farenr
    ------
    3
    ```
=== "Python"
    ```python
    >>> tripsFare.select(
    ...     columns=kx.Column('i').count().name('farenr')
    ... )
    pykx.Table(pykx.q('
    farenr
    ------
    3
    '))
    ```

Join tables using a shared column to combine related data. The following example joins `tripsFare` with `cars` using a common `vendor` column.

=== "SQL"
    ```q
    s)SELECT vendor, fare, Name
    FROM tripsFare
    JOIN cars ON tripsFare.vendor = cars.Origin
    ```
=== "q"
    ```q
    // INNER join equivalent on tripsFare.vendor = cars.Origin
    // Make cars keyed on Origin so the join aligns on the key
    select vendor, fare, Name
    from tripsFare ij (`Origin xkey cars)
    ```
=== "Python"
    ```python
    tripsFare.merge(cars, left_on='vendor', right_on='Origin', how='inner')[['vendor', 'fare', 'Name']]
    ```

> **Note — About joins**
>
>
> This example uses a simple shared column for clarity. In practice, joins are typically performed on stable identifiers such as IDs or symbols.
>
>
> **Info — Querying on-disk data**
>
>
> SQL in KDB-X queries data stored both in memory and on disk, including memory-mapped historical databases (HDBs).
>
>
## 4. Drop a table

Use `DROP TABLE` to delete a table from memory. The example below drops the `tripsFare` table if it exists.

=== "SQL"
    ```q
    s)DROP TABLE tripsFare
    ```
=== "q"
    ```q
    delete tripsFare from `.
    ```
=== "Python"
    ```python
    del tripsFare
    ```

Congratulations! You have now created, populated, queried, and dropped tables in KDB-X using SQL.

## Next steps

- Learn more SQL operations in the [Reference](reference.md).  
- Explore joins, aggregates, and real-world examples in the [Examples](examples.md).
