Oracle SQL Cheatsheet
Quick reference guide for Oracle database operations
Introduction to SQL
SQL (Structured Query Language) is the standard language for managing relational databases.
SQL Command Types
- DDL: Data Definition Language (CREATE, ALTER, DROP)
- DML: Data Manipulation Language (INSERT, UPDATE, DELETE)
- DQL: Data Query Language (SELECT)
- DCL: Data Control Language (GRANT, REVOKE)
- TCL: Transaction Control Language (COMMIT, ROLLBACK)
Basic Example
SELECT * FROM customers
WHERE country = 'USA'
ORDER BY last_name
FETCH FIRST 10 ROWS ONLY;
Setting Up Oracle Environment
Installation
- Oracle Database Express Edition (XE)
- SQL Developer (GUI)
- Oracle Instant Client
Online Tools
- Oracle Live SQL
- DB Fiddle (Oracle mode)
- SQL Fiddle (Oracle mode)
IDEs
- Oracle SQL Developer
- TOAD for Oracle
- PL/SQL Developer
- DBeaver
Schema and Table Operations
User/Schema Creation
CREATE USER my_user IDENTIFIED BY password;
GRANT CONNECT, RESOURCE TO my_user;
ALTER SESSION SET CURRENT_SCHEMA = my_user;
Table Creation
CREATE TABLE employees (
id NUMBER PRIMARY KEY,
name VARCHAR2(100) NOT NULL,
email VARCHAR2(100) UNIQUE,
hire_date DATE DEFAULT SYSDATE,
salary NUMBER(10,2) CHECK (salary > 0)
);
CREATE SEQUENCE emp_seq START WITH 1;
Common Data Types
- NUMBER: Numeric values
- VARCHAR2(n): Variable string
- DATE: Date and time
- TIMESTAMP: Precise date/time
- CHAR(n): Fixed-length string
- CLOB: Large text data
Basic SQL Queries
Filtering Data
SELECT product_name, price
FROM products
WHERE price > 100
AND category = 'Electronics'
ORDER BY price DESC
FETCH FIRST 5 ROWS ONLY;
Sorting & Limiting
SELECT * FROM customers
ORDER BY last_name ASC,
first_name ASC
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
Modifying Data (DML)
INSERT
INSERT INTO employees
(id, name, email, salary)
VALUES
(emp_seq.NEXTVAL, 'John Doe', 'john@example.com', 50000);
UPDATE
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
DELETE
DELETE FROM products
WHERE discontinued = 1;
Advanced Filtering
IN, BETWEEN, LIKE
SELECT * FROM products
WHERE category IN ('Electronics', 'Books')
AND price BETWEEN 50 AND 200
AND product_name LIKE '%Phone%';
NULL Handling
SELECT * FROM customers
WHERE phone IS NOT NULL;
UPDATE employees
SET manager_id = NULL
WHERE manager_id = 10;
Working with Multiple Tables
INNER JOIN
SELECT o.order_id, c.customer_name
FROM orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id;
LEFT JOIN
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
Self Join Example
SELECT e1.name AS employee,
e2.name AS manager
FROM employees e1
LEFT JOIN employees e2
ON e1.manager_id = e2.employee_id;
Aggregation and Grouping
Aggregate Functions
SELECT
COUNT(*) AS total_products,
AVG(price) AS avg_price,
MIN(price) AS min_price,
MAX(price) AS max_price,
SUM(stock) AS total_stock
FROM products;
GROUP BY & HAVING
SELECT department_id,
COUNT(*) AS emp_count,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5
ORDER BY avg_salary DESC;
Key Differences
- WHERE filters before grouping
- HAVING filters after grouping
- Non-aggregated columns in SELECT must be in GROUP BY
- Aggregate functions can't be used in WHERE
Subqueries
Single-Row Subquery
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
Multi-Row Subquery
SELECT product_name, price
FROM products
WHERE category_id IN (
SELECT category_id
FROM categories
WHERE department = 'Electronics'
);
Correlated Subquery
SELECT e.name, e.salary, e.department_id
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id
);
Views and Indexes
Creating Views
CREATE OR REPLACE VIEW high_paid_employees AS
SELECT id, name, salary, department
FROM employees
WHERE salary > 100000
WITH CHECK OPTION;
SELECT * FROM high_paid_employees;
Creating Indexes
CREATE INDEX idx_employee_name
ON employees(name);
CREATE UNIQUE INDEX idx_customer_email
ON customers(email);
CREATE INDEX idx_order_dates
ON orders(order_date, shipped_date);
Index Best Practices
- Index columns frequently used in WHERE, JOIN, ORDER BY
- Avoid over-indexing (slows down INSERT/UPDATE)
- Consider composite indexes for multi-column queries
- Monitor index usage and remove unused indexes
Basic SQL Functions
String Functions
SELECT
first_name || ' ' || last_name AS full_name,
UPPER(email) AS email_upper,
SUBSTR(phone, 1, 3) AS area_code,
LENGTH(address) AS addr_length
FROM customers;
Date Functions
SELECT
SYSDATE AS current_datetime,
TRUNC(order_date) AS order_date_only,
due_date - SYSDATE AS days_remaining,
ADD_MONTHS(hire_date, 12) AS anniversary
FROM orders;
Math Functions
SELECT
ROUND(price * 1.1, 2) AS price_with_tax,
ABS(discount) AS absolute_discount,
MOD(quantity, 10) AS remainder,
CEIL(weight) AS rounded_weight
FROM products;
Transactions and ACID
Transaction Control
SET TRANSACTION NAME 'FundsTransfer';
UPDATE accounts
SET balance = balance - 100
WHERE id = 123;
UPDATE accounts
SET balance = balance + 100
WHERE id = 456;
COMMIT;
-- or ROLLBACK if error occurs
ACID Properties
- Atomicity: All or nothing execution
- Consistency: Valid state transitions
- Isolation: Concurrent transactions don't interfere
- Durability: Committed changes persist
PL/SQL Procedures & Functions
Stored Procedure
CREATE OR REPLACE PROCEDURE update_salary(
emp_id IN NUMBER,
increase_percent IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = salary * (1 + increase_percent/100)
WHERE id = emp_id;
COMMIT;
END;
/
EXEC update_salary(101, 10);
PL/SQL Function
CREATE OR REPLACE FUNCTION get_employee_age(
emp_id IN NUMBER
) RETURN NUMBER
AS
age NUMBER;
BEGIN
SELECT FLOOR(MONTHS_BETWEEN(SYSDATE, birth_date)/12)
INTO age
FROM employees
WHERE id = emp_id;
RETURN age;
END;
/
SELECT name, get_employee_age(id) AS age
FROM employees;
Comprehensive Oracle SQL Cheatsheet & Examples Cheatsheet Reference
This Oracle SQL Cheatsheet & Examples cheatsheet on Nikhil Learn Hub collects syntax, commands, and practical snippets for quick revision. Learn Oracle SQL with commands, queries, syntax, examples, and best practices for beginners and developers.
Use the reference cards and examples above during coding sessions; return here instead of scattered searches when you need dependable reminders. Follow the Oracle / database learning roadmap when you want structured lessons beyond one-page lookups.
Quick lookup coverage
- Syntax, commands, and API signatures
- Copy-ready examples and common patterns
- Terminology for coursework and interviews
- Cross-links to the matching learning roadmap
How to study with this sheet
- Production debugging and tuning reminders
- Security, performance, or scale cautions
- Integration with adjacent stacks on this site
- Deeper study through tutorials and roadmaps
Who Should Use This Cheatsheet
Students, self-taught developers, and professionals who need fast Oracle SQL Cheatsheet & Examples lookups during labs, debugging, or interview revision should keep this page bookmarked.
Related Resources on Nikhil Learn Hub
- Oracle / database learning roadmapstructured learning path for the same technology
- Cheatsheets hubbrowse all quick-reference sheets
- Technology hubtutorials, roadmaps, and practice hubs