# Execute Query Example

> Learn how to run SQL queries with API Maker’s executeQuery function, including DDL, DML, DQL, and joins.

Source: https://docs.apimaker.dev/v4/examples/sys/system/executeQuery.html

> **The code in every language**
>
> Each example is the inside of `main`, written in every [language](https://docs.apimaker.dev/v4/docs/languages/index.html) API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (`GetAll`, `DeepItem`…) are those of `am` : `from am import GetAll` in Python, `use am::GetAll;` in Rust, `import am.GetAll;` in Java and Kotlin ; PHP and C# have them without an import, Go names them `am.GetAll`.

`g.sys.system.executeQuery` calls the [execute plain query system API](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-execute-plain-query-api.html) from your code, whatever the settings of that API say about HTTP callers.

### Execute query System API

Execute database query using global object 'g'.

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: 'INSTANCE_NAME',
    database: 'DATABASE_NAME',
    collection: 'COLLECTION_NAME',
    query: "select * from DATABASE_NAME.COLLECTION_NAME"
});
```

**Python**

```python
await g.sys.system.execute_query(
    'INSTANCE_NAME',
    'select * from DATABASE_NAME.COLLECTION_NAME',
    database='DATABASE_NAME',
    collection='COLLECTION_NAME',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'INSTANCE_NAME',
    'select * from DATABASE_NAME.COLLECTION_NAME',
    database: 'DATABASE_NAME',
    collection: 'COLLECTION_NAME',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("INSTANCE_NAME", "select * from DATABASE_NAME.COLLECTION_NAME")
{
    Database = "DATABASE_NAME",
    Collection = "COLLECTION_NAME",
});
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance:   "INSTANCE_NAME",
	Database:   "DATABASE_NAME",
	Collection: "COLLECTION_NAME",
	Query:      "select * from DATABASE_NAME.COLLECTION_NAME",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(
    ExecuteQuery::new("INSTANCE_NAME", "select * from DATABASE_NAME.COLLECTION_NAME")
        .database("DATABASE_NAME")
        .collection("COLLECTION_NAME"),
)?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("INSTANCE_NAME", "select * from DATABASE_NAME.COLLECTION_NAME")
    .database("DATABASE_NAME")
    .collection("COLLECTION_NAME"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(
    ExecuteQuery("INSTANCE_NAME", "select * from DATABASE_NAME.COLLECTION_NAME")
        .database("DATABASE_NAME")
        .collection("COLLECTION_NAME"),
)
```

- If the database is Mongodb API Maker supports all the commands listed in below.
- https://www.mongodb.com/docs/drivers/node/current/usage-examples/command/
- https://www.mongodb.com/docs/v6.0/reference/command/

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: 'INSTANCE_NAME',
    database: 'DATABASE_NAME',
    query: {
        dbStats: 1,
    }
});
```

**Python**

```python
await g.sys.system.execute_query(
    'INSTANCE_NAME',
    {
        'dbStats': 1,
    },
    database='DATABASE_NAME',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'INSTANCE_NAME',
    [
        'dbStats' => 1,
    ],
    database: 'DATABASE_NAME',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("INSTANCE_NAME", new JsonObject
{
    ["dbStats"] = 1,
})
{
    Database = "DATABASE_NAME",
});
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "INSTANCE_NAME",
	Database: "DATABASE_NAME",
	Query: am.M{
		"dbStats": 1,
	},
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(
    ExecuteQuery::new("INSTANCE_NAME", json!({
        "dbStats": 1,
    }))
        .database("DATABASE_NAME"),
)?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("INSTANCE_NAME", Map.of(
    "dbStats", 1
))
    .database("DATABASE_NAME"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(
    ExecuteQuery("INSTANCE_NAME", mapOf(
        "dbStats" to 1,
    ))
        .database("DATABASE_NAME"),
)
```

## How we use Native Query in API Maker.

### Native query using secret management  
- Set Database constrain in Secret management.
```ts
import * as T from 'types';

let Secret: T.ISecretType | any = {
// Place your keys here in json format.
    common: <T.ISecretTypeCommon>{
        dbConstrain: {
            instance: 'mysql',
            database: 'inventory',
            collection: 'employees'
        }
    }
};
module.exports = Secret;
```

- Read it from the secret, and create the table it names.

**TypeScript**

```typescript
const dbConstrain = await g.sys.system.getSecret('common.dbConstrain');
const instance = dbConstrain?.instance ?? 'mysql';
const database = dbConstrain?.database ?? 'inventory';
const collection = dbConstrain?.collection ?? 'employees';

await g.sys.system.executeQuery({
    instance,
    query: `CREATE TABLE ${database}.${collection}(emp_id int AUTO_INCREMENT PRIMARY KEY);`
});
```

**Python**

```python
db_constrain = await g.sys.system.get_secret('common.dbConstrain') or {}
instance = db_constrain.get('instance') or 'mysql'
database = db_constrain.get('database') or 'inventory'
collection = db_constrain.get('collection') or 'employees'

await g.sys.system.execute_query(instance, f'CREATE TABLE {database}.{collection}(emp_id int AUTO_INCREMENT PRIMARY KEY);')
```

**PHP**

```php
$dbConstrain = $g->sys->system->getSecret('common.dbConstrain');
$instance = $dbConstrain->instance ?? 'mysql';
$database = $dbConstrain->database ?? 'inventory';
$collection = $dbConstrain->collection ?? 'employees';

$g->sys->system->executeQuery($instance, "CREATE TABLE {$database}.{$collection}(emp_id int AUTO_INCREMENT PRIMARY KEY);");
```

**C#**

```csharp
var dbConstrain = await g.Sys.System.GetSecret("common.dbConstrain");
var instance = dbConstrain.GetString("instance", "mysql");
var database = dbConstrain.GetString("database", "inventory");
var collection = dbConstrain.GetString("collection", "employees");

await g.Sys.System.ExecuteQuery(new ExecuteQuery(instance, $"CREATE TABLE {database}.{collection}(emp_id int AUTO_INCREMENT PRIMARY KEY);"));
```

**Go**

```go
secret, err := g.Sys.System.GetSecret("common.dbConstrain")
if err != nil {
	return nil, err
}
dbConstrain := am.AsM(secret)
instance, database, collection := dbConstrain.String("instance"), dbConstrain.String("database"), dbConstrain.String("collection")
if instance == "" {
	instance = "mysql"
}
if database == "" {
	database = "inventory"
}
if collection == "" {
	collection = "employees"
}

if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: instance,
	Query:    "CREATE TABLE " + database + "." + collection + "(emp_id int AUTO_INCREMENT PRIMARY KEY);",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
let db_constrain = g.sys.system.get_secret("common.dbConstrain", None)?;
let instance = db_constrain["instance"].as_str().unwrap_or("mysql");
let database = db_constrain["database"].as_str().unwrap_or("inventory");
let collection = db_constrain["collection"].as_str().unwrap_or("employees");

g.sys.system.execute_query(ExecuteQuery::new(instance, format!("CREATE TABLE {database}.{collection}(emp_id int AUTO_INCREMENT PRIMARY KEY);")))?;
```

**Java**

```java
JsonObject dbConstrain = JsonObject.from(g.sys.system.getSecret("common.dbConstrain"));
String instance = dbConstrain.getString("instance", "mysql");
String database = dbConstrain.getString("database", "inventory");
String collection = dbConstrain.getString("collection", "employees");

g.sys.system.executeQuery(new ExecuteQuery(instance, "CREATE TABLE " + database + "." + collection + "(emp_id int AUTO_INCREMENT PRIMARY KEY);"));
```

**Kotlin**

```kotlin
val dbConstrain = JsonObject.from(g.sys.system.getSecret("common.dbConstrain"))
val instance = dbConstrain.getString("instance", "mysql")
val database = dbConstrain.getString("database", "inventory")
val collection = dbConstrain.getString("collection", "employees")

g.sys.system.executeQuery(ExecuteQuery(instance, "CREATE TABLE $database.$collection(emp_id int AUTO_INCREMENT PRIMARY KEY);"))
```

### Various types of Database & It`s Native Query In API Maker

- MySQL | TiDB | Percona XtraDB

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT * FROM inventory.employees;"
});
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT * FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT * FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT * FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT * FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"))
```

- MariaDB

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mariadb",
    query: "SELECT * FROM inventory.employees;"
});
```

