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 Variant | Code | |
---|---|---|
List databases [1] |
SQLite 3 |
.DATABASES
|
MySQL |
SHOW DATABASES
|
|
Spark/Hive |
SHOW DATABASES
|
|
SHOW DATABASES LIKE "*user*"
|
||
hdfs dfs -ls /path/to/hive/warehouse
|
||
/* |
||
Teradata |
SHOW DATABASES
|
|
Oracle |
SHOW DATABASES
|
|
MS SQL Server |
SHOW DATABASES
|
|
Use a databases [1] |
SQLite 3 |
|
MySQL |
USE database_name
|
|
Spark/Hive |
|
|
Teradata |
USE database_name
|
|
Oracle |
USE database_name
|
|
MS SQL Server |
USE database_name
|
|
List all tables in the current database [1] |
SQLite 3 |
.TABLES |
MySQL |
SHOW TABLES
|
|
Spark/Hive |
SHOW TABLES
|
|
SHOW TABLES in db_name
|
||
SHOW TABLES in db_name like '*cust*'
|
||
SHOW TABLE EXTENDED in db_name like '*cust*'
|
||
Teradata |
SHOW TABLES
|
|
Oracle |
/* All tables in a database */ |
|
/* The table all_tab_cols contains information |
||
/* List all tables owned by the current user */ |
||
/* List all tables accessible to the current user */ |
||
MS SQL Server |
SELECT table_name |
|
Describe a table |
SQLite 3 |
.SCHEMA table_name
|
MySQL |
DESCRIBE table_name
|
|
Spark/Hive |
DESCRIBE [EXTENDED] table_name
|
|
Teradata |
HELP TABLE table_name
|
|
HELP COLUMN table_name.*
|
||
Oracle |
DESCRIBE table_name
|
|
MS SQL Server |
DESCRIBE table_name
|
|
Source code of a table |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SHOW 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, |
|
SELECT * |
||
SELECT * |
||
MS SQL Server |
|
|
List all tables accessible by the current user |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
|
|
Oracle |
SELECT * |
|
MS SQL Server |
|
|
List all tables in the system |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
|
|
Oracle |
SELECT * FROM dba_tables
|
|
MS SQL Server |
|
|
Create a table |
SQLite 3 |
CREATE TABLE IF NOT EXISTS queries ( |
MySQL |
CREATE TABLE IF NOT EXISTS queries ( |
|
Spark/Hive |
CREATE TABLE IF NOT EXISTS queries ( |
|
Teradata |
CREATE TABLE IF NOT EXISTS queries ( |
|
Oracle |
CREATE TABLE IF NOT EXISTS queries ( |
|
MS SQL Server |
CREATE TABLE IF NOT EXISTS queries ( |
|
Drop a table if exists |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
DROP TABEL IF EXISTS table_name
|
|
Oracle |
IF object_id(table_name) IS NOT NULL THEN |
|
MS SQL Server |
|
|
Limit number of returned rows |
SQLite 3 |
SELECT * |
MySQL |
SELECT * |
|
Spark/Hive |
SELECT * |
|
Teradata |
SELECT TOP 5 * |
|
Oracle |
SELECT * |
|
MS SQL Server |
SELECT TOP 5 * |
|
SELECT TOP 50 PERCENT * |
||
Randomly sample 100 rows |
SQLite 3 |
SELECT * |
MySQL |
|
|
Spark/Hive |
SELECT * |
|
SELECT * |
||
/* NOTE: the following does NOT work!!! |
||
Teradata |
SELECT * |
|
Oracle |
|
|
MS SQL Server |
|
|
Randomly sample rows with acceptance ratio 0.1 |
SQLite 3 |
/* |
MySQL |
|
|
Spark/Hive |
SELECT * FROM table |
|
Teradata |
SELECT * |
|
Oracle |
|
|
MS SQL Server |
|
|
Randomly sample buckets |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT * FROM table |
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Insert multiple rows in one statement |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
INSERT INTO tablel_name PARTITION ( |
|
Teradata |
INSERT INTO table_name ( |
|
Oracle |
INSERT INTO pager ( |
|
INSERT ALL |
||
MS SQL Server |
|
|
Update | SQLite 3 |
|
MySQL |
|
|
Spark |
- not supported as of Spark 2.4.5 |
|
Hive |
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Insert or Replace |
SQLite 3 |
REPLACE INTO phonebook ( |
MySQL |
|
|
Spark |
- not supported as of Spark 2.4.5 |
|
Hive |
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Upsert | SQLite 3 |
INSERT INTO phonebook ( |
MySQL |
|
|
Spark |
- not supported as of Spark 2.4.5 |
|
Hive |
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Delete | SQLite 3 |
|
MySQL |
|
|
Spark |
- not supported as of Spark 2.4.5 |
|
Hive |
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Insert | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
INSERT INTO some_table |
|
Oracle |
|
|
MS SQL Server |
|
|
Refresh Table Cache |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
REFRESH TABLE table_name
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Concatenate Strings |
SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT concat('Spark', 'SQL') |
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Substring | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
/* substr and substring are equivalent in Spark/Hive SQL */ |
|
left
|
||
right
|
||
Teradata |
substr
|
|
Oracle |
|
|
MS SQL Server |
|
|
trim | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
ltrim/rtrim/trim
|
|
Teradata |
trim
|
|
Oracle |
|
|
MS SQL Server |
|
|
substitute | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
replace/translate/regexp_replace
|
|
Teradata |
oreplace, otranslate
|
|
Oracle |
|
|
MS SQL Server |
|
|
length of string | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
char_length
|
|
Oracle |
|
|
MS SQL Server |
|
|
Index of substring | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
substring_index
|
|
Teradata |
position('de' IN 'abcdefg') |
|
regexp_instr('abc', 'a') |
||
Oracle |
|
|
MS SQL Server |
|
|
upper case | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
ucase/upper
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
decode base64 | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
unbase64
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
decode hex | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
unhex
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
generate an uuid | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
uuid
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
reverse a string | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
reverse
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
string matching | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
/* like is Case sensitive */ |
|
rlike
|
||
Teradata |
/* Like is case-insensitive by default. |
|
Oracle |
/*like is case sensitive*/ |
|
regexp_like(name, 'string$', 'i')
|
||
MS SQL Server |
|
|
shift string | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
shiftleft
|
|
shiftright
|
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
ocurrence of char | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
|
|
Teradata |
char_length('Teradata is Relational Database') - char_length(Oreplace('Teradata is Relational Database', 'a', ''))
|
|
Oracle |
|
|
MS SQL Server |
|
|
Cast to date | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT |
|
Teradata |
cast(str_col as date format 'YYYY-MM-DD')
|
|
Oracle |
|
|
MS SQL Server |
|
|
Cast to timestamp | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
SELECT
|
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Cast to UTC timestamp | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Cast to Unix timestamp | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract year | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract quarter | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract month | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract day | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
SELECT
|
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract minute | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Extract second | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Add days to a date | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
/* The + operator for date and int |
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Add months to a date | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Subtract days from a date | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
/* The - operator for date and int |
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
Diff between two dates | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT
|
|
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
truncate date | SQLite 3 |
|
MySQL |
|
|
Spark/Hive |
SELECT date_trunc('2015-03-05T09:32:05.359', 'YEAR') |
|
SELECT date_trunc('2015-03-05T09:32:05.359', 'MM') |
||
SELECT date_trunc('2015-03-05T09:32:05.359', 'DD') |
||
SELECT date_trunc('2015-03-05T09:32:05.359', 'HOUR') |
||
SELECT trunc('2009-02-12', 'MM') |
||
SELECT trunc('2015-10-27', 'YEAR') |
||
Teradata |
|
|
Oracle |
|
|
MS SQL Server |
|
|
sth | SQLite 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)
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