Oracle Syntax
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
| Task | Oracle | |
|---|---|---|
| First 10 rows | SELECT * FROM t ORDER BY id FETCH FIRST 10 ROWS ONLY; | standard |
| Skip 20 rows, take 10 | SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY; | standard |
| Case-insensitive match | SELECT * FROM t WHERE LOWER(name) LIKE 'nav%'; | dialect |
| Rows with a missing value | SELECT * FROM t WHERE email IS NULL; | standard |
| Groups with a condition | SELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1; | standard |
| Full outer join | SELECT * FROM a FULL JOIN b ON b.id = a.id; | standard |
| Rows in one table but not another | SELECT id FROM a MINUS SELECT id FROM b; | standard |
| Quote a name with a space | SELECT "first name" FROM t; | standard |
All five databases side by side: Syntax: Querying.
Changing Data
| Task | Oracle | |
|---|---|---|
| Insert several rows | INSERT INTO t (id, name) VALUES (1, 'a'), (2, 'b'); -- 23ai+ | standard |
| Get the new id back | INSERT INTO t (name) VALUES ('a') RETURNING id INTO :new_id; | dialect |
| Update from another table | MERGE 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 quickly | TRUNCATE TABLE t; | dialect |
| Copy rows into a new table | CREATE TABLE t2 AS SELECT * FROM t; | dialect |
| Load a CSV file | SQL*Loader (sqlldr) or an external table | dialect |
All five databases side by side: Syntax: Changing Data.
Tables and Keys
| Task | Oracle | |
|---|---|---|
| Auto-numbered primary key | id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY | standard |
| Money column | fee_usd NUMBER(8,2) | standard |
| Text column | name VARCHAR2(40) | standard |
| True/false column | is_active BOOLEAN -- 23ai+; NUMBER(1) before | standard |
| Foreign key with cascade | student_id NUMBER REFERENCES students (student_id) ON DELETE CASCADE | standard |
| Check constraint | CHECK (fee_usd > 0) | standard |
| Create table if missing | CREATE TABLE IF NOT EXISTS t (id NUMBER); -- 23ai+ | dialect |
| Add a column | ALTER TABLE t ADD (note VARCHAR2(4000)); | standard |
| Rename a column | ALTER TABLE t RENAME COLUMN note TO memo; | standard |
| Create an index | CREATE INDEX t_name_idx ON t (name); | dialect |
| Create or replace a view | CREATE OR REPLACE VIEW v AS SELECT id FROM t; | dialect |
All five databases side by side: Syntax: Tables and Keys.
Functions
| Task | Oracle | |
|---|---|---|
| Join text | first_name || ' ' || last_name | standard |
| Text length | LENGTH(name) | dialect |
| Part of a string | SUBSTR(name, 1, 3) | standard |
| Replace NULL | COALESCE(email, 'none') | standard |
| Round to cents | ROUND(x, 2) | dialect |
| Today's date | TRUNC(SYSDATE) | standard |
| Add 30 days | d + 30 | dialect |
| Days between dates | d2 - d1 | dialect |
| Year of a date | EXTRACT(YEAR FROM d) | standard |
| Format a date | TO_CHAR(d, 'YYYY-MM-DD') | dialect |
| Convert text to a number | CAST('450.00' AS NUMBER) | standard |
| If / else | CASE WHEN x > 0 THEN 'yes' ELSE 'no' END | standard |
| Join values from many rows | LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name) | standard |
All five databases side by side: Syntax: Functions.
Advanced SQL
| Task | Oracle | |
|---|---|---|
| Common table expression | WITH x AS (SELECT 1 AS n FROM dual) SELECT n FROM x; | standard |
| Recursive CTE | WITH n(x) AS (SELECT 1 FROM dual UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; | standard |
| Rank rows | RANK() OVER (ORDER BY fee DESC) | standard |
| Running total | SUM(x) OVER (ORDER BY d) | standard |
| Conditional count | COUNT(CASE WHEN method = 'card' THEN 1 END) | standard |
| Subtotals | GROUP BY ROLLUP (a, b) | standard |
| Read a JSON field as text | JSON_VALUE(data, '$.name') | dialect |
| Full-text search | CONTAINS(body, 'join') > 0 -- needs an Oracle Text index | dialect |
| A function language | CREATE FUNCTION f RETURN NUMBER IS BEGIN RETURN 1; END; | dialect |
All five databases side by side: Syntax: Advanced SQL.
Administration
| Task | Oracle | |
|---|---|---|
| Start a transaction | Implicit: the first change starts one | standard |
| Savepoint | SAVEPOINT s; ROLLBACK TO SAVEPOINT s; | standard |
| Set the isolation level | SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; | standard |
| Show a query plan | EXPLAIN PLAN FOR SELECT ...; SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY); | dialect |
| Create a login | CREATE USER app IDENTIFIED BY secret; | dialect |
| Grant read access | GRANT SELECT ON payments TO app; | standard |
| Back up a database | expdp, or RMAN for physical backups | dialect |
| Parameter placeholder in app code | :name (most drivers); ? (JDBC) | dialect |
| Server version | SELECT banner FROM v$version; | dialect |
All five databases side by side: Syntax: Administration.
Was this tutorial helpful?