**Python**

```python
await g.sys.system.execute_query(
    'mariadb',
    'SELECT * FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mariadb',
    'SELECT * FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mariadb", "SELECT * FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mariadb",
	Query:    "SELECT * FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mariadb", "SELECT * FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mariadb", "SELECT * FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mariadb", "SELECT * FROM inventory.employees;"))
```

- Microsoft SQL Server

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "sqlserver",
    query: "SELECT * FROM inventory.dbo.employees;"
});
```

**Python**

```python
await g.sys.system.execute_query(
    'sqlserver',
    'SELECT * FROM inventory.dbo.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'sqlserver',
    'SELECT * FROM inventory.dbo.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("sqlserver", "SELECT * FROM inventory.dbo.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "sqlserver",
	Query:    "SELECT * FROM inventory.dbo.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("sqlserver", "SELECT * FROM inventory.dbo.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("sqlserver", "SELECT * FROM inventory.dbo.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("sqlserver", "SELECT * FROM inventory.dbo.employees;"))
```

- MongoDB

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: 'mongodb',
    database: 'inventory',
    query: {
        dbStats: 1,
    }
});
```

**Python**

```python
await g.sys.system.execute_query(
    'mongodb',
    {
        'dbStats': 1,
    },
    database='inventory',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mongodb',
    [
        'dbStats' => 1,
    ],
    database: 'inventory',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mongodb", new JsonObject
{
    ["dbStats"] = 1,
})
{
    Database = "inventory",
});
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mongodb",
	Database: "inventory",
	Query: am.M{
		"dbStats": 1,
	},
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(
    ExecuteQuery::new("mongodb", json!({
        "dbStats": 1,
    }))
        .database("inventory"),
)?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mongodb", Map.of(
    "dbStats", 1
))
    .database("inventory"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(
    ExecuteQuery("mongodb", mapOf(
        "dbStats" to 1,
    ))
        .database("inventory"),
)
```

- PostgreSQL

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "postgresql",
    query: `drop database if exists e_commerce;`
});

