Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

SQL translation is a great tool that transalte any SQL statement(s) to a different dialetc using the JOOQ Parser.

SQL VariantCode
List
databases [1]
SQLite 3.DATABASES
MySQLSHOW DATABASES
Spark/HiveSHOW DATABASES
SHOW DATABASES LIKE "*user*"
hdfs dfs -ls /path/to/hive/warehouse
/* You can query the Hive Metastore DB if you have access. */
TeradataSHOW DATABASES
OracleSHOW DATABASES
MS SQL ServerSHOW DATABASES
Use a
databases [1]
SQLite 3
MySQLUSE database_name
Spark/Hive
TeradataUSE database_name
OracleUSE database_name
MS SQL ServerUSE database_name
List all
tables
in the
current
database [1]
SQLite 3.TABLES SELECT name FROM db.sqlite_master WHERE type='table'
MySQLSHOW TABLES
Spark/HiveSHOW TABLES
SHOW TABLES in db_name
SHOW TABLES in db_name like '*cust*'
SHOW TABLE EXTENDED in db_name like '*cust*'
TeradataSHOW TABLES
Oracle/* All tables in a database */ SELECT * FROM dba_tables WHERE table_schema = 'current database name'
/* The table all_tab_cols contains information about tables and their columns */
/* List all tables owned by the current user */ select * from user_tables
/* List all tables accessible to the current user */ select * from all_tables
MS SQL ServerSELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_catalog = 'current database name'
Describe
a table
SQLite 3.SCHEMA table_name
MySQLDESCRIBE table_name
Spark/HiveDESCRIBE [EXTENDED] table_name
TeradataHELP TABLE table_name
HELP COLUMN table_name.*
OracleDESCRIBE table_name
MS SQL ServerDESCRIBE table_name
Source code
of a table
SQLite 3
MySQL
Spark/HiveSHOW CREATE table_name
Teradata
Oracle
MS SQL Server
List all
tables
owned
by the
current
user
SQLite 3
MySQL
Spark/Hive
Teradata
Oracle/* there is no "owner" column in user_tables, since all tables in user_tables are owned by the current user */ SELECT * FROM user_tables
SELECT * FROM all_tables WHERE owner = "current_user_name"
SELECT * FROM dba_tables WHERE owner = "curent_user_name"
MS SQL Server
List all
tables
accessible
by the
current
user
SQLite 3
MySQL
Spark/Hive
Teradata
OracleSELECT * FROM all_tables
MS SQL Server
List all
tables
in the
system
SQLite 3
MySQL
Spark/Hive
Teradata
OracleSELECT * FROM dba_tables
MS SQL Server
Create
a
table
SQLite 3CREATE TABLE IF NOT EXISTS queries (     query Text NOT NULL PRIMARY KEY,     timestamp Real NOT NULL,     data Blob NOT NULL )
MySQLCREATE TABLE IF NOT EXISTS queries (     query VarChar NOT NULL PRIMARY KEY,     timestamp Decimal(18, 3) NOT NULL,     data Blob NOT NULL )
Spark/HiveCREATE TABLE IF NOT EXISTS queries (     query String NOT NULL,     timestamp Double NOT NULL,     data Binary NOT NULL )
TeradataCREATE TABLE IF NOT EXISTS queries (     query VarChar NOT NULL PRIMARY KEY,     timestamp Number NOT NULL,     data Blob NOT NULL )
OracleCREATE TABLE IF NOT EXISTS queries (     query Text NOT NULL PRIMARY KEY,     timestamp Decimal(18, 3) NOT NULL,     data Blob NOT NULL )
MS SQL ServerCREATE TABLE IF NOT EXISTS queries (     query VarChar NOT NULL PRIMARY KEY,     timestamp Decimal(18, 3) NOT NULL,     data Blob NOT NULL )
Drop a
table
if exists
SQLite 3
MySQL
Spark/Hive
TeradataDROP TABEL IF EXISTS table_name
OracleIF object_id(table_name) IS NOT NULL THEN DROP TABLE table_name
MS SQL Server
Limit
number
of
returned
rows
SQLite 3SELECT * FROM table LIMIT 5
MySQLSELECT * FROM table_name LIMIT 5
Spark/HiveSELECT * FROM table_name LIMIT 5
TeradataSELECT TOP 5 * FROM table
OracleSELECT * FROM table LIMIT 5
MS SQL ServerSELECT TOP 5 * FROM table
SELECT TOP 50 PERCENT * FROM table
Randomly
sample
100 rows
SQLite 3SELECT * FROM table ORDER BY random() LIMIT 100
MySQL
Spark/HiveSELECT * FROM table TABLESAMPLE (1 PERCENT) LIMIT 100
SELECT * FROM table ORDER BY random() LIMIT 100
/* NOTE: the following does NOT work!!! It is equivalent to `LIMIT 100` */ SELECT * FROM table TABLESAMPLE (100 ROWS)
TeradataSELECT * FROM table SAMPLE 100
Oracle
MS SQL Server
Randomly sample
rows with
acceptance
ratio 0.1
SQLite 3/* Note that `random()` generates a pseudo-random integer between -9223372036854775808 and +9223372036854775807. */ SELECT * FROM table WHERE random() % 10 = 0
MySQL
Spark/HiveSELECT * FROM table TABLESAMPLE (10 PERCENT)
TeradataSELECT * FROM table SAMPLE 0.1
Oracle
MS SQL Server
Randomly
sample
buckets
SQLite 3
MySQL
Spark/HiveSELECT * FROM table TABLESAMPLE (BUCKET 4 out of 10)
Teradata
Oracle
MS SQL Server
Insert
multiple
rows in
one
statement
SQLite 3
MySQL
Spark/HiveINSERT INTO tablel_name PARTITION (     par_col_1 = pv1,     par_col_2 = pv2 ) VALUES (     v11, v12, ... ), (     v21, v22, ... )
TeradataINSERT INTO table_name (     first_name,     last_name ) VALUES     ('Fred', 'Smith'),     ('John', 'Smith'),     ('Michael', 'Smith'),     ('Robert', 'Smith')
OracleINSERT INTO pager (     pag_id,     pag_parent,     pag_name,     pag_active ) SELECT 8000, 0, 'Multi 8000', 1 FROM dual UNION ALL SELECT 8001, 0, 'Multi 8001', 1 FROM dual
INSERT ALL INTO t (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3') INTO t (col1, col2, col3) VALUES ('val2_1', 'val2_2', 'val2_3') INTO t (col1, col2, col3) VALUES ('val3_1', 'val3_2', 'val3_3')
MS SQL Server
UpdateSQLite 3
MySQL
Spark- not supported as of Spark 2.4.5 - Spark SQL 3.0.0 has update/delete APIs but not implemented - Update/delete is feasible using Detal Lake
Hive
Teradata
Oracle
MS SQL Server
Insert
or
Replace
SQLite 3REPLACE INTO phonebook (     name, phonenumber, validdate ) VALUES (     'Alice', '704-555-1212', '2018-05-08' )
MySQL
Spark- not supported as of Spark 2.4.5 - Spark SQL 3.0.0 has update/delete APIs but not implemented - Update/delete is feasible using Detal Lake
Hive
Teradata
Oracle
MS SQL Server
UpsertSQLite 3INSERT INTO phonebook (     name, phonenumber, validDate ) VALUES (     'Alice', '704-555-1212', '2018-05-08' ) ON CONFLICT (name) DO UPDATE SET     phonenumber=excluded.phonenumber,     validDate=excluded.validDate WHERE     excluded.validDate > phonebook.validDate
MySQL
Spark- not supported as of Spark 2.4.5 - Spark SQL 3.0.0 has update/delete APIs but not implemented - Update/delete is feasible using Detal Lake
Hive
Teradata
Oracle
MS SQL Server
DeleteSQLite 3
MySQL
Spark- not supported as of Spark 2.4.5 - Spark SQL 3.0.0 has update/delete APIs but not implemented - Update/delete is feasible using Detal Lake
Hive
Teradata
Oracle
MS SQL Server
InsertSQLite 3
MySQL
Spark/Hive
TeradataINSERT INTO some_table SELECT * FROM another_table
Oracle
MS SQL Server
Refresh
Table
Cache
SQLite 3
MySQL
Spark/HiveREFRESH TABLE table_name
Teradata
Oracle
MS SQL Server
Concatenate
Strings
SQLite 3
MySQL
Spark/HiveSELECT concat('Spark', 'SQL') FROM table
Teradata
Oracle
MS SQL Server
SubstringSQLite 3
MySQL
Spark/Hive/* substr and substring are equivalent in Spark/Hive SQL */ SELECT     substr('Spark SQL', 5, 1) -- resulting 'k' FROM     table
left
right
Teradatasubstr
Oracle
MS SQL Server
trimSQLite 3
MySQL
Spark/Hiveltrim/rtrim/trim
Teradatatrim
Oracle
MS SQL Server
substituteSQLite 3
MySQL
Spark/Hivereplace/translate/regexp_replace
Teradataoreplace, otranslate
Oracle
MS SQL Server
length of stringSQLite 3
MySQL
Spark/Hive
Teradatachar_length
Oracle
MS SQL Server
Index of substringSQLite 3
MySQL
Spark/Hivesubstring_index
Teradataposition('de' IN 'abcdefg')
regexp_instr('abc', 'a') -- 1-base index
Oracle
MS SQL Server
upper caseSQLite 3
MySQL
Spark/Hiveucase/upper
Teradata
Oracle
MS SQL Server
decode base64SQLite 3
MySQL
Spark/Hiveunbase64
Teradata
Oracle
MS SQL Server
decode hexSQLite 3
MySQL
Spark/Hiveunhex
Teradata
Oracle
MS SQL Server
generate an uuidSQLite 3
MySQL
Spark/Hiveuuid
Teradata
Oracle
MS SQL Server
reverse a stringSQLite 3
MySQL
Spark/Hivereverse
Teradata
Oracle
MS SQL Server
string matchingSQLite 3
MySQL
Spark/Hive/* like is Case sensitive */ like
rlike
Teradata/* Like is case-insensitive by default. You can specify the keyword CaseSpecific to make it case-sensitive. */ SELECT empname FROM tbl_emp WHERE empname (CaseSpecific) like '%JO%’
Oracle/*like is case sensitive*/ like
regexp_like(name, 'string$', 'i')
MS SQL Server
shift stringSQLite 3
MySQL
Spark/Hiveshiftleft
shiftright
Teradata
Oracle
MS SQL Server
ocurrence of charSQLite 3
MySQL
Spark/Hive
Teradatachar_length('Teradata is Relational Database') - char_length(Oreplace('Teradata is Relational Database', 'a', ''))
Oracle
MS SQL Server
Cast to dateSQLite 3
MySQL
Spark/HiveSELECT     to_date("2000-01-01") AS date FROM     table
Teradatacast(str_col as date format 'YYYY-MM-DD')
Oracle
MS SQL Server
Cast to timestampSQLite 3
MySQL
Spark/HiveSELECT     to_timestamp("2000-01-01") AS date FROM     table
SELECT     to_timestamp("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Cast to UTC timestampSQLite 3
MySQL
Spark/HiveSELECT     to_utc_timestamp("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Cast to Unix timestampSQLite 3
MySQL
Spark/HiveSELECT     to_unix_timestamp("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract yearSQLite 3
MySQL
Spark/HiveSELECT     year("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract quarterSQLite 3
MySQL
Spark/HiveSELECT     quarter("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract monthSQLite 3
MySQL
Spark/HiveSELECT     montofyearh("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract daySQLite 3
MySQL
Spark/HiveSELECT     day("2000-01-01") AS date FROM     table
SELECT     dayofmonth("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract minuteSQLite 3
MySQL
Spark/HiveSELECT     minute("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Extract secondSQLite 3
MySQL
Spark/HiveSELECT     second("2000-01-01") AS date FROM     table
Teradata
Oracle
MS SQL Server
Add days to a dateSQLite 3
MySQL
Spark/HiveSELECT     date_add(current_date, 3) AS date3 FROM     table
/* The + operator for date and int is support since Spark 3 */ SELECT     current_date + 3 AS date3 FROM     table
Teradata
Oracle
MS SQL Server
Add months to a dateSQLite 3
MySQL
Spark/HiveSELECT     add_months(current_date, 3) AS date3 FROM     table
Teradata
Oracle
MS SQL Server
Subtract days from a dateSQLite 3
MySQL
Spark/HiveSELECT     date_sub(current_date, 3) AS date3 FROM     table
/* The - operator for date and int is supported since Spark 3 */ SELECT     current_date - 3 AS date3 FROM     table
Teradata
Oracle
MS SQL Server
Diff between two datesSQLite 3
MySQL
Spark/HiveSELECT     datediff(date1, date2) AS diff FROM     table
Teradata
Oracle
MS SQL Server
truncate dateSQLite 3
MySQL
Spark/HiveSELECT date_trunc('2015-03-05T09:32:05.359', 'YEAR') -- 2015-01-01T00:00:00
SELECT date_trunc('2015-03-05T09:32:05.359', 'MM') -- 2015-03-01T00:00:00
SELECT date_trunc('2015-03-05T09:32:05.359', 'DD') -- 2015-03-05T00:00:00
SELECT date_trunc('2015-03-05T09:32:05.359', 'HOUR') -- 2015-03-05T09:00:00
SELECT trunc('2009-02-12', 'MM') -- 2009-02-01
SELECT trunc('2015-10-27', 'YEAR') -- 2015-01-01
Teradata
Oracle
MS SQL Server
sthSQLite 3
MySQL
Spark/Hive
Teradata
Oracle
MS SQL Server

[1] The terminology (database, schema or namespace) might be different for differennt databases.

A [NOT] LIKE B¶ NULL if A or B is NULL, TRUE if string A matches the SQL simple regular expression B, otherwise FALSE. The comparison is done character by character. The character in B matches any character in A (similar to . in posix regular expressions) while the % character in B matches an arbitrary number of characters in A (similar to .* in posix regular expressions). For example, ‘foobar’ like ‘foo’ evaluates to FALSE whereas ‘foobar’ like 'foo ’ evaluates to TRUE and so does ‘foobar’ like ‘foo%’.

A RLIKE B NULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, ‘foobar’ RLIKE ‘foo’ evaluates to TRUE and so does ‘foobar’ RLIKE ‘^f.*r$’.

A REGEXP B Same as RLIKE.

A || B Concatenate A and B (as of Hive 2.2.0).

References

Ten SQL Tricks that You Didn’t Think Were Possible (Lukas Eder)

Column Alias in SQL

http://www.legendu.net/misc/blog/Use-tablesample-in-sql

http://www.legendu.net/misc/blog/spark-dataframe-func-date

http://www.legendu.net/misc/blog/spark-dataframe-func-string

https://www.oreilly.com/library/view/high-performance-mysql/9780596101718/ch04.html

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-StringFunctions

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-StringOperators

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-RelationalOperators