Skip to main content
Quick Reference

Oracle Cheatsheet

Oracle SQL on one page — identity columns, queries, FETCH FIRST, joins, analytic functions, indexes, transactions, and the NULL rules that differ from MySQL.

10 sections 10 tables 14 code snippets

Connect

SQL*Plus, password prompted

sqlplus app@localhost/FREEPDB1

What you own

SELECT table_name FROM user_tables;
DESCRIBE books;

The names

NameMeaning
PDBThe database you put tables in. Connect to the service, such as FREEPDB1 or XEPDB1, not CDB$ROOT
SchemaA user. books means app.books while you are connected as app
Port 1521The listener default
SQLclThe newer client. The command is sql, with the same connect string
SemicolonSends the statement. A slash on its own line ends a PL/SQL block
No USEThere is no statement that switches database the way MySQL does. Another schema is other.books

This page is Oracle SQL for 12c and later, including 19c and 23ai. It is not PL/SQL packages.

Create a table

Identity instead of auto-increment

CREATE TABLE books (
  id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  title VARCHAR2(200 CHAR) NOT NULL,
  author_id NUMBER,
  pages NUMBER(5) NOT NULL,
  price NUMBER(8, 2) NOT NULL,
  published DATE,
  CONSTRAINT pages_chk CHECK (pages > 0)
);

Column rules

PieceEffect
VARCHAR2, not VARCHARVARCHAR is reserved for a future change. Store text in VARCHAR2
VARCHAR2(200 CHAR)200 characters. Without CHAR, 200 is bytes, and one character may take several
NUMBER(8, 2)Exact number. 8 is the total digits, 2 of them after the point
GENERATED BY DEFAULTOmitting id fills it. You may still supply an id
GENERATED ALWAYSRejects a supplied id. ON NULL also fills an explicit NULL
DATEDate and time, down to a second. TIMESTAMP keeps fractions. A date literal is DATE '2026-11-28'

Insert

Leave the id out

INSERT INTO books (title, pages, price)
VALUES ('Dune', 412, 18.50);

INSERT INTO books (title, pages, price)
VALUES ('Neuromancer', 271, 14.00);

What gets stored

You writeOracle stores
No idThe next identity value
An explicit id on BY DEFAULTStored as given. The sequence is not moved past it, so a later generated id can collide
'' in a VARCHAR2NULL. A zero-length string is null
A missing NOT NULL columnThe insert fails
seq.NEXTVALThe old way, still valid. Identity uses a sequence behind the curtain
Several rowsOn 19c, one INSERT per row, or INSERT ALL. A multi-row VALUES list arrives in 23ai

Select

Top 10 after the sort

SELECT title, pages
FROM books
WHERE pages >= 300
ORDER BY pages DESC
FETCH FIRST 10 ROWS ONLY;

An expression needs DUAL

SELECT SYSDATE FROM dual;

SELECT title
FROM books
WHERE published IS NULL;

Read rules

FormMeaning
FETCH FIRST 10 ROWS ONLYThe row limit. It belongs after ORDER BY. There is no LIMIT
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLYSkip 20, then return 10. Put a unique column last in ORDER BY or pages can skip and repeat
DUALA one-row table. SELECT SYSDATE FROM dual is how you select an expression
NVL(price, 0)A stand-in when the value is NULL. COALESCE takes more than two arguments
IS NULLThe null test. = NULL and = '' match nothing
Unquoted namesFolded to uppercase. title and TITLE are the same column

Update & delete

Change a price

UPDATE books
SET price = 19
WHERE title = 'Dune';

Remove rows, then keep it

DELETE FROM books
WHERE pages < 100;

COMMIT;

Write rules

StatementUntil you commit
UPDATE / DELETEVisible in your session, invisible to others, and gone if the session dies
COMMITMakes the open transaction durable and releases its locks
ROLLBACKThrows the open transaction away. It cannot undo a commit
AutocommitOff in SQL*Plus and SQLcl. A statement does not commit itself
No WHEREEvery row. The statement still waits for COMMIT
TRUNCATE TABLE booksEmpties the table immediately. It is DDL, so it commits and cannot be rolled back

Joins

Orders with their book

SELECT o.id, b.title, o.amount
FROM orders o
INNER JOIN books b ON b.id = o.book_id;

SELECT o.id, b.title
FROM orders o
LEFT JOIN books b ON b.id = o.book_id;