await g.sys.system.executeQuery({
    instance: "postgresql",
    query: `create database e_commerce;`
});

await g.sys.system.executeQuery({
    instance: "postgresql",
    database: "e_commerce", // 👈 Need to provide db name
    query: `
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
        `
});

```

**Python**

```python
await g.sys.system.execute_query(
    'postgresql',
    'drop database if exists e_commerce;',
)

await g.sys.system.execute_query(
    'postgresql',
    'create database e_commerce;',
)

await g.sys.system.execute_query(
    'postgresql',
    """
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
        """,
    database='e_commerce',  # 👈 Need to provide db name
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'postgresql',
    'drop database if exists e_commerce;',
);

$g->sys->system->executeQuery(
    'postgresql',
    'create database e_commerce;',
);

$g->sys->system->executeQuery(
    'postgresql',
    <<<'TEXT'
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
TEXT,
    database: 'e_commerce', // 👈 Need to provide db name
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("postgresql", "drop database if exists e_commerce;"));

await g.Sys.System.ExecuteQuery(new ExecuteQuery("postgresql", "create database e_commerce;"));

await g.Sys.System.ExecuteQuery(new ExecuteQuery("postgresql", """
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
""")
{
    Database = "e_commerce", // 👈 Need to provide db name
});
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "postgresql",
	Query:    "drop database if exists e_commerce;",
}); err != nil {
	return nil, err
}

if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "postgresql",
	Query:    "create database e_commerce;",
}); err != nil {
	return nil, err
}

if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "postgresql",
	Database: "e_commerce", // 👈 Need to provide db name
	Query: `
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
        `,
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("postgresql", "drop database if exists e_commerce;"))?;

g.sys.system.execute_query(ExecuteQuery::new("postgresql", "create database e_commerce;"))?;

