This document contains practical examples demonstrating how to use the Generic SQL API Framework.
The examples show how JSON API requests are translated into SQL queries by the backend query builder.
Each example includes:
- JSON Request
- Generated SQL
- Expected Response where applicable
These examples are intended as a quick reference for developers integrating applications with the API.
For the complete JSON request structure, see JSON Request Reference.
For HTTP API usage, see API.
{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"Phone"
]
}SELECT
Cust_Name,
Phone
FROM CustomerTable;{
"success": true,
"rowsReturned": 2,
"data": [
{
"Cust_Name": "ABC Traders",
"Phone": "9876543210"
},
{
"Cust_Name": "XYZ Enterprises",
"Phone": "9988776655"
}
]
}{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"City"
],
"where": [
{
"column": "City",
"operator": "=",
"value": "Bangalore"
}
]
}SELECT
Cust_Name,
City
FROM CustomerTable
WHERE City = ?;The value is passed separately to the prepared statement.
{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"City",
"Balance"
],
"where": [
{
"column": "City",
"operator": "=",
"value": "Bangalore"
},
{
"column": "Balance",
"operator": ">",
"value": 50000
}
]
}SELECT
Cust_Name,
City,
Balance
FROM CustomerTable
WHERE City = ?
AND Balance > ?;{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"Balance"
],
"orderBy": [
{
"column": "Balance",
"direction": "DESC"
}
]
}SELECT
Cust_Name,
Balance
FROM CustomerTable
ORDER BY Balance DESC;{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"City",
"Balance"
],
"orderBy": [
{
"column": "City",
"direction": "ASC"
},
{
"column": "Balance",
"direction": "DESC"
}
]
}SELECT
Cust_Name,
City,
Balance
FROM CustomerTable
ORDER BY
City ASC,
Balance DESC;{
"controller": "Query",
"action": "select",
"table": "SalesTable",
"columns": [
"City",
{
"function": "COUNT",
"column": "InvoiceNo",
"alias": "Invoices"
}
],
"groupBy": [
"City"
]
}SELECT
City,
COUNT(InvoiceNo) AS Invoices
FROM SalesTable
GROUP BY City;{
"controller": "Query",
"action": "select",
"table": "SalesTable",
"columns": [
"City",
{
"function": "SUM",
"column": "Amount",
"alias": "TotalSales"
}
],
"groupBy": [
"City"
],
"having": [
{
"function": "SUM",
"column": "Amount",
"operator": ">",
"value": 100000
}
]
}SELECT
City,
SUM(Amount) AS TotalSales
FROM SalesTable
GROUP BY City
HAVING SUM(Amount) > ?;The value is passed separately to the prepared statement.
{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"CustomerTable.Cust_Name",
"InvoiceTable.InvoiceNo"
],
"joins": [
{
"type": "INNER",
"table": "InvoiceTable",
"on": {
"left": "CustomerTable.Cust_ID",
"right": "InvoiceTable.Cust_ID"
}
}
]
}SELECT
CustomerTable.Cust_Name,
InvoiceTable.InvoiceNo
FROM CustomerTable
INNER JOIN InvoiceTable
ON CustomerTable.Cust_ID = InvoiceTable.Cust_ID;{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name"
],
"pagination": {
"page": 2,
"pageSize": 20
}
}Pagination is generated according to the query builder's SQL Server pagination requirements.
Conceptually:
SELECT
Cust_Name
FROM CustomerTable
ORDER BY <column>
OFFSET 20 ROWS
FETCH NEXT 20 ROWS ONLY;SQL Server pagination requires an ordering expression. The actual generated query depends on the request and query builder implementation.
{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
{
"function": "COUNT",
"column": "Cust_Name",
"alias": "TotalCustomers"
},
{
"function": "MIN",
"column": "Bill_Amt",
"alias": "MinimumBill"
},
{
"function": "MAX",
"column": "Bill_Amt",
"alias": "MaximumBill"
}
],
"groupBy": [
"Cust_Name"
],
"where": [],
"page": 1,
"pageSize": 50
}SELECT
Cust_Name,
COUNT(Cust_Name) AS TotalCustomers,
MIN(Bill_Amt) AS MinimumBill,
MAX(Bill_Amt) AS MaximumBill
FROM CustomerTable
GROUP BY Cust_Name;Supported aggregate functions include:
COUNT
SUM
AVG
MIN
MAX
STRING_AGG
{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"City"
],
"distinct": true
}SELECT DISTINCT
City
FROM CustomerTable;{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
"Balance"
],
"top": 10
}SELECT TOP 10
Cust_Name,
Balance
FROM CustomerTable;{
"controller": "Query",
"action": "select",
"table": "CustomerTable",
"columns": [
"Cust_Name",
{
"case": [
{
"when": {
"column": "Balance",
"operator": ">=",
"value": 100000
},
"then": "Premium"
}
],
"else": "Regular",
"alias": "CustomerType"
}
]
}SELECT
Cust_Name,
CASE
WHEN Balance >= ? THEN ?
ELSE ?
END AS CustomerType
FROM CustomerTable;Values are supplied separately during query execution.
Arithmetic expressions can be used when calculating values from database columns.
SELECT
Quantity,
UnitPrice,
Quantity * UnitPrice AS TotalAmount
FROM SalesTable;The expression can be represented using the framework's expression structure where supported.
SELECT
Cust_Name,
City
FROM CustomerTable
WHERE City IN (?, ?, ?);The values are supplied separately during execution.
SELECT
Cust_Name,
City
FROM CustomerTable
WHERE City NOT IN (?, ?, ?);SELECT
Cust_Name,
Balance
FROM CustomerTable
WHERE Balance BETWEEN ? AND ?;SELECT
Cust_Name,
Balance
FROM CustomerTable
WHERE Balance NOT BETWEEN ? AND ?;A subquery can be used where supported by the framework query definition.
SELECT
Cust_Name,
Balance
FROM CustomerTable
WHERE Balance > (
SELECT AVG(Balance)
FROM CustomerTable
);The subquery is evaluated by SQL Server as part of the generated query.
SELECT
CustomerTable.Cust_Name
FROM CustomerTable
WHERE EXISTS (
SELECT 1
FROM InvoiceTable
WHERE InvoiceTable.Cust_ID = CustomerTable.Cust_ID
);SELECT
CustomerTable.Cust_Name
FROM CustomerTable
WHERE NOT EXISTS (
SELECT 1
FROM InvoiceTable
WHERE InvoiceTable.Cust_ID = CustomerTable.Cust_ID
);The API supports SQL functions through the query definition.
SELECT
UPPER(Cust_Name) AS CustomerName,
LOWER(City) AS CityName,
LEN(Cust_Name) AS NameLength
FROM CustomerTable;Supported examples include:
UPPER
LOWER
LTRIM
RTRIM
TRIM
LEN
CONCAT
LEFT
RIGHT
SUBSTRING
REPLACE
CHARINDEX
PATINDEX
FORMAT
SELECT
Cust_Name,
COALESCE(Phone, 'N/A') AS Phone
FROM CustomerTable;Supported functions include:
COALESCE
ISNULL
NULLIF
SELECT
CAST(Bill_Amt AS DECIMAL(18,2)) AS BillAmount
FROM CustomerTable;Another example:
SELECT
CONVERT(VARCHAR(10), InvoiceDate, 120) AS InvoiceDate
FROM InvoiceTable;Supported functions include:
CAST
CONVERT
SELECT
InvoiceDate,
YEAR(InvoiceDate) AS InvoiceYear,
MONTH(InvoiceDate) AS InvoiceMonth,
DAY(InvoiceDate) AS InvoiceDay
FROM InvoiceTable;Supported functions include:
YEAR
MONTH
DAY
DATEPART
DATENAME
GETDATE
DATEADD
DATEDIFF
EOMONTH
ISDATE
DATEFROMPARTS
DATETIMEFROMPARTS
TIMEFROMPARTS
SYSDATETIME
CURRENT_TIMESTAMP
IIF
SELECT
Bill_Amt,
ABS(Bill_Amt) AS AbsoluteAmount,
ROUND(Bill_Amt, 2) AS RoundedAmount,
CEILING(Bill_Amt) AS CeilingAmount,
FLOOR(Bill_Amt) AS FloorAmount
FROM CustomerTable;Supported functions include:
ABS
ROUND
CEILING
FLOOR
POWER
SQRT
EXP
LOG
SELECT
Cust_Name,
Balance,
ROW_NUMBER() OVER (
ORDER BY Balance DESC
) AS RowNumber
FROM CustomerTable;SELECT
Cust_Name,
Balance,
RANK() OVER (
ORDER BY Balance DESC
) AS CustomerRank
FROM CustomerTable;SELECT
InvoiceDate,
Amount,
LAG(Amount) OVER (
ORDER BY InvoiceDate
) AS PreviousAmount,
LEAD(Amount) OVER (
ORDER BY InvoiceDate
) AS NextAmount
FROM SalesTable;Supported window functions include:
ROW_NUMBER
RANK
DENSE_RANK
NTILE
LAG
LEAD
FIRST_VALUE
LAST_VALUE
WITH CustomerTotals AS (
SELECT
Cust_ID,
SUM(Bill_Amt) AS TotalBill
FROM CustomerTable
GROUP BY Cust_ID
)
SELECT
Cust_ID,
TotalBill
FROM CustomerTotals;The CTE is processed as part of the generated SQL query.
Recursive CTE support can be used for hierarchical data where supported by the framework.
WITH EmployeeHierarchy AS (
SELECT
EmployeeID,
EmployeeName,
ManagerID,
0 AS Level
FROM EmployeeTable
WHERE ManagerID IS NULL
UNION ALL
SELECT
E.EmployeeID,
E.EmployeeName,
E.ManagerID,
H.Level + 1
FROM EmployeeTable E
INNER JOIN EmployeeHierarchy H
ON E.ManagerID = H.EmployeeID
)
SELECT
EmployeeID,
EmployeeName,
ManagerID,
Level
FROM EmployeeHierarchy;SELECT
Cust_Name,
City
FROM CustomerTable
UNION
SELECT
CustomerName,
City
FROM ArchivedCustomerTable;UNION combines the result sets and removes duplicate rows.
SELECT
Cust_Name,
City
FROM CustomerTable
UNION ALL
SELECT
CustomerName,
City
FROM ArchivedCustomerTable;UNION ALL combines the result sets without removing duplicates.
Stored procedure execution is supported by the backend.
EXEC GetCustomerDetails
@CustomerId = ?;Parameters are supplied separately during execution.
The exact request structure for stored procedures should follow the API request definition supported by the current backend.
Scalar database functions can be executed through the backend.
SELECT
dbo.CalculateCustomerBalance(?) AS Balance;Parameters are supplied separately during execution.
Table-valued functions can be used as database result sources.
SELECT
CustomerID,
CustomerName,
Balance
FROM dbo.GetCustomerDetails(?);The API can combine multiple supported query components in a single request.
{
"controller": "Query",
"action": "select",
"table": "SalesTable",
"columns": [
"City",
{
"function": "SUM",
"column": "Amount",
"alias": "TotalSales"
}
],
"where": [
{
"column": "Status",
"operator": "=",
"value": "Completed"
}
],
"groupBy": [
"City"
],
"having": [
{
"function": "SUM",
"column": "Amount",
"operator": ">",
"value": 50000
}
],
"orderBy": [
{
"column": "TotalSales",
"direction": "DESC"
}
],
"pagination": {
"page": 1,
"pageSize": 10
}
}WHERE
|
v
GROUP BY
|
v
HAVING
|
v
ORDER BY
|
v
PAGINATION
This allows multiple query components to be combined without creating a separate API endpoint for each combination.
Values supplied through conditions are represented as parameters in generated SQL.
For example:
{
"where": [
{
"column": "City",
"operator": "=",
"value": "Bangalore"
}
]
}WHERE City = ?The value is passed separately during query execution.
This allows the database execution layer to use prepared ODBC statements instead of directly inserting request values into the SQL string.
The examples demonstrate the following request components:
| Component | Purpose |
|---|---|
controller |
Selects the controller |
action |
Selects the operation |
table |
Defines the main table |
columns |
Defines selected columns and expressions |
where |
Filters rows |
joins |
Joins tables |
groupBy |
Groups results |
having |
Filters grouped results |
orderBy |
Sorts results |
pagination |
Controls result pagination |
distinct |
Removes duplicate rows |
top |
Limits the number of returned rows |
functions |
Represents supported SQL functions |
parameters |
Supplies values separately during execution |
The current API supports the following major SQL capabilities:
SELECT
DISTINCT
TOP
WHERE
AND / OR
JOIN
GROUP BY
HAVING
ORDER BY
PAGINATION
CASE
Arithmetic Expressions
Subqueries
EXISTS
NOT EXISTS
IN
NOT IN
BETWEEN
NOT BETWEEN
CTE
Recursive CTE
UNION
UNION ALL
COUNT
SUM
AVG
MIN
MAX
STRING_AGG
UPPER
LOWER
LTRIM
RTRIM
TRIM
LEN
COALESCE
ISNULL
CAST
CONVERT
NULLIF
CONCAT
LEFT
RIGHT
SUBSTRING
REPLACE
CHARINDEX
PATINDEX
FORMAT
YEAR
MONTH
DAY
DATEPART
DATENAME
GETDATE
DATEADD
DATEDIFF
EOMONTH
ISDATE
DATEFROMPARTS
DATETIMEFROMPARTS
TIMEFROMPARTS
SYSDATETIME
CURRENT_TIMESTAMP
IIF
ABS
ROUND
CEILING
FLOOR
POWER
SQRT
EXP
LOG
ROW_NUMBER
RANK
DENSE_RANK
NTILE
LAG
LEAD
FIRST_VALUE
LAST_VALUE
Stored Procedures
Scalar Functions
Table-Valued Functions
The following examples will be added when the corresponding backend functionality is implemented:
- INSERT
- UPDATE
- DELETE
- UPSERT
- Transactions
These operations are planned for future backend versions and should not be treated as currently supported API operations.
The examples in this document should match the actual backend query builder and validation implementation.
When a new query capability is added:
- Add or update the corresponding example.
- Update the supported capability summary.
- Update
JSON-Request-Reference.mdif the request structure changes. - Update
Roadmap.mdif the feature changes the planned release scope.
The documentation should not list SQL functionality as supported unless it is implemented and tested in the backend.