GO4SQL is an open source project to write in-memory SQL engine using nothing but Golang.
-
CREATE TABLE - you can create table with name table1
using
command:
CREATE TABLE table1( one TEXT , two INT);
First column is called one
and it contains strings (keyword TEXT
), second
one is called two
and it contains integers (keyword INT
).
-
DROP TABLE - you can destroy the table of name table1
using
command:
DROP TABLE table1;
After using this command table1 will no longer be available and all data connected to it (column
definitions and inserted values) will be lost.
-
INSERT INTO - you can insert values into table called table1
with
command:
INSERT INTO table1 VALUES( 'hello', 1);
Please note that the number of arguments and types of the values
must be the same as you declared with CREATE
.
-
UPDATE - you can update values in table called table1
with command:
UPDATE table1
SET column_name_1 TO new_value_1, column_name_2 TO new_value_2
WHERE id EQUAL 1;
It will update all rows where column id
is equal to 1
by replacing value in
column_name_1
with new_value_1
and column_name_2
with new_value_2
.
-
SELECT FROM - you can either select everything from table1
with:
SELECT * FROM table1;
Or you can specify column names that you're interested in:
SELECT one, two FROM table1;
Note that column names must be the
same as you declared with CREATE
and also duplicated column names will be ignored.
-
WHERE - is used to filter records. It is used to extract only those records that fulfill a
specified condition. It can be used with SELECT
like this:
SELECT column1, column2
FROM table_name
WHERE column1 NOT 'goodbye' OR column2 EQUAL 3;
Supported logical operations are: EQUAL
, NOT
, OR
, AND
, FALSE
, TRUE
.
-
IN - is used to check if a value from a column exists in a specified list of values.
It can be used with WHERE
like this:
SELECT column1, column2
FROM table_name
WHERE column1 IN ('value1', 'value2');
table_name
is the name of the table, and WHERE
returns rows that value is either equal to
value1
or value2
-
NOTIN - is used to check if a value from a column doesn't exist in a specified list of
values. It can be used with WHERE
like this:
SELECT column1, column2
FROM table_name
WHERE column1 NOTIN ('value1', 'value2');
table_name
is the name of the table, and WHERE
returns rows which values are not equal to
value1
and not equal to value2
-
DELETE FROM is used to delete existing records in a table. It can be used like this:
DELETE FROM tb1 WHERE two EQUAL 3;
tb1
is the name of the table, and WHERE
specify records that fulfill a
specified condition and afterward will be deleted.
-
ORDER BY is used to sort the result-set in ascending or descending order. It can be used
with SELECT
like this:
SELECT column1, column2,
FROM table_name
ORDER BY column1 ASC, column2 DESC;
In this case, this command will order by column1
in ascending order, but if some rows have the
same column1
, it orders them by column2 in descending order.
-
LIMIT is used to reduce number of rows printed out by returning only specified number of
records with SELECT
like this:
SELECT column1, column2,
FROM table_name
ORDER BY column1 ASC
LIMIT 5;
In this case, this command will order by column1
in ascending order and return 5 first
records.
-
OFFSET is used to reduce number of rows printed out by not skipping specified numbers of
rows in returned output with SELECT
like this:
SELECT column1, column2,
FROM table_name
ORDER BY column1 ASC
LIMIT 5 OFFSET 3;
In this case, this command will order by column1
in ascending order and skip 3 first records,
then return records from 4th to 8th.
-
DISTINCT is used to return only distinct (different) values in returned output with
SELECT
like this:
SELECT DISTINCT column1, column2,
FROM table_name;
In this case, this command will return only unique rows from table_name
table.
-
INNER JOIN is used to return a new table by combining rows from both tables where there is a
match on the
specified condition. Only the rows that satisfy the condition from both tables are included in the
result.
Rows from either table that do not meet the condition are excluded from the result.
SELECT *
FROM tableOne
JOIN tableTwo
ON tableOne.columnY EQUAL tableTwo.columnX;
or
SELECT *
FROM tableOne
INNER JOIN tableTwo
ON tableOne.columnY EQUAL tableTwo.columnX;
In this case, this command will return all columns from tableOne and tableTwo for rows where the
condition
tableOne.columnY
= tableTwo.columnX
is met (i.e., the value of columnY
in tableOne
is equal to the
value of columnX
in tableTwo
).
-
LEFT JOIN is used to return a new table that includes all records from the left table and
the matched records
from the right table. If there is no match, the result will contain empty values for columns from
the right table.
SELECT *
FROM tableOne
LEFT JOIN tableTwo
ON tableOne.columnY EQUAL tableTwo.columnX;
In this case, this command will return all columns from tableOne
and the matching columns from
tableTwo
. For
rows in
tableOne
that do not have a corresponding match in tableTwo
, the result will include empty
values for columns
from
tableTwo
.
-
RIGHT JOIN is used to return a new table that includes all records from the right table and
the matched records
from the left table. If there is no match, the result will contain empty values for columns from
the left table.
SELECT *
FROM tableOne
RIGHT JOIN tableTwo
ON tableOne.columnY EQUAL tableTwo.columnX;
In this case, this command will return all columns from tableTwo
and the matching columns from
tableOne
. For
rows in
tableTwo
that do not have a corresponding match in tableOne
, the result will include empty
values for columns
from
tableOne
.
-
FULL JOIN is used to return a new table created by joining two tables as a whole. The
joined table contains all
records from both tables and fills empty values for missing matches on either side. This join
combines the results of
both LEFT JOIN
and RIGHT JOIN
.
SELECT *
FROM tableOne
FULL JOIN tableTwo
ON tableOne.columnY EQUAL tableTwo.columnX;
In this case, this command will return all columns from tableOne
and tableTwo
for rows
fulfilling condition
tableOne.columnY EQUAL tableTwo.columnX
(value of columnY
in tableOne
is equal the
value of columnX
in
tableTwo
).
-
MIN() is used to return the smallest value in a specified column.
SELECT MIN(columnName)
FROM tableName;
In this case, this command will return the smallest value found in the column columnName
of
tableName
.
-
MAX() is used to return the largest value in a specified column.
SELECT MAX(columnName)
FROM tableName;
This command will return the largest value found in the column columnName
of tableName
.
-
COUNT() is used to return the number of rows that match a given condition or the total
number of rows in a
specified column.
SELECT COUNT(columnName)
FROM tableName;
This command will return the number of rows in the columnName
of tableName
.
-
SUM() is used to return the total sum of the values in a specified numerical column.
SELECT SUM(columnName)
FROM tableName;
This command will return the total sum of all values in the numerical column columnName
of
tableName
.
-
AVG() is used to return the average of values in a specified numerical column.
SELECT AVG(columnName)
FROM tableName;
This command will return the average of all values in the numerical column columnName
of
tableName
.