g.sys.system.execute_query(
    ExecuteQuery::new("postgresql", r#"
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
        "#)
        .database("e_commerce"), // 👈 Need to provide db name
)?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("postgresql", "drop database if exists e_commerce;"));

g.sys.system.executeQuery(new ExecuteQuery("postgresql", "create database e_commerce;"));

g.sys.system.executeQuery(new ExecuteQuery("postgresql", """
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
""")
    .database("e_commerce")); // 👈 Need to provide db name
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("postgresql", "drop database if exists e_commerce;"))

g.sys.system.executeQuery(ExecuteQuery("postgresql", "create database e_commerce;"))

g.sys.system.executeQuery(
    ExecuteQuery("postgresql", """
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers"
(
    "customer_id" int2                                       NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 32767 START 1 ),
    "first_name"  varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "last_name"   varchar(45) COLLATE "pg_catalog"."default" NOT NULL,
    "phone"       int8                                       NOT NULL,
    "last_update" timestamp(6)                               NOT NULL,
    "pincode" int NOT NULL,
    "shipping_id" int NULL,
    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);
CREATE INDEX "idx_actor_last_name" ON "public"."customers" USING btree ("last_name" COLLATE "pg_catalog"."default" "pg_catalog"."text_ops" ASC NULLS LAST);

INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('PENELOPE', 'GUINESS', 123456789, '2006-02-15 04:34:33', 100050, 1);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('NICK', 'WAHLBERG', 1123456789, '2006-02-15 04:34:33', 111111, 2);
INSERT INTO "public"."customers" ("first_name", "last_name", "phone", "last_update", "pincode", "shipping_id") VALUES ('ED', 'CHASE', 2123456789, '2006-02-15 04:34:33', 222222, 3);
        """)
        .database("e_commerce"), // 👈 Need to provide db name
)
```

- Oracle

> Do not add a **semicolon (;)** at the end of a query or a statement of Oracle.

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "oracle",
    query: `SELECT * FROM "INVENTORY"."employees"`
});
```

**Python**

```python
await g.sys.system.execute_query(
    'oracle',
    'SELECT * FROM "INVENTORY"."employees"',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'oracle',
    'SELECT * FROM "INVENTORY"."employees"',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("oracle", "SELECT * FROM \"INVENTORY\".\"employees\""));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "oracle",
	Query:    "SELECT * FROM \"INVENTORY\".\"employees\"",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("oracle", "SELECT * FROM \"INVENTORY\".\"employees\""))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("oracle", "SELECT * FROM \"INVENTORY\".\"employees\""));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("oracle", "SELECT * FROM \"INVENTORY\".\"employees\""))
```

### DDL Operation in API Maker

- CREATE TABLE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "CREATE TABLE inventory.employees(id int AUTO_INCREMENT PRIMARY KEY);"))
```

- ALTER TABLE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "ALTER TABLE inventory.employees ADD COLUMN emp_name VARCHAR(30) NOT NULL;"))
```

- RENAME TABLE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "ALTER TABLE inventory.employees RENAME COLUMN id TO emp_id;"))
```

- TRUNCATE TABLE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "TRUNCATE TABLE inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'TRUNCATE TABLE inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'TRUNCATE TABLE inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "TRUNCATE TABLE inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "TRUNCATE TABLE inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "TRUNCATE TABLE inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "TRUNCATE TABLE inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "TRUNCATE TABLE inventory.employees;"))
```

- DROP TABLE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "DROP TABLE IF EXISTS inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'DROP TABLE IF EXISTS inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'DROP TABLE IF EXISTS inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "DROP TABLE IF EXISTS inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "DROP TABLE IF EXISTS inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "DROP TABLE IF EXISTS inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "DROP TABLE IF EXISTS inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "DROP TABLE IF EXISTS inventory.employees;"))
```

### DML Operation in API Maker
API Maker allows us to perform various DML operations, such as the following examples.

- INSERT

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "INSERT into inventory.employees Values(101, 'Bob', 'M');"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    "INSERT into inventory.employees Values(101, 'Bob', 'M');",
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'INSERT into inventory.employees Values(101, \'Bob\', \'M\');',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "INSERT into inventory.employees Values(101, 'Bob', 'M');"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "INSERT into inventory.employees Values(101, 'Bob', 'M');",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "INSERT into inventory.employees Values(101, 'Bob', 'M');"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "INSERT into inventory.employees Values(101, 'Bob', 'M');"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "INSERT into inventory.employees Values(101, 'Bob', 'M');"))
```