Which rows survive

JoinKeeps
INNER JOINOnly rows that match on both sides
LEFT JOINEvery left row. Missing right-side columns are NULL
ONThe match. Put it here, not in WHERE, or a left join collapses
(+)The old outer-join marker. It sits on the optional side and is easy to get wrong
Same column namePrefix it. id alone is ambiguous once two tables are in the query
A filter on the right table in WHERETurns the left join into an inner join, because NULL fails the test

Analytic functions

Rank inside each author

SELECT title, price,
       ROW_NUMBER() OVER (
         PARTITION BY author_id
         ORDER BY price DESC, id DESC
       ) AS price_rank
FROM books;

Window, not group

PieceEffect
OVERComputes a value per row. The row stays. GROUP BY collapses rows
PARTITION BY author_idRestarts the numbering for each author
ORDER BY inside OVERThe order of the number. A unique tiebreaker such as id keeps it stable
ROW_NUMBER1, 2, 3 with no ties
RANK / DENSE_RANKRANK leaves a gap after a tie. DENSE_RANK does not
Filter the rankA window value cannot appear in WHERE. Wrap the query and filter the outer query

Indexes & foreign keys

Index the column you look up

CREATE INDEX idx_books_title ON books (title);

CREATE INDEX idx_orders_book ON orders (book_id);

ALTER TABLE orders
  ADD CONSTRAINT fk_orders_book
  FOREIGN KEY (book_id) REFERENCES books (id);

What Oracle does not add for you

KeyDetail
PRIMARY KEYUnique, and NOT NULL. Oracle builds a unique index for it
CREATE INDEXSpeeds equality, range, and a matching sort. A composite index serves its leftmost columns
Foreign keyDoes not create an index on book_id. Add that index yourself
Why the child index mattersA delete of a parent otherwise scans the child table and holds a lock longer
UNIQUERejects a second non-null duplicate. Several NULLs are allowed
EXPLAIN PLANShows the plan. A full table scan is TABLE ACCESS FULL

Transactions

Two writes, one outcome

UPDATE books SET price = price - 1 WHERE id = 1;
INSERT INTO orders (book_id, amount) VALUES (1, 17.50);
COMMIT;

When the work sticks

FactWhy it matters
DML is one transactionUPDATE and INSERT above commit together, or ROLLBACK drops both
DDL commitsCREATE, ALTER, DROP, and TRUNCATE commit the open transaction first, then commit themselves
SELECT ... FOR UPDATELocks the selected rows until you commit or roll back
ReadersAnother session does not see your uncommitted rows
A dead sessionUncommitted work is rolled back. Locks go with it
SAVEPOINT markRoll back to a named point without ending the whole transaction

Interview traps

Traps worth memorising

TrapWhat happensPrefer
LIMIT 10Not valid Oracle SQLFETCH FIRST 10 ROWS ONLY after ORDER BY
AUTO_INCREMENTNot an Oracle column attributeGENERATED BY DEFAULT AS IDENTITY
WHERE title = ''Matches nothing. A zero-length string is NULL, and = never succeeds for NULLIS NULL
NOT NULL and an empty stringThe insert is rejectedA real value, or a nullable column
'a' || NULLThe result is a. Concatenation is the operator that does not turn NULL into NULLDo not use || as a null test
WHERE ROWNUM <= 10 then ORDER BYYou sort 10 arbitrary rows, not the top 10ORDER BY and then FETCH FIRST
VARCHAR2(200)200 bytes by default. A multibyte title can overflowVARCHAR2(200 CHAR)
Text longer than 4000 bytesRejected unless the database uses extended stringsCLOB for long text
A foreign key with no child indexParent deletes scan and lock the child tableAn index on the referencing column
WHERE published >= '01-JAN-2026'Depends on NLS_DATE_FORMAT and can miss rows or raise an errorA date literal, DATE '2026-01-01'
Quoted "Title"A different column from TITLE. You must quote it foreverUnquoted names
CREATE TABLE after an UPDATEThe update is committed. DDL does that before it runsCOMMIT yourself, and keep DDL out of the transaction

The null test

WHERE title IS NULL

The date literal

WHERE published >= DATE '2026-01-01'

How to use this page: connect to a PDB, create books with an identity column, and insert one row without an id. Then FETCH FIRST the longest books, and COMMIT after the update.