Oracle Syntax

Updated · 5 min read
mediumPostgreSQL 18sqlsyntaxreferenceoracleassessment

Every task from the syntax reference, written for Oracle, on one page. Use it as a quick lookup when you know what you want to do and need the Oracle spelling. Rows marked standard use SQL that other databases share. For the other databases, see the topic pages linked from the SQL Syntax by Database.

How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.

Querying

TaskOracle
First 10 rowsSELECT * FROM t ORDER BY id FETCH FIRST 10 ROWS ONLY;standard
Skip 20 rows, take 10SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;standard
Case-insensitive matchSELECT * FROM t WHERE LOWER(name) LIKE 'nav%';dialect
Rows with a missing valueSELECT * FROM t WHERE email IS NULL;standard
Groups with a conditionSELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1;standard
Full outer joinSELECT * FROM a FULL JOIN b ON b.id = a.id;standard
Rows in one table but not anotherSELECT id FROM a MINUS SELECT id FROM b;standard
Quote a name with a spaceSELECT "first name" FROM t;standard

All five databases side by side: Syntax: Querying.

Changing Data

TaskOracle
Insert several rowsINSERT INTO t (id, name) VALUES (1, 'a'), (2, 'b'); -- 23ai+standard
Get the new id backINSERT INTO t (name) VALUES ('a') RETURNING id INTO :new_id;dialect
Update from another tableMERGE INTO t USING s ON (s.id = t.id) WHEN MATCHED THEN UPDATE SET t.name = s.name;dialect
Insert or update (upsert)MERGE INTO t USING (SELECT 1 id, 'a' name FROM dual) s ON (t.id = s.id) WHEN MATCHED THEN UPDATE SET t.name = s.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);dialect
Delete all rows quicklyTRUNCATE TABLE t;dialect
Copy rows into a new tableCREATE TABLE t2 AS SELECT * FROM t;dialect
Load a CSV fileSQL*Loader (sqlldr) or an external tabledialect

All five databases side by side: Syntax: Changing Data.

Tables and Keys

TaskOracle
Auto-numbered primary keyid NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEYstandard
Money columnfee_usd NUMBER(8,2)standard
Text columnname VARCHAR2(40)standard
True/false columnis_active BOOLEAN -- 23ai+; NUMBER(1) beforestandard
Foreign key with cascadestudent_id NUMBER REFERENCES students (student_id) ON DELETE CASCADEstandard
Check constraintCHECK (fee_usd > 0)standard
Create table if missingCREATE TABLE IF NOT EXISTS t (id NUMBER); -- 23ai+dialect
Add a columnALTER TABLE t ADD (note VARCHAR2(4000));standard
Rename a columnALTER TABLE t RENAME COLUMN note TO memo;standard
Create an indexCREATE INDEX t_name_idx ON t (name);dialect
Create or replace a viewCREATE OR REPLACE VIEW v AS SELECT id FROM t;dialect

All five databases side by side: Syntax: Tables and Keys.

Functions

TaskOracle
Join textfirst_name || ' ' || last_namestandard
Text lengthLENGTH(name)dialect
Part of a stringSUBSTR(name, 1, 3)standard
Replace NULLCOALESCE(email, 'none')standard
Round to centsROUND(x, 2)dialect
Today's dateTRUNC(SYSDATE)standard
Add 30 daysd + 30dialect
Days between datesd2 - d1dialect
Year of a dateEXTRACT(YEAR FROM d)standard
Format a dateTO_CHAR(d, 'YYYY-MM-DD')dialect
Convert text to a numberCAST('450.00' AS NUMBER)standard
If / elseCASE WHEN x > 0 THEN 'yes' ELSE 'no' ENDstandard
Join values from many rowsLISTAGG(name, ', ') WITHIN GROUP (ORDER BY name)standard

All five databases side by side: Syntax: Functions.

Advanced SQL

TaskOracle
Common table expressionWITH x AS (SELECT 1 AS n FROM dual) SELECT n FROM x;standard
Recursive CTEWITH n(x) AS (SELECT 1 FROM dual UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;standard
Rank rowsRANK() OVER (ORDER BY fee DESC)standard
Running totalSUM(x) OVER (ORDER BY d)standard
Conditional countCOUNT(CASE WHEN method = 'card' THEN 1 END)standard
SubtotalsGROUP BY ROLLUP (a, b)standard
Read a JSON field as textJSON_VALUE(data, '$.name')dialect
Full-text searchCONTAINS(body, 'join') > 0 -- needs an Oracle Text indexdialect
A function languageCREATE FUNCTION f RETURN NUMBER IS BEGIN RETURN 1; END;dialect

All five databases side by side: Syntax: Advanced SQL.

Administration

TaskOracle
Start a transactionImplicit: the first change starts onestandard
SavepointSAVEPOINT s; ROLLBACK TO SAVEPOINT s;standard
Set the isolation levelSET TRANSACTION ISOLATION LEVEL SERIALIZABLE;standard
Show a query planEXPLAIN PLAN FOR SELECT ...; SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);dialect
Create a loginCREATE USER app IDENTIFIED BY secret;dialect
Grant read accessGRANT SELECT ON payments TO app;standard
Back up a databaseexpdp, or RMAN for physical backupsdialect
Parameter placeholder in app code:name (most drivers); ? (JDBC)dialect
Server versionSELECT banner FROM v$version;dialect

All five databases side by side: Syntax: Administration.

Was this tutorial helpful?