- DELETE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "DELETE FROM inventory.employees WHERE emp_Id = '101';"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    "DELETE FROM inventory.employees WHERE emp_Id = '101';",
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'DELETE FROM inventory.employees WHERE emp_Id = \'101\';',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "DELETE FROM inventory.employees WHERE emp_Id = '101';"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "DELETE FROM inventory.employees WHERE emp_Id = '101';",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "DELETE FROM inventory.employees WHERE emp_Id = '101';"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "DELETE FROM inventory.employees WHERE emp_Id = '101';"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "DELETE FROM inventory.employees WHERE emp_Id = '101';"))
```

- UPDATE

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';",
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'UPDATE inventory.employees SET emp_Name = \'Alice\' WHERE emp_Id = \'101\';',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "UPDATE inventory.employees SET emp_Name = 'Alice' WHERE emp_Id = '101';"))
```

### DQL Operation in API Maker
API Maker allows us to perform various DQL operations, such as the following examples.

- SELECT

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT * FROM inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT * FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT * FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT * FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT * FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT * FROM inventory.employees;"))
```

- DISTINCT

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT DISTINCT emp_Name FROM inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT DISTINCT emp_Name FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT DISTINCT emp_Name FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT DISTINCT emp_Name FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT DISTINCT emp_Name FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT DISTINCT emp_Name FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT DISTINCT emp_Name FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT DISTINCT emp_Name FROM inventory.employees;"))
```

- ORDER BY

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT * FROM inventory.employees order by emp_Gen,emp_Name;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT * FROM inventory.employees order by emp_Gen,emp_Name;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT * FROM inventory.employees order by emp_Gen,emp_Name;"))
```

### Aggregated Function in API Maker
API Maker allows us to perform various Aggregated Function, such as the following examples.

- SUM

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT SUM(emp_Sal) as Total_Salary FROM inventory.employees;"))
```

- AVG

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT AVG(emp_Sal) as Average_Salary FROM inventory.employees;"))
```

### Joins in API Maker
API Maker allows us to perform Joins, such as the following examples.

- INNER JOIN

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT * FROM inventory.employees INNER JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"))
```

