SQL (Structured Query Language) is a descriptive computer language designed for updating, retrieving, and calculating data in table-based databases.
(c) MDN
🐊Putout plugin adds ability improve SQL. Not Bundled.
npm i putout @putout/plugin-sql -D
Add .putout.json with:
{
"plugins": ["sql"]
}{
"rules": {
"sql/apply-count": "on",
"sql/postgres": "on",
"sql/convert-sqlite-to-postgres": "off",
"sql/convert-postgres-to-sqlite": "off"
}
}The COUNT() function returns the number of rows that matches a specified criterion.
(c) w3cshools.com
Check out in 🐊Putout Editor.
SELECT COUNT(1) FROM ordersSELECT COUNT(*) FROM orders- ✅ apply-key-exists;
- ✅ apply-jsonb-extract-path-text;
- ✅ convert-last-insert-rowid-to-returning-id;
- ✅ convert-auto-increment-to-identity;
- ✅ convert-generate-series-to-with-recursive;
{
"rules": {
"sql/convert-sqlite-to-postgres/apply-key-exists": "off",
"sql/convert-sqlite-to-postgres/apply-jsonb-extract-path-text": "off",
"sql/convert-sqlite-to-postgres/convert-last-insert-rowid-to-returnning-id": "off",
"sql/convert-sqlite-to-postgres/convert-auto-increment-to-identitiy": "off",
"sql/convert-sqlite-to-postgres/convert-generate-series-to-with-recursive": "off"
}
}
?- does the string exist as a top-level key within the JSON value?
Checkout in 🐊Putout Editor.
SELECT *
FROM users
WHERE json_type(data, '$.email') IS NOT NULL;SELECT *
FROM users
WHERE data ? 'email';The
jsonb_extract_path_text()function allows you to extract a JSON subobject as text from a JSONB value at a specified path.
Checkout in 🐊Putout Editor.
SELECT json_extract(data, '$.user.name')
FROM users;SELECT jsonb_extract_path_text(data, 'user', 'name')
FROM users;The
last_insert_rowid()function returns the ROWID of the last row insert from the database connection which invoked the function
Sometimes it is useful to obtain data from modified rows while they are being manipulated. The
INSERT,UPDATE,DELETE, andMERGEcommands all have an optionalRETURNINGclause that supports this. Use ofRETURNINGavoids performing an extra database query to collect the data, and is especially valuable when it would otherwise be difficult to identify the modified rows reliably.
Checkout in 🐊Putout Editor.
INSERT INTO users (name)
VALUES ('Alice');
SELECT last_insert_rowid();INSERT INTO users (name)
VALUES ('Alice')
RETURNING id;If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
An identity column is a special column that is generated automatically from an implicit sequence. It can be used to generate key values.
Checkout in 🐊Putout Editor.
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);CREATE TABLE users (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);The
generate_series()generates a series of values from start to stop, with a step size of step
Checkout in 🐊Putout Editor.
SELECT * FROM generate_series(1, 10, 2);WITH RECURSIVE numbers(value) AS (
SELECT 1
UNION ALL
SELECT value + 2
FROM numbers
WHERE value < 10
)
SELECT value FROM numbers;- ✅ apply-json-extract;
- ✅ apply-json-type;
- ✅ apply-auto-increment;
- ✅ convert-with-to-sequential;
- ✅ convert-lastval-to-last-insert-rowid;
{
"rules": {
"sql/convert-postgrest-to-sqlite/apply-json-type": "off",
"sql/convert-postgrest-to-sqlite/apply-json-extract": "off",
"sql/convert-postgrest-to-sqlite/apply-auto-increment": "off",
"sql/convert-postgrest-to-sqlite/convert-with-to-sequential": "off",
"sql/convert-postgrest-to-sqlite/convert-lastval-to-last-insert-rowid": "off"
}
}The
json_type(X)function returns the "type" of the outermost element ofX
Checkout in 🐊Putout Editor.
SELECT *
FROM users
WHERE data ? 'email';SELECT *
FROM users
WHERE json_type(data, '$.email') IS NOT NULL;The
json_extract(X,P1,P2,...)extracts and returns one or more values from the well-formed JSON atX.
Checkout in 🐊Putout Editor.
SELECT jsonb_extract_path_text(data, 'user', 'name')
FROM users;SELECT json_extract(data, '$.user.name')
FROM users;With
AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the automatically generated ROWIDs are guaranteed to be monotonically increasing.
Checkout in 🐊Putout Editor.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);
CREATE TABLE users (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT
);CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
WITHprovides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query. Each auxiliary statement in aWITHclause can be aSELECT,INSERT,UPDATE,DELETE, orMERGE.
Checkout in 🐊Putout Editor.
INSERT INTO users (name)
VALUES ('Alice');
SELECT last_insert_rowid();INSERT INTO users (name)
VALUES ('Alice')
RETURNING id;
nextvalreturns the value most recently returned by nextval in the current session. This function is identical tocurrval, except that instead of taking the sequence name as an argument it refers to whichever sequence nextval was most recently applied to in the current session. It is an error to calllastvalifnextvalhas not yet been called in the current session.
The
last_insert_rowid()function returns theROWIDof the last row insert from the database connection which invoked the function.(c) sqlite.org
Checkout in 🐊Putout Editor.
SELECT lastval();SELECT last_insert_rowid();{
"rules": {
"sql/postgres/apply-generate-series": "on",
"sql/postgres/convert-sequence-to-serial": "on",
"sql/postgres/convert-serial-to-identity": "on"
}
}The
generate_series()generates a series of values from start to stop, with a step size of step
Checkout in 🐊Putout Editor.
WITH RECURSIVE numbers(value) AS (
SELECT 1
UNION ALL
SELECT value + 2
FROM numbers
WHERE value < 10
)
SELECT value FROM numbers;SELECT * FROM generate_series(1, 10, 2);The data types
smallserial,serialandbigserialare not true types, but merely a notational convenience for creating unique identifier columns (similar to theAUTO_INCREMENTproperty supported by some other databases)
Checkout in 🐊Putout Editor.
CREATE SEQUENCE users_id_seq;
CREATE TABLE users (
id INTEGER DEFAULT nextval('users_id_seq') PRIMARY KEY,
name TEXT
);CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);An identity column is a special column that is generated automatically from an implicit sequence. It can be used to generate key values.
Checkout in 🐊Putout Editor.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);CREATE TABLE users (
id GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT
);MIT