- LEFT JOIN

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "mysql",
    query: "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"
})
```

**Python**

```python
await g.sys.system.execute_query(
    'mysql',
    'SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'mysql',
    'SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mysql",
	Query:    "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("mysql", "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("mysql", "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("mysql", "SELECT * FROM inventory.employees LEFT JOIN inventory.attandance ON inventory.employees.emp_Id = inventory.attandance.emp_id;"))
```

### How to call SQL Server Store Procedure

- Just replace your procedure name with below "procedureName".

**TypeScript**

```typescript
await g.sys.system.executeQuery({
    instance: "sql_server",
    query: "USE inventory; exec dbo.procedureName;"
});
```

**Python**

```python
await g.sys.system.execute_query(
    'sql_server',
    'USE inventory; exec dbo.procedureName;',
)
```

**PHP**

```php
$g->sys->system->executeQuery(
    'sql_server',
    'USE inventory; exec dbo.procedureName;',
);
```

**C#**

```csharp
await g.Sys.System.ExecuteQuery(new ExecuteQuery("sql_server", "USE inventory; exec dbo.procedureName;"));
```

**Go**

```go
if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "sql_server",
	Query:    "USE inventory; exec dbo.procedureName;",
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
g.sys.system.execute_query(ExecuteQuery::new("sql_server", "USE inventory; exec dbo.procedureName;"))?;
```

**Java**

```java
g.sys.system.executeQuery(new ExecuteQuery("sql_server", "USE inventory; exec dbo.procedureName;"));
```

**Kotlin**

```kotlin
g.sys.system.executeQuery(ExecuteQuery("sql_server", "USE inventory; exec dbo.procedureName;"))
```

### Commands of MongoDB : indexes

A command of MongoDB is an object : `listIndexes`, `createIndexes`, `dropIndexes`… [createIndexes](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-create-indexes-api.html#from-code) and [getIndexes](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-get-indexes-api.html#from-code) do the same on every database, in the same words.

**TypeScript**

```typescript
const listed = await g.sys.system.executeQuery({
    instance: 'mongodb', database: 'shop',
    query: { listIndexes: 'products' },
});

await g.sys.system.executeQuery({
    instance: 'mongodb', database: 'shop',
    query: {
        createIndexes: 'products',
        indexes: [{ key: { category: 1, productName: 1 }, name: 'category_productName', unique: true }],
    },
});
```

**Python**

```python
listed = await g.sys.system.execute_query(
    'mongodb',
    {'listIndexes': 'products'},
    database='shop',
)

await g.sys.system.execute_query(
    'mongodb',
    {
        'createIndexes': 'products',
        'indexes': [{'key': {'category': 1, 'productName': 1}, 'name': 'category_productName', 'unique': True}],
    },
    database='shop',
)
```

**PHP**

```php
$listed = $g->sys->system->executeQuery(
    'mongodb',
    ['listIndexes' => 'products'],
    database: 'shop',
);

$g->sys->system->executeQuery(
    'mongodb',
    [
        'createIndexes' => 'products',
        'indexes' => [['key' => ['category' => 1, 'productName' => 1], 'name' => 'category_productName', 'unique' => true]],
    ],
    database: 'shop',
);
```

**C#**

```csharp
var listed = await g.Sys.System.ExecuteQuery(new ExecuteQuery("mongodb", new JsonObject { ["listIndexes"] = "products" })
{
    Database = "shop",
});

await g.Sys.System.ExecuteQuery(new ExecuteQuery("mongodb", new JsonObject
{
    ["createIndexes"] = "products",
    ["indexes"] = new JsonArray { new JsonObject { ["key"] = new JsonObject { ["category"] = 1, ["productName"] = 1 }, ["name"] = "category_productName", ["unique"] = true } },
})
{
    Database = "shop",
});
```

**Go**

```go
listed, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mongodb", Database: "shop",
	Query: am.M{"listIndexes": "products"},
})
if err != nil {
	return nil, err
}

if _, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "mongodb", Database: "shop",
	Query: am.M{
		"createIndexes": "products",
		"indexes":       []any{am.M{"key": am.M{"category": 1, "productName": 1}, "name": "category_productName", "unique": true}},
	},
}); err != nil {
	return nil, err
}
```

**Rust**

```rust
let listed = g.sys.system.execute_query(
    ExecuteQuery::new("mongodb", json!({ "listIndexes": "products" }))
        .database("shop"),
)?;

g.sys.system.execute_query(
    ExecuteQuery::new("mongodb", json!({
        "createIndexes": "products",
        "indexes": [{ "key": { "category": 1, "productName": 1 }, "name": "category_productName", "unique": true }],
    }))
        .database("shop"),
)?;
```

**Java**

```java
var listed = g.sys.system.executeQuery(new ExecuteQuery("mongodb", Map.of("listIndexes", "products"))
    .database("shop"));

g.sys.system.executeQuery(new ExecuteQuery("mongodb", Map.of(
    "createIndexes", "products",
    "indexes", List.of(Map.of("key", Map.of("category", 1, "productName", 1), "name", "category_productName", "unique", true))
))
    .database("shop"));
```

**Kotlin**

```kotlin
val listed = g.sys.system.executeQuery(
    ExecuteQuery("mongodb", mapOf("listIndexes" to "products"))
        .database("shop"),
)

g.sys.system.executeQuery(
    ExecuteQuery("mongodb", mapOf(
        "createIndexes" to "products",
        "indexes" to listOf(mapOf("key" to mapOf("category" to 1, "productName" to 1), "name" to "category_productName", "unique" to true)),
    ))
        .database("shop"),
)
```

## Multi-tenant

- `instance: "crm::acme"` runs the query in the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v4/docs/features/multi-tenant.html) instance `crm`.
- In a custom API called for a tenant, `instance: "crm"` runs it in the database of that tenant: the [x-am-tenant-username](https://docs.apimaker.dev/v4/docs/apis-all/header/requestHeader.html#x-am-tenant-username) header is passed on. Without a tenant, it runs in the structure database.

**TypeScript**

```typescript
// The database of acme.
const ordersOfAcme = await g.sys.system.executeQuery({
    instance: 'crm::acme',
    database: 'crm',
    query: "select status, count(*) as orders from orders group by status"
});

// The database of the tenant of the request.
const customers = await g.sys.system.executeQuery({
    instance: 'crm',
    database: 'crm',
    query: "select count(*) as customers from customers"
});
```

**Python**

```python
# The database of acme.
orders_of_acme = await g.sys.system.execute_query(
    'crm::acme',
    'select status, count(*) as orders from orders group by status',
    database='crm',
)

# The database of the tenant of the request.
customers = await g.sys.system.execute_query(
    'crm',
    'select count(*) as customers from customers',
    database='crm',
)
```

**PHP**

```php
// The database of acme.
$ordersOfAcme = $g->sys->system->executeQuery(
    'crm::acme',
    'select status, count(*) as orders from orders group by status',
    database: 'crm',
);

// The database of the tenant of the request.
$customers = $g->sys->system->executeQuery(
    'crm',
    'select count(*) as customers from customers',
    database: 'crm',
);
```

**C#**

```csharp
// The database of acme.
var ordersOfAcme = await g.Sys.System.ExecuteQuery(new ExecuteQuery("crm::acme", "select status, count(*) as orders from orders group by status")
{
    Database = "crm",
});

// The database of the tenant of the request.
var customers = await g.Sys.System.ExecuteQuery(new ExecuteQuery("crm", "select count(*) as customers from customers")
{
    Database = "crm",
});
```

**Go**

```go
// The database of acme.
ordersOfAcme, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "crm::acme",
	Database: "crm",
	Query:    "select status, count(*) as orders from orders group by status",
})
if err != nil {
	return nil, err
}

// The database of the tenant of the request.
customers, err := g.Sys.System.ExecuteQuery(am.ExecuteQuery{
	Instance: "crm",
	Database: "crm",
	Query:    "select count(*) as customers from customers",
})
if err != nil {
	return nil, err
}
```

**Rust**

```rust
// The database of acme.
let orders_of_acme = g.sys.system.execute_query(
    ExecuteQuery::new("crm::acme", "select status, count(*) as orders from orders group by status")
        .database("crm"),
)?;

// The database of the tenant of the request.
let customers = g.sys.system.execute_query(
    ExecuteQuery::new("crm", "select count(*) as customers from customers")
        .database("crm"),
)?;
```

**Java**

```java
// The database of acme.
var ordersOfAcme = g.sys.system.executeQuery(new ExecuteQuery("crm::acme", "select status, count(*) as orders from orders group by status")
    .database("crm"));

// The database of the tenant of the request.
var customers = g.sys.system.executeQuery(new ExecuteQuery("crm", "select count(*) as customers from customers")
    .database("crm"));
```

**Kotlin**

```kotlin
// The database of acme.
val ordersOfAcme = g.sys.system.executeQuery(
    ExecuteQuery("crm::acme", "select status, count(*) as orders from orders group by status")
        .database("crm"),
)

// The database of the tenant of the request.
val customers = g.sys.system.executeQuery(
    ExecuteQuery("crm", "select count(*) as customers from customers")
        .database("crm"),
)
```

## Related

- [Execute plain query system API](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-execute-plain-query-api.html) · [All system APIs from code](https://docs.apimaker.dev/v4/examples/sys/system/system.html) · [The global object g](https://docs.apimaker.dev/v4/docs/pre-defined-terms/global-object-g.html)
