Welcome to Coding Codex
Your comprehensive guide to essential programming functions, methods, and patterns across multiple languages
Python
A versatile, beginner-friendly language with clean, readable syntax. Covers essential built-in functions (print, len, range, map, filter), object-oriented programming with classes and inheritance, powerful data structures including lists, dictionaries, sets, and tuples, file I/O, exception handling, and key standard library modules like os, json, re, and datetime.
R
The go-to language for statistical computing and data science. Covers essential inspection functions (str, summary, head), vector and data frame operations, statistical analysis including mean, median, variance, and correlation, the apply function family for functional programming, file I/O, and popular ecosystem packages like dplyr, ggplot2, tidyr, and lubridate.
HTML/CSS
The foundational technologies of every webpage. Learn HTML5 semantic elements (header, nav, article, section), form controls, inline and block elements, CSS selectors and specificity, the box model (margin, padding, border), positioning strategies, modern layout systems including Flexbox and CSS Grid, and responsive design techniques using media queries for all screen sizes.
JavaScript
The language of the web, powering interactive front-end and server-side Node.js applications. Covers modern ES6+ syntax (let/const, arrow functions, destructuring), comprehensive array and string methods, DOM manipulation for dynamic UIs, event handling, asynchronous programming with Promises and async/await, the Fetch API for HTTP requests, JSON serialization, and browser localStorage APIs.
Java
A robust, statically-typed, platform-independent object-oriented language widely used in enterprise, Android, and back-end development. Covers primitive and reference data types, rich string manipulation methods, arrays and the Java Collections Framework (ArrayList, HashMap, HashSet), core OOP principles like encapsulation, inheritance, and polymorphism, interfaces, annotations, and structured exception handling.
T-SQL
Microsoft SQL Server's powerful dialect of SQL for querying, transforming, and managing relational data. Covers SELECT, INSERT, UPDATE, DELETE, and MERGE statements; all JOIN types for combining tables; GROUP BY with HAVING for aggregations; powerful window functions (ROW_NUMBER, RANK, LAG, LEAD); built-in string and date manipulation functions; DDL for table and constraint management; and control-flow constructs.
PL/SQL
Oracle's procedural extension to SQL for writing complex, high-performance database logic that runs directly on the server. Covers anonymous PL/SQL blocks, variable declaration and scoping, control structures (IF, CASE, loops), explicit and implicit cursors for row-by-row processing, stored procedures and functions, triggers for automated responses to DML events, packages for modular code organization, exception handling, and bulk operations for efficient batch data processing.
☁️ Modern Cloud & Tech Stacks
Explore comprehensive guides to industry-leading cloud platforms and big data technologies:
Apache Ecosystem
A powerful suite of open-source big data frameworks. Hadoop provides distributed storage via HDFS and batch processing via MapReduce; Spark delivers in-memory, high-speed computation up to 100x faster than MapReduce; Kafka enables real-time, fault-tolerant event streaming at massive scale; and Hive brings SQL-like analytics over distributed datasets stored in HDFS.
Microsoft Azure
Microsoft's enterprise-grade cloud platform with 200+ services spanning compute (VMs, App Service, Functions, AKS), storage (Blob, Data Lake), managed databases (SQL, Cosmos DB, Synapse), AI/ML (Azure ML, Cognitive Services), identity management (Entra ID), and deep hybrid-cloud integration with on-premises Microsoft infrastructure and enterprise Active Directory environments.
Google Cloud Platform
Google's cloud platform built on the same global infrastructure powering Search, YouTube, and Gmail. Specializes in data analytics with BigQuery's serverless petabyte-scale SQL, containerized workloads with GKE, real-time streaming with Pub/Sub and Dataflow, and cutting-edge AI/ML capabilities via Vertex AI, pre-trained APIs, and TPU-accelerated model training.
Amazon AWS
The world's most comprehensive and widely adopted cloud platform with 200+ services. Covers compute (EC2, Lambda, ECS/EKS), object storage (S3), managed databases (RDS, Aurora, DynamoDB), big data analytics (EMR, Redshift, Kinesis), networking and security (VPC, IAM, WAF), machine learning (SageMaker), and end-to-end DevOps toolchains (CodePipeline, CodeBuild, CloudFormation).
🚀 Getting Started
📚 Navigate
Use the collapsible sidebar on the left to switch instantly between programming languages and cloud platforms. Each section is organized into clearly labeled category tables covering the most commonly used functions, methods, syntax patterns, and concepts — all formatted for rapid lookup without having to dig through documentation.
🖱️ Click to Copy
Click any highlighted code snippet in the tables to instantly copy it to your clipboard. A "✓ Copied!" confirmation message will briefly appear so you always know the copy was successful. This works across all browsers using the modern Clipboard API with a fallback for older environments.
⌨️ Keyboard Shortcuts
- ← → Navigate between tabs sequentially
- 1-7 Jump directly to a specific language tab
- Esc Close search results panel
- Click on
codeto copy it to clipboard
💡 About This Reference
This reference is designed for developers, students, and data professionals who need fast, reliable access to common programming patterns, functions, and platform concepts. Whether you are learning a new language from scratch, switching between languages on a project, or simply need a quick reminder of a specific method's signature or behavior — you will find clearly organized tables with accurate, elaborated descriptions of the most frequently used features across all covered languages and technologies.
The reference is structured to minimize friction: no sign-up, no ads, no page reloads. Everything is available instantly from the sidebar. Each language section includes:
- ✓ Core syntax and primitive data types with type-specific nuances explained
- ✓ String, array, and collection manipulation methods with behavioral details
- ✓ Built-in functions with parameter descriptions and practical usage notes
- ✓ Object-oriented programming concepts including classes, inheritance, and interfaces
- ✓ File I/O and exception handling patterns for robust, production-ready code
- ✓ Important standard libraries, frameworks, and ecosystem packages
- ✓ Best practices, gotchas, and performance considerations where relevant
Ready to explore?
Select a programming language or cloud tech stack from the sidebar to get started. Use the search bar at the top to instantly find any function, method, or concept across all sections — results appear as you type.
Python Quick Reference
Built-in Functions
| Function / Syntax | Description |
|---|---|
print(x) | Outputs text or values to the standard console. Accepts multiple comma-separated arguments. Use sep to change the separator (default: space) and end to change the line terminator (default: newline). E.g., print(a, b, sep=', '). |
len(x) | Returns the number of items in a container: character count for strings, element count for lists/tuples, key count for dictionaries, and item count for sets. Raises TypeError on objects that don't define __len__. |
type(x) | Returns the class/type of any Python object. E.g., type(42) → <class 'int'>. Useful for debugging. For type-checking in code, prefer isinstance() as it handles subclasses correctly. |
range(n) | Generates an immutable sequence of integers from 0 up to (but not including) n. Memory-efficient — the full list is never constructed in memory. Commonly used in for loops. E.g., range(5) → 0, 1, 2, 3, 4. |
range(a, b, step) | Creates a sequence from a up to (but not including) b, incrementing by step. A negative step produces descending sequences. E.g., range(10, 0, -2) → 10, 8, 6, 4, 2. Step cannot be zero. |
int(), float(), str() | Explicit type conversion functions. int() parses integers from strings or truncates floats; float() converts to floating-point; str() converts any object to its string representation. Raises ValueError on invalid input. |
bool(x) | Converts any value to its boolean equivalent. Returns False for: 0, 0.0, empty string, empty list/dict/set/tuple, and None. Returns True for everything else. Useful for truthiness-based conditional checks. |
abs(x) | Returns the non-negative magnitude of a number. Works with integers, floats, and complex numbers. For complex numbers, returns the magnitude (Euclidean distance from zero). E.g., abs(-7) → 7, abs(3+4j) → 5.0. |
round(x, n) | Rounds x to n decimal places (defaults to 0 for nearest integer). Uses banker's rounding (round half to even) to avoid statistical bias. E.g., round(2.5) → 2, round(3.5) → 4, round(3.14159, 2) → 3.14. |
max(iter) | Returns the largest item in an iterable or the largest of multiple arguments. Accepts an optional key function for custom comparisons. E.g., max(words, key=len) finds the longest word. Returns the item itself, not its index. |
min(iter) | Returns the smallest item in an iterable or among multiple arguments. Accepts an optional key function for custom ordering logic. Raises ValueError if the iterable is empty and no default is provided. |
sum(iter) | Returns the arithmetic sum of all numeric items in an iterable. The optional start parameter sets the initial value (default: 0). Does not work on strings — use ''.join(list) instead. E.g., sum([1,2,3], 10) → 16. |
sorted(iter) | Returns a new sorted list from an iterable without modifying the original (non-destructive). Accepts key function and reverse=True parameters. Uses TimSort — a stable algorithm that preserves the original order of equal elements. |
reversed(iter) | Returns a lazy reverse iterator over a sequence without creating a copy in memory. Wrap in list() to materialize. Works on sequences with defined __reversed__ or __len__. More memory-efficient than slicing with [::-1]. |
enumerate(iter) | Wraps an iterable and yields (index, value) tuples during iteration. Eliminates the need for manual counter variables. The optional start parameter sets the initial index. E.g., enumerate(['a','b'], start=1) → (1,'a'), (2,'b'). |
zip(a, b) | Pairs elements from multiple iterables into tuples and returns a lazy zip object. Stops at the shortest iterable by default. Use itertools.zip_longest() to continue past the shorter sequence. E.g., zip([1,2],[3,4]) → (1,3), (2,4). |
map(fn, iter) | Applies a function to every item of an iterable and returns a lazy map object. More memory-efficient than list comprehension for large datasets. Convert to list with list(map(...)). Accepts multiple iterables for functions with multiple arguments. |
filter(fn, iter) | Returns a lazy filter object containing only elements for which the function returns True. Wrap in list() to materialize. Passing None as the function filters out all falsy values (0, None, empty strings, etc.). |
isinstance(x, T) | Tests whether an object is an instance of a class or a tuple of classes. Returns True for subclasses as well, making it inheritance-aware. Preferred over direct type(x) == T comparisons for robust type checking in production code. |
input(prompt) | Pauses execution, displays the optional prompt string, waits for the user to type a line, and returns it as a string (excluding the trailing newline). Always returns a str — wrap with int() or float() when numeric input is expected. |
open(file, mode) | Opens a file and returns a file object. Modes: 'r' (read), 'w' (write/truncate), 'a' (append), 'b' (binary), '+' (read+write). Always use with the with statement to guarantee the file is automatically closed even if an exception occurs. |
dir(x) | Returns a sorted list of all attributes and methods available on an object. Without arguments, lists all names in the current local scope. Essential for interactive exploration of unfamiliar objects, modules, or APIs in the REPL or during debugging. |
OOP Essentials
| Function / Syntax | Description |
|---|---|
class Foo: | Defines a new class named Foo. Classes are blueprints for creating objects. By convention, class names use CapitalizedWords (PascalCase). A class groups data (attributes) and behavior (methods) into a single unit. |
def __init__(self, x): | The constructor method, automatically called when an instance is created. self refers to the newly created instance. Use this method to initialize instance attributes. Every instance gets its own copy of instance variables defined here. |
def __str__(self): | Defines the human-readable string representation of an object. Called by str(obj) and print(obj). Should return a descriptive, user-friendly string. If not defined, falls back to __repr__. |
def __repr__(self): | Defines the official developer-facing string representation. Called by repr(obj) and displayed in the REPL. Should ideally return a string that could recreate the object (e.g., Foo(x=5)). Used as a fallback when __str__ is missing. |
def __len__(self): | Allows the built-in len() function to work on instances of this class. Must return a non-negative integer. Implementing this makes the object feel native and compatible with Python idioms like if not obj. |
def __eq__(self, other): | Defines the behavior of the == equality operator. By default, objects compare by identity (same memory address). Override to compare by value. Should return True, False, or NotImplemented. |
@property | Decorator that turns a method into a read-only attribute accessible without parentheses. Allows computed or validated attribute access. Pair with @name.setter and @name.deleter for full property control. |
@staticmethod | Decorator for a method that receives neither the instance (self) nor the class (cls). Behaves like a plain function but lives in the class namespace for organizational purposes. Cannot access or modify class or instance state. |
@classmethod | Decorator for a method that receives the class itself as the first argument (cls) rather than the instance. Commonly used to create alternative constructors (factory methods). Can access and modify class-level state. |
class Bar(Foo): | Creates a subclass Bar that inherits all attributes and methods from parent class Foo. Bar can override inherited methods and add its own. Python supports multiple inheritance, allowing class Bar(Foo, Baz):. |
super().__init__(...) | Calls the parent class's __init__ method from within a subclass, ensuring the parent is properly initialized before adding subclass-specific setup. Essential in cooperative multiple inheritance to properly follow the MRO (Method Resolution Order). |
String Methods
| Function / Syntax | Description |
|---|---|
s.upper() / s.lower() | Returns a new string with all characters converted to uppercase or lowercase respectively. Does not modify the original string (strings are immutable in Python). Locale-independent ASCII conversion. Use casefold() for aggressive lowercasing in case-insensitive comparisons. |
s.strip() | Returns a copy of the string with leading and trailing whitespace removed (spaces, tabs, newlines). Pass a string argument to strip specific characters instead. lstrip() strips only the left side; rstrip() strips only the right side. |
s.split(sep) | Splits the string into a list of substrings using sep as the delimiter. If sep is not specified, splits on any whitespace and removes empty strings. The optional maxsplit argument limits the number of splits performed. |
sep.join(list) | Joins all string elements of an iterable into a single string, inserting sep between each element. The separator string is called on, not passed in. E.g., ', '.join(['a','b','c']) → 'a, b, c'. All elements must be strings. |
s.replace(old, new) | Returns a copy of the string with all occurrences of old replaced by new. An optional count argument limits the number of replacements. The original string is not modified. Case-sensitive by default. |
s.find(sub) | Returns the lowest index where sub is found within the string. Returns -1 if not found (unlike index() which raises ValueError). Optionally accepts start and end arguments to search within a slice. |
s.startswith(p) | Returns True if the string begins with the specified prefix p. Accepts a tuple of prefixes to check multiple at once. Supports optional start and end arguments to check within a slice of the string. |
s.endswith(s) | Returns True if the string ends with the specified suffix. Accepts a tuple of suffixes to check multiple alternatives simultaneously. Useful for file extension checks, e.g., filename.endswith(('.jpg', '.png')). |
s.format(...) | Formats the string by replacing {} or named {name} placeholders with provided arguments. Supports format specifiers for alignment, precision, and type. E.g., '{:.2f}'.format(3.14159) → '3.14'. Largely superseded by f-strings. |
f"{var}" | F-strings (formatted string literals, introduced in Python 3.6) embed expressions directly inside string literals. Evaluated at runtime. Support full Python expressions, method calls, and format specifiers inside braces. E.g., f"Pi is {3.14159:.2f}" → 'Pi is 3.14'. The preferred interpolation method. |
s.count(sub) | Returns the number of non-overlapping occurrences of substring sub within the string. Optional start and end arguments restrict the search range. Case-sensitive. E.g., 'banana'.count('an') → 2. |
s.isdigit() | Returns True only if all characters in the string are decimal digit characters and the string is not empty. Returns False for floats (due to the decimal point), negative numbers (due to the minus sign), or empty strings. |
s.isalpha() | Returns True only if all characters in the string are alphabetic (letters) and the string is non-empty. Returns False if the string contains spaces, digits, or special characters. Use isalnum() to allow digits as well. |
s[a:b] | Extracts a substring from index a (inclusive) up to index b (exclusive). Negative indices count from the end. Omit a to start from the beginning; omit b to go to the end. E.g., 'hello'[1:3] → 'el'. |
s[::-1] | Reverses the entire string using extended slice notation. The -1 step means iterate backwards. This is idiomatic Python for string reversal and works on any sequence (lists, tuples). More concise than reversed() for strings. |
List Methods
| Function / Syntax | Description |
|---|---|
list.append(x) | Adds a single element x to the end of the list, modifying it in place. O(1) amortized time complexity. Does not return a new list — returns None. To add multiple items at once, use extend() instead. |
list.extend(iter) | Appends all elements from an iterable (list, tuple, string, generator, etc.) to the end of the list in place. Equivalent to list += iter. Unlike append(), does not add the iterable as a single element — it unpacks it. |
list.insert(i, x) | Inserts element x before the element at index i. If i is greater than the list length, x is appended to the end. O(n) operation because existing elements must be shifted. For adding to the end, append() is faster. |
list.remove(x) | Removes the first occurrence of element x from the list. Raises ValueError if x is not found. Searches from left to right. To remove by index instead, use pop(i) or the del statement. |
list.pop(i) | Removes and returns the element at index i. Without an argument, removes and returns the last element (O(1)). Removing from other positions is O(n) due to element shifting. Raises IndexError if the list is empty or index is out of range. |
list.index(x) | Returns the index of the first occurrence of element x. Raises ValueError if x is not found. Optional start and end arguments restrict the search range. For existence checking, use the in operator instead. |
list.count(x) | Returns the number of times element x appears in the list. Performs a linear search — O(n). Returns 0 if x is not present (does not raise an error). Useful for frequency counting without additional imports. |
list.sort() | Sorts the list in place (modifies the original list, returns None). Uses TimSort — stable, O(n log n). Accepts key function for custom sort order and reverse=True for descending order. For a non-destructive sort, use sorted(list). |
list.reverse() | Reverses the elements of the list in place (modifies the original, returns None). O(n) time. For a non-destructive reversed copy, use list[::-1] slicing or list(reversed(list)). |
list.copy() | Returns a shallow copy of the list — a new list object containing references to the same elements. Equivalent to list[:]. For nested structures, changes to nested objects are still reflected in the copy. Use copy.deepcopy() for truly independent copies. |
list.clear() | Removes all elements from the list, leaving an empty list. Equivalent to del list[:]. The list object itself remains in memory. Unlike reassigning to [], clear() modifies the list in place, which matters if other variables reference the same list. |
[x for x in iter] | List comprehension — a concise, Pythonic syntax for creating a new list by applying an expression to each element of an iterable. Equivalent to a for loop with append(), but typically faster and more readable. E.g., [x**2 for x in range(5)] → [0,1,4,9,16]. |
[x for x in iter if cond] | Filtered list comprehension — only includes elements for which the condition evaluates to True. The filter condition comes after the for clause. E.g., [x for x in range(10) if x % 2 == 0] → [0,2,4,6,8]. More readable than nested filter() and map() calls. |
Dictionary Methods
| Function / Syntax | Description |
|---|---|
d[key] | Accesses the value associated with key. Raises KeyError if the key does not exist. Use for direct access when you are certain the key is present. For safe access, prefer d.get(key) which returns None instead of raising an error. |
d.get(key, default) | Returns the value for key if it exists, otherwise returns default (defaults to None if not specified). Never raises KeyError. The idiomatic, safe way to access dictionary values when the key's presence is uncertain. |
d.keys() | Returns a dynamic dict_keys view object containing all keys. Reflects changes to the dictionary in real time. Supports set operations (union, intersection). Iterate directly or wrap in list() to get a static snapshot. |
d.values() | Returns a dynamic dict_values view containing all values (not necessarily unique). Reflects real-time changes to the dictionary. Supports iteration but not set operations (since values may be duplicated or unhashable). |
d.items() | Returns a dynamic dict_items view of all (key, value) tuples. Used extensively in for k, v in d.items(): patterns. Supports set operations on key-value pairs. Essential for simultaneous key and value access during iteration. |
d.update(other) | Merges all key-value pairs from other (dict, iterable of pairs, or keyword args) into d, overwriting existing keys with new values. Modifies d in place. E.g., d.update({'x': 1, 'y': 2}) or d.update(x=1, y=2). |
d.pop(key) | Removes the key and returns its associated value. Raises KeyError if the key is missing and no default is provided. Pass a second argument as a fallback default to avoid the error. Useful for consuming a value from the dict atomically. |
d.setdefault(k, v) | Returns the value for k if it exists; if not, inserts k with value v and returns v. Useful for initializing default values on first access without overwriting existing ones. Common pattern for building nested dictionaries. |
{k: v for k, v in ...} | Dictionary comprehension — creates a new dict by iterating over an iterable and mapping keys to values. Concise and readable. E.g., {k: v**2 for k, v in pairs.items() if v > 0}. Supports filtering with an optional if clause. |
key in d | Membership test — returns True if key exists as a key in the dictionary. O(1) average time due to hash table implementation. Note: tests keys only, not values. Use key in d.values() to test for value membership (O(n)). |
File I/O
| Function / Syntax | Description |
|---|---|
with open(f, 'r') as f: | Opens a file as a context manager, automatically closing it when the with block exits — even if an exception occurs. This is the strongly recommended pattern for file handling, preventing resource leaks and dangling file handles. The file object is bound to the as variable. |
f.read() | Reads and returns the entire file content as a single string (text mode) or bytes object (binary mode). Advances the file pointer to the end. For very large files, reading all at once may exhaust memory — prefer iterating line-by-line or using readlines() with chunking. |
f.readlines() | Reads all remaining lines and returns them as a list of strings, with newline characters (\n) included. Useful when you need random access to specific lines. For memory efficiency with large files, iterate over the file object directly (for line in f:). |
f.readline() | Reads and returns exactly one line including the trailing newline character. Returns an empty string at end-of-file. Useful for processing files one line at a time without loading the entire file into memory. Call repeatedly to advance through the file. |
f.write(s) | Writes the string s to the file and returns the number of characters written. Does not automatically add a newline — include \n explicitly when needed. In 'w' mode, truncates existing content; in 'a' mode, appends to the end. |
f.writelines(list) | Writes all strings from an iterable to the file in sequence. Does not insert separators or newlines between items — include \n at the end of each string if needed. More efficient than calling write() in a loop for large collections of lines. |
f.seek(0) | Moves the file pointer (read/write cursor) to a specific byte position. seek(0) rewinds to the beginning of the file, allowing re-reading after a previous read. The optional second argument specifies the reference point: 0 (beginning), 1 (current), or 2 (end). |
Exception Handling
| Function / Syntax | Description |
|---|---|
try: / except E: | The try block wraps code that may raise an exception. If an exception of type E (or a subclass of E) is raised, execution jumps to the matching except block. Code after the error in the try block is skipped. Multiple except clauses can handle different exception types. |
except (E1, E2): | Catches multiple exception types in a single except clause by providing a tuple. The parentheses are required. Useful when the same recovery logic applies to several different error types. Equivalent to writing separate except blocks for each type. |
except E as e: | Catches exception of type E and binds the exception instance to variable e within the except block. Provides access to the exception's message, arguments, and traceback. E.g., print(e.args) or str(e) to display the error message. |
else: | The optional else clause runs only if the try block completed without raising any exception. Useful for code that should execute only on success but should not be inside the try block itself (to avoid accidentally masking errors in the success path). |
finally: | The finally block always executes regardless of whether an exception was raised, caught, or even re-raised. Used for cleanup operations like closing files, releasing locks, or freeing resources. Runs even if return or break is called inside try/except. |
raise ValueError(msg) | Explicitly raises an exception, interrupting normal execution. Provide a descriptive message string to aid debugging. Use built-in exception types when applicable (ValueError, TypeError, KeyError, etc.) or define custom exception classes for application-specific errors. |
raise | Re-raises the currently active exception without modification, preserving the original traceback. Used inside an except block after performing partial handling (like logging) when you want the exception to propagate further up the call stack. |
Common Standard Library
| Function / Syntax | Description |
|---|---|
import os | Provides a portable interface to operating system functionality: file and directory operations, path manipulation, environment variables (os.environ), process management, and platform detection. Essential for any script that interacts with the file system. |
os.path.join(a, b) | Constructs a file path by joining one or more path components with the correct OS-specific separator (backslash on Windows, forward slash on Unix). Prevents common bugs from hardcoded separators. E.g., os.path.join('data', 'file.csv'). |
os.listdir(path) | Returns a list of all filenames and directory names in the specified directory (not recursive). Does not include . or ... For recursive listing, use os.walk(). For pattern-based filtering, use the glob module. |
import sys | Provides access to Python interpreter internals: command-line arguments (sys.argv), the module search path (sys.path), standard streams (sys.stdin/stdout/stderr), the Python version (sys.version), and the exit function (sys.exit()). |
sys.argv | A list of command-line arguments passed to the script. sys.argv[0] is always the script name itself; sys.argv[1:] contains the actual arguments. For complex CLI argument parsing, use the argparse module instead. |
import json | Provides JSON (JavaScript Object Notation) encoding and decoding. Converts between Python objects and JSON strings. Python dicts map to JSON objects, lists to arrays, strings, numbers, booleans, and None to their JSON counterparts. Widely used for API communication and data storage. |
json.dumps(obj) | Serializes a Python object to a JSON-formatted string. Accepts optional parameters: indent for pretty-printing, sort_keys=True for consistent key ordering, and default function for custom serialization of non-serializable types (like datetime objects). |
json.loads(s) | Parses a JSON string and returns the corresponding Python object. JSON objects become dicts, arrays become lists, strings/numbers/booleans/null map to their Python equivalents. Raises json.JSONDecodeError for invalid JSON. Use json.load(f) to parse directly from a file object. |
import re | Provides full regular expression support for advanced string matching, searching, and manipulation. Patterns follow Perl-style regex syntax. Key functions: search(), match(), findall(), sub(), compile(). Compile patterns with re.compile() for repeated use performance. |
re.search(p, s) | Scans through the string s looking for any location where pattern p matches. Returns a match object on the first match, or None if no match is found. Unlike re.match(), does not require the match to start at the beginning of the string. |
re.findall(p, s) | Finds all non-overlapping matches of pattern p in string s and returns them as a list of strings. If the pattern contains groups, returns a list of group tuples. Returns an empty list if no matches are found. |
import datetime | Provides classes for working with dates, times, time intervals, and time zones. Core classes: datetime.date (year/month/day), datetime.time (hour/minute/second/microsecond), datetime.datetime (combined), and datetime.timedelta (duration arithmetic). |
datetime.datetime.now() | Returns the current local date and time as a datetime object. For UTC time, use datetime.datetime.utcnow() or datetime.datetime.now(datetime.timezone.utc). Use .strftime(format) to format as a string. |
import math | Provides mathematical functions for real-number arithmetic. Includes: math.sqrt(), math.pow(), math.ceil(), math.floor(), math.log(), trigonometric functions, and constants like math.pi and math.e. Use cmath for complex number math. |
import random | Provides pseudo-random number generation for simulations, games, testing, and sampling. Key functions: random() (float in [0.0, 1.0)), randint(a, b), choice(), shuffle(), sample(). For cryptographic randomness, use the secrets module instead. |
random.choice(list) | Selects and returns a single random element from a non-empty sequence. Raises IndexError on empty sequences. Uses the Mersenne Twister PRNG. For weighted random selection, use random.choices(seq, weights=...). |
random.randint(a, b) | Returns a random integer N such that a ≤ N ≤ b (both endpoints are inclusive). Equivalent to random.randrange(a, b+1). For a random float, use random.uniform(a, b). |
from collections import Counter | A dict subclass for counting hashable objects. Initialize with any iterable to count element frequencies. Supports arithmetic operations between counters. Key methods: most_common(n) returns the n most frequent elements. E.g., Counter('banana') → {'a': 3, 'n': 2, 'b': 1}. |
from collections import defaultdict | A dict subclass that calls a factory function to supply missing values automatically instead of raising KeyError. The factory (e.g., int, list, set) is provided at construction. E.g., defaultdict(list) initializes missing keys with an empty list — ideal for grouping operations. |
R Quick Reference
Basic Functions
| Function / Syntax | Description |
|---|---|
print(x) | Prints a representation of an R object to the console, automatically formatting the output based on the object's type (vector, list, data frame, etc.). Adds an index prefix like [1] for vectors. Calling an object name at the top level implicitly calls print(). |
cat(x) | Concatenates and outputs objects as a character stream without automatic formatting or index labels. Ideal for printing clean, readable messages with custom formatting. Unlike print(), does not add a newline by default — append \n explicitly when needed. |
length(x) | Returns the number of elements in a vector, list, or other R object. For data frames, returns the number of columns (not rows). For matrices, returns the total number of elements (rows × columns). Use nrow() and ncol() for dimensional queries on 2D objects. |
nrow(df), ncol(df) | Return the number of rows and columns in a data frame or matrix respectively. Essential for quickly understanding dataset dimensions before processing. Equivalent to dim(df)[1] and dim(df)[2]. Return NULL for one-dimensional objects like vectors. |
dim(x) | Returns an integer vector of the dimensions of an object. For a data frame or matrix, returns c(nrow, ncol). For a 3D array, returns all three dimensions. Returns NULL for 1D vectors. Useful for quickly understanding the shape of multi-dimensional data structures. |
str(x) | Displays a compact, human-readable summary of an R object's internal structure. Shows the class, dimensions, column names, data types, and the first few values of each column. Invaluable for quickly inspecting data frames, lists, and complex nested structures during exploratory analysis. |
class(x) | Returns the class attribute of an R object, indicating how R should handle it (e.g., "numeric", "data.frame", "lm"). Objects can have multiple classes. The class drives method dispatch for S3 generic functions. Use inherits(x, "class") for safer class testing. |
typeof(x) | Returns the low-level internal storage type of an R object (e.g., "double", "integer", "list", "logical"). Unlike class(), reflects the underlying C-level representation. Useful for distinguishing between, for example, numeric (double) and integer storage. |
head(df, n) | Returns the first n rows of a data frame, matrix, or vector (default n=6). Essential for quickly previewing data without printing the entire dataset. Works on any object with defined row indexing. Use tail() to see the last rows instead. |
tail(df, n) | Returns the last n rows of a data frame, matrix, or vector (default n=6). Particularly useful for checking data appended at the end, verifying row counts, and detecting issues like incomplete final records or trailing NA values after an import or transformation. |
summary(x) | Produces a statistical summary tailored to the object type. For numeric vectors and columns: min, Q1, median, mean, Q3, max. For factors: frequency counts per level. For data frames: per-column summaries. For model objects (e.g., lm): coefficients, residuals, and fit statistics. |
Vector Operations
| Function / Syntax | Description |
|---|---|
c(1, 2, 3) | The fundamental function for creating vectors by combining (concatenating) scalar values or other vectors. All elements are coerced to the same type (type coercion hierarchy: logical → integer → double → character). E.g., c(1, "a") → c("1", "a"). |
seq(from, to, by) | Generates a sequence of values from from to to with increment by. Alternative: use length.out instead of by to specify the desired number of values. E.g., seq(0, 1, by=0.25) → 0.00 0.25 0.50 0.75 1.00. The 1:n shorthand creates integer sequences. |
rep(x, times) | Replicates elements of x a specified number of times. times can be a single integer (repeat the whole vector) or a vector of the same length as x (repeat each element individually). The each argument repeats each element before moving to the next. |
v[i] | Extracts the element at 1-based index i (R uses 1-based indexing, unlike most languages). Negative indices exclude elements (e.g., v[-1] returns all but the first). Named vectors can be indexed by name: v["name"]. Out-of-range indices return NA. |
v[c(1,3,5)] | Extracts multiple elements at once by providing an index vector. Duplicated indices return duplicated elements. Named vectors support character index vectors. This is vectorized subsetting — one of R's most powerful features for data extraction without loops. |
v[v > 5] | Logical subsetting — extracts only elements where the condition evaluates to TRUE. R evaluates the condition element-wise to produce a logical vector, then uses it as a mask. Multiple conditions can be combined with & (AND) and | (OR). Produces NA for NA positions. |
sort(v) | Returns a sorted copy of the vector in ascending order by default. Use decreasing=TRUE for descending order. NA values are removed by default (controlled by na.last argument). Works on numeric, character, and logical vectors. |
rev(v) | Returns the vector with its elements in reversed order. Works on vectors, lists, and other sequential objects. Does not modify the original (R uses copy-on-modify semantics). Useful for reversing sorted data or iterating from end to beginning. |
unique(v) | Returns a vector with duplicate elements removed, preserving the order of first occurrences. Works on vectors, data frames (returns unique rows), and lists. Use duplicated(v) to get a logical mask identifying duplicate positions. Use table(v) to count occurrences. |
%in% | Infix membership operator that tests whether each element of the left-hand side exists anywhere in the right-hand side vector. Returns a logical vector of the same length as the LHS. E.g., c(1,5,9) %in% c(1,2,3) → TRUE FALSE FALSE. Vectorized equivalent of multiple == comparisons with |. |
Data Frame Operations
| Function / Syntax | Description |
|---|---|
data.frame(...) | Creates a data frame — R's primary structure for tabular data, analogous to a spreadsheet or database table. Each named argument becomes a column; all columns must have the same length. Columns can have different data types. The workhorse data structure for statistical analysis. |
df$col | Extracts a single column from a data frame as a vector using the dollar sign accessor. The column name is unquoted. Returns NULL if the column doesn't exist (not an error). Supports autocomplete in RStudio. Equivalent to df[["col"]]. |
df[i, j] | Generalized subsetting using bracket notation. i is the row index (integer, logical vector, or row name), j is the column index (integer, character, or logical vector). Leave either blank to select all rows or columns. E.g., df[1:5, c("a","b")]. |
df[, c("col1", "col2")] | Selects specific columns by name, returning a data frame (not a vector). The leading comma with blank row selector means "all rows." Preserves the data frame structure. Useful for isolating a subset of features before modeling or analysis. |
subset(df, condition) | Filters rows where the condition is TRUE and optionally selects columns using the select argument. Column names can be used directly without df$. More readable than bracket notation for complex conditions. Note: not recommended inside functions due to non-standard evaluation. |
rbind(df1, df2) | Stacks two or more data frames or matrices vertically (row-wise). Both objects must have the same column names and compatible types. Used for appending new observations to an existing dataset. For large-scale row binding, dplyr::bind_rows() is more flexible. |
cbind(df1, df2) | Combines two or more data frames or matrices horizontally (column-wise). Both objects must have the same number of rows. Used to attach new features/columns to an existing data frame. For safer column binding, dplyr::bind_cols() provides alignment checking. |
merge(df1, df2, by) | Joins two data frames based on shared key columns specified in by. Defaults to an inner join. Control join type with all.x (left join), all.y (right join), or all (full join). For multiple key columns, pass a character vector to by. |
names(df) | Gets or sets the column names of a data frame (or any named R object). Assigning to names(df) renames columns in place: names(df)[2] <- "newname". Also works on named vectors and lists to retrieve their element names. |
colnames(df) | Gets or sets the column names of a matrix or data frame. Equivalent to names() for data frames but also works on matrices (which don't support names()). Use rownames() for the complementary row name access. |
rownames(df) | Gets or sets the row names (row identifiers) of a data frame or matrix. By default, rows are numbered 1 to n. Assigning meaningful row names enables row-based subsetting by name. For large data frames, row names can slow operations — consider using a dedicated ID column instead. |
Statistical Functions
| Function / Syntax | Description |
|---|---|
mean(x) | Computes the arithmetic mean (average) of a numeric vector. Returns NA if any element is NA — use mean(x, na.rm=TRUE) to exclude missing values. For trimmed means (removing outliers), specify the trim argument (e.g., trim=0.1 drops 10% from each tail). |
median(x) | Returns the middle value (50th percentile) of a sorted numeric vector. More robust than the mean to outliers and skewed distributions. For even-length vectors, returns the average of the two middle values. Use na.rm=TRUE to ignore missing values. |
sd(x) | Computes the sample standard deviation using Bessel's correction (divides by n-1, not n). Measures the average distance of values from the mean. Returns NA with missing values unless na.rm=TRUE. Use sd(x)^2 to get the variance, or use var(x) directly. |
var(x) | Computes the sample variance (square of the standard deviation). Uses n-1 degrees of freedom (Bessel's correction) for an unbiased estimate of population variance. For a 2D matrix or data frame, var(df) returns the covariance matrix between all column pairs. |
min(x), max(x) | Return the minimum and maximum values in a numeric vector respectively. Handle NA values with na.rm=TRUE. For element-wise min/max across two vectors, use pmin() and pmax(). Can accept multiple arguments: min(a, b, c). |
sum(x) | Computes the total sum of all elements in a numeric vector. Returns NA if any element is NA unless na.rm=TRUE is specified. For column-wise or row-wise sums in matrices, use colSums() and rowSums() respectively for better performance. |
quantile(x, probs) | Computes quantiles (percentiles) of a numeric vector at the specified probabilities. probs is a vector of values between 0 and 1. E.g., quantile(x, c(0.25, 0.75)) returns Q1 and Q3 for the interquartile range. Nine types of interpolation methods available. |
cor(x, y) | Computes the Pearson correlation coefficient between two numeric vectors (default). Returns a value between -1 (perfect negative) and 1 (perfect positive). For matrices or data frames, returns the full correlation matrix. Alternative methods: method="spearman" or "kendall" for rank-based correlation. |
table(x) | Creates a contingency table of counts for each unique value in a vector or factor. Passing two variables creates a cross-tabulation (2-way table). Useful for exploring categorical data distributions, checking class imbalance, and computing conditional frequencies. Convert with as.data.frame() for tidy output. |
Apply Family Functions
| Function / Syntax | Description |
|---|---|
apply(X, MARGIN, FUN) | Applies a function over the rows (MARGIN=1) or columns (MARGIN=2) of a matrix or array. Returns a vector or matrix depending on the function's output. For data frames, consider lapply()/sapply() or dplyr::summarise() for more flexibility. |
lapply(X, FUN) | Applies a function to each element of a list (or vector) and always returns a list of the same length. The "l" stands for list. Use when the function returns variable-length or complex results. Ideal base for functional programming in R. The result can be unpacked with do.call(). |
sapply(X, FUN) | A user-friendly wrapper around lapply() that attempts to simplify the result into a vector or matrix when possible. If all elements have length 1, returns a named vector. If all have the same length >1, returns a matrix. Returns a list if simplification is not possible. |
tapply(X, INDEX, FUN) | Applies a function to subsets of a vector defined by a grouping factor (INDEX). Returns an array (or vector) of results, one per group. The "t" stands for "table." Similar in concept to SQL GROUP BY. E.g., tapply(salary, department, mean) computes mean salary per department. |
mapply(FUN, ...) | A multivariate version of sapply() that applies a function to the corresponding elements of multiple vectors simultaneously (element-wise). The "m" stands for multivariate. Equivalent to a vectorized multi-argument function call. Useful when you need to pass multiple arguments in parallel. |
File I/O
| Function / Syntax | Description |
|---|---|
read.csv(file) | Reads a comma-separated values file into a data frame. Common arguments: header=TRUE (default), stringsAsFactors=FALSE (recommended to keep strings as character), na.strings to define NA markers, and skip to skip header rows. For large files, consider data.table::fread() for speed. |
write.csv(df, file) | Writes a data frame to a CSV file. By default, writes row names as the first column (use row.names=FALSE to suppress). Always opens the file in text mode with the appropriate line endings. Use write.csv2() for semicolon-delimited files common in European locales. |
read.table(file) | A general-purpose function for reading delimited text files into a data frame. The sep argument specifies the delimiter (default: whitespace), header indicates whether the first row contains column names. read.csv() is a wrapper around read.table() with CSV-appropriate defaults. |
write.table(df, file) | Writes a data frame to a delimited text file with full control over the separator (sep), quote handling, row names, column names, and NA representation. More flexible than write.csv() for non-standard formats. Use append=TRUE to add rows to an existing file. |
readLines(file) | Reads the entire file as a character vector where each element is one line (without the newline character). Useful for processing unstructured text, log files, or custom formats. The n argument limits the number of lines read. Use con parameter for connection objects and remote URLs. |
writeLines(text, file) | Writes a character vector to a file, one element per line, appending a newline after each element. Simpler than write.table() for plain text output. Use con to write to a connection object, URL, or standard output. Automatically handles line endings per platform. |
Common Packages
| Package / Function | Description |
|---|---|
library(package) | Loads an installed package into the current R session, making its functions available without the package::function() prefix. Raises an error if the package is not installed. Use require() as a softer alternative that returns FALSE instead of erroring. |
install.packages("pkg") | Downloads and installs a package from CRAN (the Comprehensive R Archive Network) or another configured repository. Only needs to be run once per machine. Use update.packages() to keep packages up to date. For GitHub packages, use remotes::install_github(). |
dplyr | The core tidyverse package for data manipulation. Provides a consistent, expressive grammar of data manipulation: filter() to subset rows, select() to pick columns, mutate() to add/transform columns, summarise() to aggregate, and group_by() for grouped operations. Chained with the pipe operator |>. |
ggplot2 | A declarative data visualization package based on the Grammar of Graphics. Build plots layer by layer: define data and aesthetic mappings with ggplot(), then add geometry layers (geom_point(), geom_bar(), geom_line()), scales, themes, and facets. Produces publication-quality graphics. |
tidyr | Provides tools for reshaping and tidying data into a consistent "tidy" format where each variable is a column, each observation is a row. Key functions: pivot_longer() (wide to long), pivot_wider() (long to wide), separate(), unite(), and drop_na() for cleaning missing values. |
stringr | A consistent set of string manipulation functions built on top of the stringi package. All functions start with str_ for discoverability. Covers: pattern matching (str_detect(), str_extract()), transformation (str_to_upper(), str_replace()), and padding/trimming (str_pad(), str_trim()). |
lubridate | Makes working with dates and times in R intuitive and reliable. Parsing functions named after the format: ymd(), mdy(), dmy() handle common date formats automatically. Arithmetic with years(), months(), days() durations. Handles time zones, intervals, and daylight saving transitions correctly. |
HTML & CSS Quick Reference
HTML Structure
| Element / Syntax | Description |
|---|---|
<!DOCTYPE html> | Declares the document type and version to the browser. The HTML5 doctype is short and simple. Without it, browsers enter "quirks mode" and may render the page inconsistently across browsers. Must be the very first line of every HTML document. |
<html> | The root element that wraps the entire HTML document. All other elements must be descendants of this element. The lang attribute (e.g., lang="en") specifies the document language, which is important for accessibility tools and search engines. |
<head> | Container for document metadata that is not displayed directly on the page. Holds the document title, character encoding, viewport settings, CSS links, JavaScript links, Open Graph meta tags for social sharing, and other machine-readable information about the document. |
<title> | Defines the document's title shown in the browser tab, bookmarks, and search engine result pages (SERPs). Critical for SEO as search engines use it prominently. Should be descriptive and unique per page. Typically 50–60 characters for optimal display in search results. |
<meta charset="UTF-8"> | Specifies the character encoding for the document. UTF-8 is the universal encoding that supports virtually all characters from all human languages. Should be placed as early as possible inside <head> to ensure the browser decodes subsequent characters correctly. |
<link rel="stylesheet"> | Links an external CSS stylesheet to the document. The rel="stylesheet" attribute is required. Multiple stylesheets can be linked and are applied in order. The media attribute enables conditional loading (e.g., media="print" for print-only styles). |
<script src="..."> | Links an external JavaScript file. Add defer attribute to download without blocking HTML parsing and execute after parsing is complete (recommended). Add async to execute as soon as downloaded (for independent scripts). Without either, the script blocks page rendering. |
<body> | Contains all the visible content of the webpage — text, images, links, buttons, forms, and all other user-facing elements. The browser renders this section to the screen. Event handlers and most JavaScript DOM manipulation targets elements within the body. |
Common HTML Elements
| Element / Syntax | Description |
|---|---|
<h1> to <h6> | Six levels of section headings, from <h1> (most important, largest) to <h6> (least important, smallest). Use only one <h1> per page for SEO. Headings define the document outline and are critical for accessibility — screen readers use them for navigation. |
<p> | Defines a block-level paragraph of text. Browsers automatically add top and bottom margins. Should contain only inline-level content (text, <span>, <a>, <strong>, etc.). Do not nest block elements like <div> or other <p> tags inside a paragraph. |
<a href="url"> | Creates a hyperlink to another URL, a same-page anchor (href="#id"), an email address (href="mailto:..."), or a phone number (href="tel:..."). Use target="_blank" to open in a new tab (pair with rel="noopener noreferrer" for security). The alt-equivalent for links is the link text. |
<img src="url" alt=""> | Embeds an image. The alt attribute is required for accessibility — it describes the image for screen readers and is shown when the image fails to load. Use width and height attributes to reserve layout space and prevent cumulative layout shift (CLS). Self-closing (void element). |
<div> | A generic block-level container with no inherent semantic meaning. Used for grouping and styling purposes. Renders as a full-width block that starts on a new line. Use semantic elements (<section>, <article>, <nav>) when the content has meaning; reserve <div> for pure layout containers. |
<span> | A generic inline container with no inherent semantic meaning. Does not create line breaks or block-level formatting. Used to style or script a portion of text within a paragraph or other inline context. The inline equivalent of <div>. |
<ul>, <ol>, <li> | <ul> creates an unordered (bulleted) list; <ol> creates an ordered (numbered) list. <li> defines each list item inside either. <ol> supports start and reversed attributes. Lists can be nested by placing a new list inside an <li>. |
<table>, <tr>, <td>, <th> | <table> defines the table, <tr> a row, <td> a data cell, and <th> a header cell (bold and centered by default). Use <thead>, <tbody>, and <tfoot> for semantic grouping. Tables are for tabular data — avoid using them for page layout. |
<form> | Wraps interactive controls that collect user input. The action attribute specifies where to send the data; method specifies HTTP GET or POST. The novalidate attribute disables browser validation. Form submission can be intercepted with JavaScript's submit event for client-side validation. |
<input type="..."> | The most versatile form element. The type attribute determines the control: text, password, email, number, checkbox, radio, file, range, date, submit, hidden. Attributes like required, min, max, pattern, and placeholder provide built-in validation and UX. |
<button> | A clickable button element. Default type="submit" submits the enclosing form — set type="button" to prevent this. Preferred over <input type="button"> because it can contain rich HTML content (icons, styled text). Always add descriptive text or an aria-label for accessibility. |
<textarea> | A multi-line plain text input control. Size is set with rows and cols attributes or CSS. The content between the opening and closing tags becomes the default value. Unlike <input>, it is not a void element. Supports the same form attributes like required, placeholder, and maxlength. |
<select>, <option> | <select> creates a dropdown list; each <option> is a selectable item. The value attribute of the selected option is what gets submitted. Add multiple to allow multi-selection. Use <optgroup> to group related options with a label. |
<br> | Inserts a single line break within inline content (e.g., inside a paragraph or address). It is a void element (no closing tag). Use sparingly — for structural spacing, prefer CSS margin/padding. Appropriate uses include poetry, postal addresses, or code samples requiring explicit line breaks. |
<hr> | Represents a thematic break between paragraph-level content — a visual horizontal rule. Semantically indicates a shift in topic or section. Self-closing (void element). Style with CSS (height, border, color) rather than using the deprecated HTML attributes. Useful as a content divider in long-form text. |
Semantic HTML
| Element / Syntax | Description |
|---|---|
<header> | Represents introductory content or a group of navigational aids. Typically contains a logo, site title, main navigation, and a search bar. Can be used both at the page level (site header) and within <article> or <section> elements for section-specific headers. |
<nav> | Defines a section of navigation links — links that navigate to other parts of the same page or to other pages. Only major navigation blocks should be wrapped in <nav>. Screen readers use this landmark to let users skip directly to or over the navigation menu. |
<main> | Represents the dominant, unique content of the document body — excluding headers, footers, navigation, and sidebars. There should be only one <main> per page. A crucial landmark for accessibility, letting screen reader users jump directly to the primary content. |
<section> | Represents a standalone thematic grouping of content, typically with its own heading. Used to divide a page into logical sections (e.g., "About", "Services", "Contact"). If the content could stand alone as a complete item, use <article> instead. Avoid using <section> purely as a styling container — use <div> for that. |
<article> | Represents a self-contained piece of content that could be distributed or reused independently, such as a blog post, news article, forum post, or product card. Should make sense on its own without the surrounding context. Can contain its own <header> and <footer>. |
<aside> | Represents content that is tangentially related to the main content and could be considered separate from it. Common uses: pull quotes, related articles, sidebars, advertising, author bios, and supplementary information. Assistive technologies announce it as a complementary landmark region. |
<footer> | Represents the footer of its nearest sectioning ancestor. Typically contains copyright information, links to terms of service, contact details, or secondary navigation. A page can have multiple footers (e.g., one for the page, one inside each <article>). Not required to appear at the bottom visually. |
CSS Selectors
| Selector / Syntax | Description |
|---|---|
element | Type selector — matches all HTML elements of the specified tag name (e.g., p, h1, div). Low specificity (0,0,1). Applies to every instance globally, so use with caution to avoid broad unintended styling. Good for setting consistent baseline styles. |
.class | Class selector — matches all elements that have the specified class attribute. Medium specificity (0,1,0). An element can have multiple classes (class="btn primary"). The most common selector for component-based styling. Multiple class selectors can be chained: .btn.primary matches elements with both classes. |
#id | ID selector — matches exactly one element with the specified unique id attribute. High specificity (1,0,0). IDs should be unique per page. Because of high specificity, overriding ID styles can be difficult. Prefer class selectors for styling; reserve IDs for JavaScript hooks and page anchors. |
* | Universal selector — matches every element in the document. Very low specificity (0,0,0). Commonly used in CSS resets (* { box-sizing: border-box; margin: 0; }). Can be expensive on very large DOMs if used in complex combinators. Scoped use (div *) is more targeted. |
parent child | Descendant combinator — selects all elements matching child that are descendants (not just direct children) of elements matching parent, at any nesting depth. E.g., nav a styles all anchor tags anywhere inside a nav, regardless of nesting level. |
parent > child | Child combinator — selects elements matching child that are direct children only (one level deep) of elements matching parent. More specific and performant than the descendant combinator when deep nesting should be excluded. E.g., ul > li targets only top-level list items, not nested ones. |
element:hover | Pseudo-class selector — applies styles when the user's pointer hovers over the element. No specificity increase relative to the element type alone. Other interactive pseudo-classes: :focus, :active, :visited, :checked, :disabled, :nth-child(n), :first-child, :last-child. |
element::before | Pseudo-element — inserts generated content before the element's actual content. Requires a content property (even if empty: content: ""). Behaves as an inline element by default. Commonly used for decorative icons, counters, or clearfix hacks. ::after inserts content after the element's content. |
[attribute] | Attribute selector — matches elements that have the specified attribute. Variants: [attr="val"] (exact match), [attr^="val"] (starts with), [attr$="val"] (ends with), [attr*="val"] (contains). E.g., input[type="email"] styles email inputs specifically. |
selector1, selector2 | Grouping selector — applies the same declarations to multiple selectors separated by commas. Each selector in the group is evaluated independently. Reduces code duplication. E.g., h1, h2, h3 { font-family: sans-serif; } applies the font to all three heading levels. |
CSS Properties - Layout
| Property | Description |
|---|---|
display | Controls how an element is rendered in the layout flow. Key values: block (full width, new line), inline (flows with text, no width/height), inline-block (inline but supports dimensions), flex (flexbox container), grid (grid container), none (removed from layout entirely, unlike visibility: hidden). |
position | Determines the positioning scheme. static (default, normal flow), relative (offset from its normal position), absolute (removed from flow, positioned relative to nearest non-static ancestor), fixed (stays in viewport when scrolling), sticky (toggles between relative and fixed at a scroll threshold). |
top, right, bottom, left | Specifies the offset of a positioned element (any position value except static) from its containing block's edges. For relative elements, offsets from normal position. For absolute/fixed, offsets from the containing block/viewport. Values can be px, %, em, or auto. |
width, height | Sets the element's content dimensions. Accepts px, %, em, rem, vw/vh, or auto. With box-sizing: border-box, width and height include padding and border. min-width/max-width and min-height/max-height constrain dimensions for responsive layouts. |
margin | Adds space outside the element's border (between elements). Shorthand: one value (all sides), two values (top/bottom, left/right), four values (top, right, bottom, left). Margins between adjacent block elements collapse (the larger margin "wins"). margin: auto horizontally centers a block element with a defined width. |
padding | Adds space between the element's content and its border (inside the element). Same shorthand as margin. Unlike margins, padding does not collapse and is included in the element's clickable/visible area. Affects the background-color region. Follows the same shorthand order: top, right, bottom, left. |
border | Shorthand for setting border-width, border-style, and border-color together (e.g., border: 1px solid #ccc). Individual properties: border-width, border-style (solid, dashed, dotted, none), border-color. Each side can be set independently (border-top, etc.). border-radius rounds corners. |
box-sizing | Determines what is included in an element's stated width and height. content-box (default): width/height are content only; padding and border are added on top. border-box: width/height include content, padding, and border — making layout math predictable. Apply globally with *, *::before, *::after { box-sizing: border-box; }. |
overflow | Controls what happens when content overflows the element's box. visible (default, content renders outside), hidden (clips excess content), scroll (always shows scrollbars), auto (shows scrollbars only when needed). overflow-x and overflow-y control each axis independently. |
z-index | Controls the stacking order of positioned elements (those with a position value other than static). Higher values appear on top. Only works on positioned elements or flex/grid items. Elements within a stacking context are stacked relative to each other. A new stacking context is created by position: relative/absolute/fixed combined with a z-index value. |
CSS Properties - Text & Colors
| Property | Description |
|---|---|
color | Sets the foreground color of text content and text decorations. Accepts named colors (red), hex values (#ff0000), RGB (rgb(255,0,0)), RGBA with transparency (rgba(255,0,0,0.5)), and HSL/HSLA. Also affects the color of borders and outlines that use currentColor. |
background-color | Sets the background fill color of an element's box (content, padding, and border areas). Accepts the same color formats as color. Transparent by default. Can be layered with background-image. Use background: linear-gradient() or background-image for gradient or image backgrounds. |
font-family | Specifies the typeface(s) for text. Provide a comma-separated list of font names as a fallback stack (e.g., font-family: 'Roboto', sans-serif). Generic families (serif, sans-serif, monospace) should always be the last fallback. Load web fonts with @font-face or Google Fonts. |
font-size | Sets the size of text. Common units: px (fixed), em (relative to parent font size), rem (relative to root element's font size — preferred for accessibility), vw/vh (viewport-relative), and %. Browser default is typically 16px. Avoid using px exclusively to respect user font size preferences. |
font-weight | Sets the thickness/boldness of text. Keywords: normal (400), bold (700), lighter, bolder. Numeric values from 100 (thin) to 900 (black) in steps of 100. Actual appearance depends on which weights the loaded font supports — unsupported weights are approximated by the browser. |
text-align | Sets the horizontal alignment of inline content (text and inline elements) within a block container. Values: left, right, center, justify (stretches text to fill the line). Does not affect block-level children — use margin: auto or flexbox for those. |
text-decoration | Shorthand for decorating text with lines. Common values: underline (default for links), none (removes underlines from links), line-through (strikethrough), overline. Extended shorthand: text-decoration: underline dotted red sets line, style, and color together. |
line-height | Sets the height of a line box, controlling the space between lines of text. Unitless values (e.g., 1.5) are recommended as they scale with font size and avoid inheritance issues. A value of 1.5 means 150% of the current font size. Affects vertical centering when set equal to the element's height for single-line text. |
letter-spacing | Adjusts the spacing between characters (tracking). Positive values increase spacing; negative values decrease it. Useful for uppercase text readability (e.g., letter-spacing: 0.1em) or tight display headings. Always specify units (em or px). Use word-spacing to control the space between words. |
Flexbox
| Property | Description |
|---|---|
display: flex | Enables the Flexbox layout model on the container element. All direct children become flex items that can be aligned, distributed, and sized along two axes. Revolutionized CSS layout — far simpler than using floats or tables for component-level alignment and distribution. |
flex-direction | Sets the main axis direction for flex items. row (default, left to right), row-reverse (right to left), column (top to bottom), column-reverse (bottom to top). Determines which axis justify-content and align-items operate on. |
justify-content | Distributes flex items along the main axis. Values: flex-start (default), flex-end, center, space-between (equal gaps between items), space-around (equal space on each side of items), space-evenly (equal space everywhere). Critical for horizontal centering and even distribution. |
align-items | Aligns flex items along the cross axis (perpendicular to the main axis). Values: stretch (default, items fill the container height), flex-start, flex-end, center (vertical centering), baseline (align by text baseline). Use align-self on individual items to override. |
flex-wrap | Controls whether flex items wrap onto multiple lines when they overflow the container. nowrap (default, all items on one line — may overflow), wrap (items wrap to next line as needed), wrap-reverse (wraps to previous line). Use align-content to align the wrapped lines. |
gap | Sets spacing between flex (or grid) items uniformly without using margins. Shorthand: one value (both row and column gaps), two values (row-gap column-gap). Unlike margins, gap only creates space between items — not between items and the container edge. Cleaner than margin hacks for evenly spaced grids. |
flex-grow | Determines how much a flex item grows to fill available space relative to sibling items. Default: 0 (don't grow). Value 1 means the item takes its proportional share of free space. E.g., if one item has flex-grow: 2 and another has 1, the first takes twice as much free space. |
flex-shrink | Determines how much a flex item shrinks when there is not enough space in the container. Default: 1 (items shrink equally). Value 0 prevents the item from shrinking below its initial size. Higher values mean the item shrinks proportionally more than siblings. Part of the flex shorthand. |
JavaScript Quick Reference
Variables & Data Types
| Syntax | Description |
|---|---|
let x = value | Declares a block-scoped variable (introduced in ES6). Scoped to the enclosing block ({}), function, or module. Can be reassigned but not re-declared in the same scope. Not hoisted to an accessible state (temporal dead zone). Preferred over var for mutable values. |
const x = value | Declares a block-scoped constant binding (ES6). The binding cannot be reassigned after declaration — the variable cannot point to a different value. However, the content of objects and arrays declared with const can still be mutated. Must be initialized at declaration. Default choice for all variables that don't need reassignment. |
var x = value | Legacy variable declaration (pre-ES6). Function-scoped (or globally scoped if outside a function) rather than block-scoped. Variables are hoisted to the top of their function scope with undefined as the initial value. Avoid in modern code — use let or const instead to prevent hoisting-related bugs. |
typeof x | Returns a string indicating the type of the unevaluated operand. Possible values: "number", "string", "boolean", "undefined", "object" (also for null — a known quirk), "function", "symbol", "bigint". Safe to use on undeclared variables without throwing a ReferenceError. |
Number, String, Boolean | The three most common primitive data types. Number: 64-bit IEEE 754 floating-point (handles integers and decimals; includes special values Infinity and NaN). String: immutable sequence of UTF-16 characters. Boolean: logical true or false. All have corresponding wrapper objects but primitive forms are preferred. |
null, undefined | Two distinct "empty" values. undefined means a variable has been declared but not assigned a value, or a function returns without an explicit return value. null is an intentional absence of any object value, explicitly assigned by the developer. Both are falsy. Use strict equality (===) to distinguish between them. |
Symbol, BigInt | Symbol creates a unique, immutable primitive often used as object property keys to avoid naming collisions (e.g., in libraries and frameworks). BigInt represents arbitrary-precision integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1). Create with BigInt(n) or the n suffix (e.g., 9007199254740993n). |
Array Methods
| Method | Description |
|---|---|
arr.push(x) | Appends one or more elements to the end of an array and returns the new array length. Mutates the original array. For adding multiple elements at once: arr.push(a, b, c). To append another array without nesting, use arr.push(...otherArr) or concat(). |
arr.pop() | Removes the last element from an array and returns it. Mutates the original array. Returns undefined if the array is empty (does not throw). O(1) operation. The counterpart of push() for implementing stack (LIFO) data structures. |
arr.shift() | Removes and returns the first element of an array. Mutates the original array and re-indexes all remaining elements (O(n) operation). Use sparingly on large arrays — a queue built on shift()/push() has O(n) dequeue cost. The counterpart of unshift(). |
arr.unshift(x) | Inserts one or more elements at the beginning of an array and returns the new length. Mutates the array and shifts all existing elements to higher indices (O(n)). For high-performance queues with frequent front-insertions, consider a linked list or deque structure instead. |
arr.slice(start, end) | Returns a shallow copy of a portion of the array from index start to end (exclusive). Does not modify the original array (non-destructive). Negative indices count from the end. Omit end to slice to the last element. E.g., arr.slice(-3) returns the last 3 elements. |
arr.splice(i, n) | Modifies an array in place by removing n elements starting at index i and optionally inserting new elements. Returns an array of removed elements. Can simultaneously remove and insert. E.g., arr.splice(2, 1, 'a', 'b') removes 1 element at index 2 and inserts 'a' and 'b'. |
arr.map(fn) | Creates a new array by calling a function on every element, collecting the return values. Does not mutate the original. Returns a new array of the same length. The callback receives (currentValue, index, array). Ideal for transformations: arr.map(x => x * 2). |
arr.filter(fn) | Creates a new array containing only elements for which the callback returns a truthy value. Does not mutate the original. Returns an array that may be shorter than the original. Callback receives (currentValue, index, array). E.g., arr.filter(x => x > 0) keeps only positive numbers. |
arr.reduce(fn, init) | Applies a reducer function to each element, accumulating a single output value. The callback receives (accumulator, currentValue, index, array). init is the initial accumulator value (strongly recommended). Powerful for summations, flattening, grouping, and building objects from arrays. |
arr.forEach(fn) | Executes a callback function once for each array element. Does not return a value (always returns undefined). Unlike map(), it is used for side effects (logging, DOM updates). Cannot be broken out of early — use a regular for loop or Array.some() if early exit is needed. |
arr.find(fn) | Returns the first element for which the callback returns true. Returns undefined if no match is found. Stops at the first match (does not traverse the entire array). Use arr.findIndex(fn) to get the index instead of the value. Non-mutating. |
arr.indexOf(x) | Returns the index of the first occurrence of element x using strict equality (===). Returns -1 if not found. Accepts an optional fromIndex argument to start the search from. Does not work for finding objects by content — use find() or findIndex() with a predicate instead. |
arr.includes(x) | Returns true if the array contains the specified element (strict equality). Returns false otherwise. Unlike indexOf(), correctly handles NaN comparisons. Accepts an optional fromIndex to start the search. Non-mutating and cleaner than indexOf(x) !== -1 for boolean checks. |
arr.sort() | Sorts the array in place (mutates the original) and returns the sorted array. Without a comparator, elements are converted to strings and sorted lexicographically — [10, 2, 1].sort() → [1, 10, 2]. Always provide a comparator for numbers: arr.sort((a, b) => a - b). Uses a stable sort algorithm in modern engines. |
arr.reverse() | Reverses the elements of an array in place and returns a reference to the same array. Mutates the original. O(n). For a non-destructive reversed copy, use [...arr].reverse() (spread to clone first). Useful for descending sort: combine with sort(). |
arr.join(sep) | Joins all array elements into a single string, inserting sep between each element. Default separator is a comma. undefined and null elements are rendered as empty strings. Non-mutating. E.g., ['a','b','c'].join(' - ') → 'a - b - c'. The inverse of String.split(). |
String Methods
| Method | Description |
|---|---|
str.length | A read-only property that returns the number of UTF-16 code units in the string. Note: multi-byte emoji and some special characters may count as 2. Unlike Python, this is a property, not a method — no parentheses needed. Returns 0 for empty strings. |
str.toUpperCase() | Returns a new string with all alphabetic characters converted to uppercase. Non-alphabetic characters remain unchanged. Does not modify the original string (strings are immutable in JavaScript). Locale-sensitive variant: toLocaleUpperCase() for language-specific capitalization rules. |
str.toLowerCase() | Returns a new string with all alphabetic characters converted to lowercase. Non-alphabetic characters are unaffected. Commonly used for case-insensitive comparisons: str.toLowerCase() === input.toLowerCase(). Locale-sensitive variant: toLocaleLowerCase(). |
str.trim() | Returns a copy of the string with all leading and trailing whitespace (spaces, tabs, newlines) removed. Does not modify the original. trimStart() / trimLeft() removes only leading whitespace; trimEnd() / trimRight() removes only trailing whitespace. |
str.split(sep) | Splits a string by the separator and returns an array of substrings. If separator is an empty string (""), splits into individual characters. If separator is a regex, it splits on pattern matches. Passing no argument returns the original string wrapped in a single-element array. |
str.slice(start, end) | Extracts a section of a string from index start to end (exclusive) and returns it as a new string. Supports negative indices (counting from the end). Does not modify the original. More powerful than substring() because it handles negative indices correctly. |
str.substring(start, end) | Similar to slice() but does not support negative indices (treats them as 0). If start is greater than end, the arguments are swapped automatically. Non-mutating. Generally prefer slice() for its more predictable behavior with edge cases. |
str.replace(old, new) | Returns a new string with the first occurrence of old replaced by new. old can be a string (first match only) or a regular expression (use /pattern/g for all matches). new can be a string with special replacement patterns ($&, $1) or a function that computes the replacement. |
str.replaceAll(old, new) | Returns a new string with all occurrences of old replaced by new. Introduced in ES2021. When using a regex as old, the g flag is required, otherwise it throws a TypeError. Simpler and safer than replace(/pattern/g, new) when the pattern is a literal string. |
str.includes(sub) | Returns true if the string contains the specified substring anywhere within it. Case-sensitive. Accepts an optional position argument to start searching from. More readable than indexOf(sub) !== -1 for boolean existence checks. Does not support regex patterns — use test() for regex. |
str.startsWith(sub) | Returns true if the string begins with sub. Case-sensitive. Accepts an optional position argument to check from a given offset. Cleaner than checking indexOf() === 0. Commonly used for URL prefix checks, protocol detection, and similar validation tasks. |
str.endsWith(sub) | Returns true if the string ends with sub. Case-sensitive. Accepts an optional length argument to consider only the first N characters of the string as its "end." Useful for file extension checks and suffix validation without regex. |
str.indexOf(sub) | Returns the index of the first occurrence of sub within the string, or -1 if not found. Accepts an optional fromIndex to begin the search. Case-sensitive. For the last occurrence, use lastIndexOf(). For regex-based searching, use String.search() or RegExp.exec(). |
str.charAt(i) | Returns the character at the specified index as a single-character string. If the index is out of range, returns an empty string (not undefined). Equivalent to str[i] bracket notation, though charAt() is more predictable at out-of-range indices across environments. |
`template ${var}` | Template literals (backtick strings, ES6) embed expressions directly using ${expression} syntax. Expressions are evaluated and coerced to strings. Support multi-line strings without \n. Support tagged templates for advanced processing (e.g., css`...` in styled-components). The modern standard for string interpolation. |
Object Methods
| Method | Description |
|---|---|
Object.keys(obj) | Returns an array of the object's own enumerable string-keyed property names. Order follows insertion order for string keys (non-integer) and ascending numeric order for integer-like keys. Does not include inherited properties or Symbol keys. Used for iteration and transformation. |
Object.values(obj) | Returns an array of the object's own enumerable property values in the same order as Object.keys(). Introduced in ES2017. Useful for iterating over or computing aggregates from object values without needing the keys. Does not include prototype-inherited values. |
Object.entries(obj) | Returns an array of [key, value] pairs for all own enumerable properties. Introduced in ES2017. Perfect for destructuring iteration: for (const [k, v] of Object.entries(obj)). Use Object.fromEntries() to convert back from pairs to an object. |
Object.assign(target, src) | Copies all enumerable own properties from one or more source objects into the target object, returning the modified target. Used for shallow merging and object cloning. Properties in later sources overwrite earlier ones. Does not deep-clone — nested objects are still references. |
{...obj} | Spread syntax creates a shallow copy of all own enumerable properties of obj into a new object literal. Idiomatic and concise. Used for immutable updates: { ...user, age: 31 } creates a new object with all user properties plus an updated age. Same behavior as Object.assign({}, obj). |
obj.hasOwnProperty(key) | Returns true if the object has the specified key as a direct (own) property, not inherited through the prototype chain. For a safer alternative that works even when hasOwnProperty is overridden, use Object.prototype.hasOwnProperty.call(obj, key) or the newer Object.hasOwn(obj, key). |
delete obj.key | Removes a property from an object. Returns true on success (including when the property didn't exist). Does not affect prototype chain properties. Note: deleting properties can de-optimize JavaScript engine object shape caching. For maps with frequent additions/deletions, prefer Map data structure. |
Functions
| Syntax | Description |
|---|---|
function name(params) {} | A function declaration that is hoisted to the top of its scope — it can be called before it appears in the source code. Creates a named function bound to the name identifier in the current scope. Has its own this, arguments, and can be used as a constructor with new. |
const fn = function() {} | A function expression assigned to a variable. Not hoisted (only the variable declaration is hoisted, not the assignment). Can be anonymous or named. Named function expressions are useful for recursion and stack traces. The function can only be called after this line executes. |
const fn = (params) => {} | Arrow function syntax (ES6). Shorter syntax with no own this binding — inherits this from the enclosing lexical scope. No arguments object. Cannot be used as a constructor. Ideal for callbacks and non-method functions. Mandatory braces and explicit return for multi-line bodies. |
(param) => expression | Concise arrow function with implicit return — when the body is a single expression, the braces and return keyword are omitted. The expression value is automatically returned. Wrap object literals in parentheses to avoid ambiguity with block braces: () => ({ key: val }). |
function(a, b = 0) {} | Default parameter syntax (ES6). If the argument is undefined (or not provided), the default value is used. Works with any expression as the default, including function calls. null is not treated as missing — it does not trigger the default. Replaces the older b = b || 0 pattern. |
function(...args) {} | Rest parameter syntax (ES6) collects all remaining arguments passed to the function into a real Array named args. Must be the last parameter. Unlike the legacy arguments object, rest parameters are true arrays with full array methods available. E.g., sum(...nums) to accept any number of arguments. |
fn(...arr) | Spread syntax in a function call expands an iterable (array, string, Set, etc.) into individual arguments. E.g., Math.max(...nums) passes each element as a separate argument. Can be combined with other arguments: fn(a, ...arr, b). Replaces the older Function.apply(null, arr) pattern. |
DOM Manipulation
| Method | Description |
|---|---|
document.getElementById(id) | Returns the element with the specified unique id attribute. Returns null if not found. The fastest DOM selector because IDs are indexed directly. Should only match one element per page (IDs must be unique). Use when you need a single specific element by its identifier. |
document.querySelector(sel) | Returns the first element in the document that matches the CSS selector string. Returns null if no match. Supports any valid CSS selector (class, ID, attribute, pseudo-class, combinators). More flexible than getElementById(). Use element.querySelector() to scope the search within a subtree. |
document.querySelectorAll(sel) | Returns a static NodeList of all elements matching the CSS selector. Does not update automatically when the DOM changes (unlike getElementsByClassName). Convert to array with Array.from() or [...nodeList] to use array methods like map() and filter(). |
element.textContent | Gets or sets the text content of an element and all its descendants as a plain string. Setting it replaces all child nodes with a single text node. Safer than innerHTML for inserting user-provided text because it does not parse HTML, preventing XSS injection attacks. |
element.innerHTML | Gets or sets the HTML markup contained within the element. Setting it parses the string as HTML and replaces all children. Powerful but dangerous with untrusted input — always sanitize user data before using innerHTML to prevent cross-site scripting (XSS) vulnerabilities. For plain text, prefer textContent. |
element.classList.add(class) | Adds one or more CSS class names to the element's classList. Ignores duplicates — adding a class that already exists has no effect and does not throw. Multiple classes can be added at once: element.classList.add('a', 'b', 'c'). The preferred way to manipulate classes vs. string manipulation of className. |
element.classList.remove(class) | Removes one or more CSS class names from the element. If the class does not exist, it is silently ignored (no error thrown). Multiple classes can be removed at once. Does not affect other classes on the element. Safer and more readable than manual string manipulation of className. |
element.classList.toggle(class) | Adds the class if it is absent, removes it if it is present. Returns true if the class was added, false if removed. The optional second argument forces the add (true) or remove (false) regardless of current state. Ideal for show/hide, dark mode, and active state toggles. |
element.setAttribute(attr, val) | Sets or updates the value of an attribute on the element. If the attribute doesn't exist, it is created. For boolean attributes (e.g., disabled), pass any string value. Use getAttribute() to read, removeAttribute() to delete. For common properties like src or href, direct property assignment is often simpler. |
element.style.property = value | Sets an inline CSS style directly on the element. Property names are camelCase (e.g., backgroundColor). Inline styles have the highest specificity and are hard to override with stylesheets. For toggleable states, prefer classList instead. Set to an empty string ("") to remove an inline style and revert to stylesheet values. |
element.addEventListener(event, fn) | Attaches an event handler for the specified event type (e.g., 'click', 'input', 'keydown'). Multiple listeners can be added for the same event. The optional third argument controls bubbling/capturing behavior. Remove with removeEventListener() — pass the same function reference. Does not overwrite existing handlers unlike the onclick property. |
document.createElement(tag) | Creates a new, disconnected HTML element of the specified tag type (e.g., 'div', 'li', 'img'). The element exists in memory but is not yet part of the document. Set its properties and attributes before inserting into the DOM with appendChild(), prepend(), or insertBefore(). |
parent.appendChild(child) | Inserts a node as the last child of the parent element. If child already exists in the DOM, it is moved (not cloned) to the new position. Returns the appended child node. For inserting at a specific position, use insertBefore() or element.before()/element.after(). |
element.remove() | Removes the element from the DOM entirely. Simpler than the older element.parentNode.removeChild(element) pattern. Does not destroy the element object — it can be reinserted later. Does not throw if called on an element not in the DOM. Detached elements remain in memory until all references are released. |
Async & Promises
| Syntax | Description |
|---|---|
setTimeout(fn, ms) | Schedules a function to execute once after a minimum delay of ms milliseconds. Returns a timer ID that can be passed to clearTimeout(id) to cancel before it fires. The delay is a minimum — the actual delay may be longer if the call stack is busy. For zero-delay (ms=0), execution is deferred until the current script finishes. |
setInterval(fn, ms) | Calls a function repeatedly every ms milliseconds. Returns an interval ID for cancellation with clearInterval(id). Does not guarantee precise timing — if the callback takes longer than the interval, executions can queue up. For animations, prefer requestAnimationFrame(). |
new Promise((res, rej) => {}) | Creates a new Promise object representing an eventual value. The executor function receives resolve and reject callbacks. Call resolve(value) when the operation succeeds and reject(error) when it fails. The Promise is in "pending" state until one of these is called. Promises can only settle once. |
promise.then(fn) | Registers a callback to run when the promise resolves successfully. The callback receives the resolved value. Returns a new promise, enabling method chaining. If fn returns a value, the next then() in the chain receives it. If it returns a promise, the chain waits for that promise to settle. |
promise.catch(fn) | Registers a callback to run when the promise is rejected (or when any preceding then() throws). Equivalent to promise.then(undefined, fn). Returns a new promise. Use at the end of a then() chain to handle errors from any step. Always add a catch() to avoid unhandled promise rejection warnings. |
async function name() {} | Declares an asynchronous function that always returns a Promise. Inside, you can use await to pause execution until a Promise settles. Errors thrown inside an async function automatically reject the returned Promise. Makes asynchronous code look and behave synchronously, improving readability over promise chains. |
await promise | Pauses the execution of the enclosing async function until the awaited Promise settles, then resumes with the resolved value. If the Promise rejects, await throws the rejection reason (which can be caught with try/catch). Non-Promise values are automatically wrapped in Promise.resolve(). Can only be used inside async functions. |
fetch(url) | The modern browser API for making HTTP network requests. Returns a Promise that resolves to a Response object. Chain .json(), .text(), or .blob() to read the body. Does not reject on HTTP error status codes (4xx/5xx) — check response.ok manually. Configure method, headers, and body with the optional second argument. |
JSON & Local Storage
| Method | Description |
|---|---|
JSON.stringify(obj) | Converts a JavaScript value to a JSON string. Accepts optional replacer (filter function or array of keys) and space (indentation — use 2 for readable output). undefined, functions, and Symbol values are omitted. Objects with a toJSON() method use its return value. Circular references throw a TypeError. |
JSON.parse(str) | Parses a JSON string and returns the corresponding JavaScript value. Throws SyntaxError on invalid JSON — always wrap in try/catch when parsing external data. Accepts an optional reviver function for custom transformation of parsed values (e.g., converting date strings to Date objects). JSON only supports strings, numbers, booleans, null, arrays, and objects. |
localStorage.setItem(k, v) | Persists a key-value pair in the browser's localStorage. Data persists across browser sessions until explicitly removed. Both key and value must be strings — use JSON.stringify() to store objects. Storage is scoped per origin (protocol + hostname + port). Quota is typically 5–10 MB per origin. |
localStorage.getItem(k) | Retrieves the string value associated with the key from localStorage. Returns null (not undefined) if the key doesn't exist. Use JSON.parse() to convert back to objects. Synchronous and blocking — for large data, consider IndexedDB instead. Use sessionStorage for data that should clear when the tab closes. |
localStorage.removeItem(k) | Removes the key and its associated value from localStorage. If the key doesn't exist, the call is silently ignored. Use localStorage.clear() to remove all items for the origin at once. Changes to localStorage are reflected immediately across all tabs on the same origin (listen for the storage event to detect cross-tab changes). |
Java Quick Reference
Basic Syntax
| Syntax | Description |
|---|---|
public class ClassName {} | Defines a public class accessible from any package. In Java, each public class must be in a file named exactly ClassName.java. A class is the fundamental building block of Java programs — all code must live inside a class. The class keyword creates a blueprint for objects with fields and methods. |
public static void main(String[] args) | The entry point of any standalone Java application. The JVM calls this method when running the program. public: accessible by JVM; static: can be called without an instance; void: no return value; String[] args: receives command-line arguments as a string array. |
System.out.println(x) | Prints the string representation of x followed by a newline to the standard output stream. Works with any type via automatic toString() conversion. System.out is a PrintStream object. For formatted output, use System.out.printf(format, args) with format specifiers. |
System.out.print(x) | Prints x to the standard output without appending a newline. Useful when building output incrementally in loops or when you want finer control over line breaks. Contrast with println() which always adds a \n at the end. |
Scanner sc = new Scanner(System.in) | Creates a Scanner object that reads from standard input (the keyboard). Part of java.util — requires import java.util.Scanner. Always close the scanner with sc.close() when done to prevent resource leaks. Also works with File objects for reading from files. |
sc.nextLine() | Reads and returns the remainder of the current line (up to the newline character) as a String, discarding the newline. After calling nextInt() or similar methods, call nextLine() once to consume the leftover newline before reading the next string line. |
sc.nextInt() | Reads the next token from the input stream and parses it as an int. Throws InputMismatchException if the next token is not a valid integer and NoSuchElementException if input is exhausted. Does not consume the trailing newline — pair with nextLine() afterward. |
//comment | Single-line comment — everything after // on the same line is ignored by the compiler. Used for brief explanations, notes, and temporarily disabling a line of code. Good practice to explain "why" rather than "what" the code does. Does not affect performance. |
/* comment */ | Block (multi-line) comment that can span multiple lines. Everything between /* and */ is ignored by the compiler. Use for longer explanations. The special form /** ... */ (Javadoc) generates API documentation. Cannot be nested — a /* inside another block comment does not open a new comment. |
Data Types & Variables
| Type / Syntax | Description |
|---|---|
int x = 0 | 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647. The most commonly used numeric type in Java. Default value for instance fields is 0. For very large integers, use long. Java does not support unsigned integers natively (use Integer.toUnsignedLong() for unsigned arithmetic). |
long x = 0L | 64-bit signed integer. Range: roughly ±9.2 × 10^18. The L suffix is required for long literals (lowercase l is discouraged as it looks like 1). Used when int is too small, such as for timestamps (milliseconds since epoch), file sizes, or large ID values. |
double x = 0.0 | 64-bit IEEE 754 double-precision floating-point. The default type for decimal numbers in Java. Provides approximately 15–17 significant decimal digits of precision. Avoid for financial calculations (rounding errors) — use BigDecimal instead. Supports special values: Double.NaN, Double.POSITIVE_INFINITY. |
float x = 0.0f | 32-bit IEEE 754 single-precision floating-point. The f suffix is required to distinguish float literals from the default double. About half the memory of double but lower precision (~7 significant digits). Useful in graphics programming, arrays of large numbers where precision isn't critical, and GPU/OpenGL interfaces. |
boolean x = true | Represents a logical value: exactly true or false. Unlike C/C++, Java booleans cannot be converted to or from integers (no numeric equivalents). Used in conditionals, loop conditions, and flag variables. Default value for boolean fields is false. The wrapper class is Boolean. |
char c = 'A' | 16-bit Unicode character (UTF-16 code unit). Range: '\u0000' to '\uffff'. Enclosed in single quotes (double quotes are for Strings). Can be used in arithmetic (adds its numeric Unicode value). For full Unicode support (including emoji and supplementary characters), use String or code points. |
String s = "text" | An immutable sequence of characters (a reference type, not a primitive). String literals are automatically interned — Java reuses the same object for identical literals. Operations like concatenation create new String objects. For mutable string building, use StringBuilder (single-threaded) or StringBuffer (thread-safe). |
final int X = 10 | The final keyword on a variable creates a constant — the variable can only be assigned once. By convention, constants are named in UPPER_SNAKE_CASE. Combined with static, creates a class-level constant (e.g., static final double PI = 3.14159). The compiler enforces that final variables are initialized before use. |
String Methods
| Method | Description |
|---|---|
s.length() | Returns the number of characters (UTF-16 code units) in the String. Note: unlike arrays which use the .length field, String uses the .length() method (with parentheses). Returns 0 for empty strings. For strings containing multi-byte Unicode characters (like emoji), the count may exceed the visual character count. |
s.charAt(i) | Returns the char value at the specified 0-based index. Throws StringIndexOutOfBoundsException for invalid indices (unlike some languages that return null). Works with characters up to U+FFFF. For supplementary Unicode characters (code points above U+FFFF), use codePointAt(i). |
s.substring(start, end) | Returns a new String from index start (inclusive) to end (exclusive). Without end, extracts from start to the end of the string. Throws StringIndexOutOfBoundsException for invalid bounds. Creates a new String object — Java Strings are immutable so no mutation occurs. |
s.toUpperCase() | Returns a new String with all characters converted to uppercase using the rules of the default locale. Does not modify the original String (immutable). For locale-specific conversion (e.g., Turkish dotted/dotless i), pass a Locale argument: s.toUpperCase(Locale.ENGLISH). |
s.toLowerCase() | Returns a new String with all characters converted to lowercase using the default locale. Strings in Java are immutable — this creates a new String. For case-insensitive comparisons, prefer equalsIgnoreCase() over converting both to the same case, to avoid locale-sensitive pitfalls. |
s.trim() | Returns a copy of the string with leading and trailing whitespace (characters with code point ≤ '\u0020') removed. Does not remove all Unicode whitespace — for that, use strip() (Java 11+) which uses Unicode character definitions. Neither method modifies the original String. |
s.split(regex) | Splits the string around matches of the given regular expression and returns a String array. The limit parameter controls the maximum number of parts. Be careful with special regex characters in the separator — escape them: "." must be "\\.". Trailing empty strings are discarded by default. |
s.replace(old, new) | Returns a new String with all occurrences of old (a char or CharSequence) replaced by new. Unlike replaceAll(), the first argument is a literal string, not a regex — making it safer for replacements involving regex special characters. Does not modify the original String. |
s.contains(sub) | Returns true if this string contains the specified sequence of characters anywhere within it. Equivalent to s.indexOf(sub) != -1 but more readable. Case-sensitive. For regex-based searching, use s.matches(regex) or Pattern.matcher(s).find(). |
s.startsWith(prefix) | Returns true if this string begins with the specified prefix. Case-sensitive. An optional toffset argument tests whether a suffix of the string (starting at toffset) begins with the given prefix. Useful for URL scheme detection, protocol checks, and prefix validation. |
s.endsWith(suffix) | Returns true if this string ends with the specified suffix. Case-sensitive. Commonly used for file extension checks (e.g., filename.endsWith(".csv")) and suffix validation. For case-insensitive checks, combine with toLowerCase(). |
s.indexOf(sub) | Returns the index of the first occurrence of sub within this string, or -1 if not found. An optional fromIndex argument starts the search from that position. Use lastIndexOf() to find the last occurrence. The search is case-sensitive and scans left to right. |
s.equals(other) | Compares this string to other for character-by-character content equality. Returns true only if both strings contain exactly the same characters in the same order. Critical: NEVER use == to compare Strings in Java — it tests reference equality (same object in memory), not content equality. |
s.equalsIgnoreCase(other) | Compares two strings for content equality, ignoring case differences. More reliable than converting both strings to the same case with toLowerCase() before comparing, especially across locales. Returns true if the strings are equal ignoring case considerations. |
Arrays
| Syntax | Description |
|---|---|
int[] arr = new int[size] | Declares and allocates an array of size integers, initialized to default values (0 for int, null for objects, false for boolean). Arrays are objects in Java with a fixed size set at creation. Multidimensional arrays: int[][] matrix = new int[rows][cols]. |
int[] arr = {1, 2, 3} | Array initializer shorthand that declares, allocates, and initializes in one expression. The size is inferred from the number of provided elements. This syntax can only be used in a declaration statement — not when assigning to an already-declared variable (use new int[]{1,2,3} for that). |
arr.length | A final field (not a method) that returns the number of elements in the array. Fixed at creation and cannot be changed. Note: unlike Strings, arrays use .length (no parentheses). For dynamically resizable collections, use ArrayList with the .size() method instead. |
arr[i] | Accesses or sets the element at 0-based index i. Reading beyond valid indices (0 to length-1) throws ArrayIndexOutOfBoundsException at runtime. Java performs bounds checking on every array access, unlike C/C++. Use arr[arr.length - 1] for the last element. |
Arrays.sort(arr) | Sorts the array in place (modifies the original) in ascending natural order. Uses a dual-pivot Quicksort for primitives and a stable merge sort (TimSort) for object arrays. For object arrays, elements must implement Comparable, or provide a Comparator: Arrays.sort(arr, comparator). Requires import java.util.Arrays. |
Arrays.toString(arr) | Returns a string representation of the array contents in the format [1, 2, 3]. The default arr.toString() returns a cryptic memory address string — always use Arrays.toString() for readable output. For multidimensional arrays, use Arrays.deepToString(). |
Arrays.copyOf(arr, len) | Copies the array, truncating or padding with default values (0 for int, null for objects) to produce an array of exactly len elements. Useful for growing arrays (new length larger than original) or trimming them. The original array is not modified. For partial copies with an offset, use Arrays.copyOfRange(arr, from, to). |
Arrays.fill(arr, val) | Assigns the specified value to every element in the array. Modifies the array in place. Useful for resetting arrays or initializing them with a non-default value. For partial fill, use Arrays.fill(arr, fromIndex, toIndex, val). Works for all primitive types and object arrays. |
Collections - ArrayList
| Method | Description |
|---|---|
ArrayList<Type> list = new ArrayList<>() | Creates a resizable array-backed list. The diamond operator <> infers the type from the declaration. Backed by an array that grows automatically (doubles capacity when full, amortized O(1) add). Allows null elements and duplicates. Use the interface type: List<Type> list = new ArrayList<>() for flexibility. Requires import java.util.ArrayList. |
list.add(x) | Appends the specified element to the end of the list. Always returns true (for compatibility with the Collection interface). Triggers array resizing if capacity is exceeded. For adding at a specific position, use the two-argument form. Throws NullPointerException if null is prohibited by the collection. |
list.add(i, x) | Inserts element x at index i, shifting all subsequent elements one position to the right. O(n) operation due to element shifting. Throws IndexOutOfBoundsException if i is negative or greater than list.size(). Inserting at index 0 is particularly expensive for large lists. |
list.get(i) | Returns the element at the specified 0-based index. O(1) constant time (backed by array). Throws IndexOutOfBoundsException if the index is out of range. Unlike arrays, ArrayList always boxes primitive types to their wrapper objects (e.g., int → Integer). |
list.set(i, x) | Replaces the element at index i with x and returns the old element. O(1) operation (direct array access). Does not change the list size. Throws IndexOutOfBoundsException for invalid indices. Useful for in-place updates without remove+insert overhead. |
list.remove(i) | Removes and returns the element at index i. Shifts all subsequent elements left by one (O(n)). Watch out for ambiguity: list.remove(Integer.valueOf(x)) removes by object value, while list.remove(int i) removes by index. Auto-boxing an int argument chooses the index version. |
list.size() | Returns the current number of elements in the list (not the internal array capacity). O(1). Unlike arrays, the size changes dynamically. Use list.isEmpty() as a cleaner equivalent to list.size() == 0 for emptiness checks. |
list.contains(x) | Returns true if the list contains at least one element equal to x (determined by equals()). O(n) linear search. For frequent membership testing, consider using a HashSet instead (O(1) average). For null elements, passes through to equals() checks correctly. |
list.clear() | Removes all elements from the list, leaving it empty. The internal array capacity is retained (no shrinking). O(n) to null out all references (prevents memory leaks). The list can be repopulated after clearing. Use list = new ArrayList<>() to also release the backing array memory. |
list.isEmpty() | Returns true if the list contains no elements. More readable and semantically clear than list.size() == 0. O(1). Part of the Collection interface — works on all collection types. Commonly used as a guard condition before iterating or processing the list. |
Collections - HashMap
| Method | Description |
|---|---|
HashMap<K, V> map = new HashMap<>() | Creates a hash table-based map that stores key-value pairs. Keys must be unique; values can be duplicated. O(1) average time for get/put/remove. Does not maintain insertion order (use LinkedHashMap for ordered iteration or TreeMap for sorted key order). Keys and values can be null. Requires import java.util.HashMap. |
map.put(key, value) | Associates the specified value with the key. If the key already exists, replaces the old value and returns it; if the key is new, returns null. O(1) average time. For conditional insertion (only if key is absent), prefer map.putIfAbsent(key, value) to avoid unintentional overwrites. |
map.get(key) | Returns the value mapped to the specified key, or null if the key doesn't exist (or if the key is explicitly mapped to null). O(1) average. When null values are possible, use map.containsKey(key) to distinguish "key not present" from "key mapped to null." Alternative: map.getOrDefault(key, defaultValue). |
map.remove(key) | Removes the mapping for the specified key and returns the associated value (or null if the key wasn't present). O(1) average. The conditional form map.remove(key, value) only removes the entry if the key is currently mapped to the specified value, making it useful for atomic operations. |
map.containsKey(key) | Returns true if the map contains a mapping for the specified key. O(1) average. More reliable than map.get(key) != null because it correctly handles keys explicitly mapped to null. Use this before get() if null values are a valid state in your map. |
map.containsValue(val) | Returns true if the map maps one or more keys to the specified value. O(n) — requires a linear scan of all values. Use sparingly for large maps. If you frequently need to look up by value, consider maintaining a reverse map (HashMap<V, K>) alongside the original. |
map.keySet() | Returns a Set view of all keys in the map. Modifying the set (e.g., remove) reflects in the map and vice versa. Iteration order is undefined for HashMap. Commonly used in enhanced for loops: for (K key : map.keySet()) { ... }. For key-value pair iteration, use entrySet() instead. |
map.values() | Returns a Collection view of all values in the map. Like keySet(), it reflects changes and is live. Values can be duplicated (unlike keys). Useful for aggregating or filtering all values without needing their keys. For full key-value access, use map.entrySet() and iterate over Map.Entry objects. |
map.size() | Returns the number of key-value mappings in the map. O(1). Not the capacity of the internal hash table — just the count of current entries. Use map.isEmpty() for a cleaner empty check. The size increases with put() for new keys and decreases with remove(). |
OOP Concepts
| Syntax | Description |
|---|---|
public class ClassName {} | Declares a public class accessible from any other class in any package. Encapsulates data (fields) and behavior (methods) into a reusable unit. Java enforces that a file named ClassName.java can only have one public class. Classes can also be package-private (no modifier), abstract, or final. |
private int x | Declares a field with private access — visible only within the declaring class itself. The foundation of encapsulation: hides implementation details and enforces controlled access through public getters and setters. Other access levels: protected (class + subclasses + same package), public (everywhere). |
public ClassName() {} | A constructor method used to initialize new instances of the class. Has the same name as the class and no return type. If no constructor is defined, Java provides a default no-arg constructor. Multiple constructors can be overloaded with different parameter lists. Use this() to delegate to another constructor. |
public Type method() {} | Declares an instance method with the specified return type. void methods return nothing. Methods define the behavior of the class and can access all instance fields. Java supports method overloading (same name, different parameter types). The return statement exits the method and provides the return value. |
this.field | Refers to the current object instance — used to distinguish instance variables from local variables or parameters with the same name. Also used in constructor chaining: this() calls another constructor of the same class. In non-static methods, this always refers to the calling object. |
extends ParentClass | Establishes inheritance — the subclass inherits all non-private fields and methods from the parent (superclass). Java supports single inheritance for classes (one parent only). The subclass can override inherited methods and use super.method() to call the parent's version. All classes implicitly extend Object. |
implements Interface | Declares that the class implements the specified interface(s), committing to providing concrete implementations of all abstract methods. A class can implement multiple interfaces (Java's solution to multiple inheritance). Interfaces define a contract (API) without implementation — enabling polymorphism across unrelated class hierarchies. |
@Override | An annotation that signals the compiler the annotated method is intended to override a method in a superclass or interface. If no matching method exists in the parent (due to a typo or signature mismatch), the compiler reports an error. Strongly recommended for all overrides — improves correctness and readability. |
abstract class/method | Abstract classes cannot be instantiated directly — they serve as templates for subclasses. Abstract methods have no implementation body and must be overridden by concrete subclasses. A class with at least one abstract method must be declared abstract. Abstract classes can have constructors, concrete methods, and fields. |
static | The static modifier makes a field or method belong to the class itself rather than to any specific instance. Static members are shared across all instances. Accessed via the class name: ClassName.method(). Static methods cannot access instance fields or use this. Commonly used for utility methods and constants. |
final | When applied to a class, prevents it from being subclassed (e.g., String, Integer). When applied to a method, prevents it from being overridden. When applied to a variable, makes it a constant that can only be assigned once. Enables compiler optimizations and communicates design intent clearly. |
T-SQL Quick Reference
Basic Queries
| Syntax | Description |
|---|---|
SELECT columns FROM table | Retrieves specified columns from a table (or view). Column names can include table aliases (t.column), expressions (Price * Qty AS Total), and aggregate functions. The SELECT clause is evaluated last in the logical query order, even though it appears first in the written syntax. |
SELECT * FROM table | Retrieves all columns from the table in their defined order. Convenient for exploration but discouraged in production code — it returns all columns (including future ones added via ALTER TABLE), increases network overhead, and makes code brittle. Always explicitly list required columns in production queries. |
SELECT DISTINCT column FROM table | Returns only unique values of the specified column(s), eliminating duplicate rows from the result set. Works across multiple columns — SELECT DISTINCT col1, col2 deduplicates on the combination. Note: DISTINCT forces a sort operation and can be expensive on large datasets; consider if a GROUP BY would serve better. |
WHERE condition | Filters rows before grouping and aggregation. Evaluated row-by-row before GROUP BY. Supports comparison operators (=, <, >, <>), pattern matching (LIKE, IN), range checks (BETWEEN), null checks (IS NULL), and logical operators (AND, OR, NOT). Cannot reference column aliases from SELECT. |
ORDER BY column ASC|DESC | Sorts the result set by one or more columns. ASC (ascending, default) or DESC (descending). Can order by multiple columns: ORDER BY last_name ASC, first_name DESC. Can reference SELECT column aliases. NULL values sort first in ascending order by default. Required for deterministic pagination with OFFSET/FETCH. |
GROUP BY column | Groups rows with identical values in the specified column(s) into summary rows. All columns in SELECT that are not wrapped in aggregate functions must appear in GROUP BY. Evaluated after WHERE but before HAVING. Enables aggregate functions (COUNT, SUM, AVG, MIN, MAX) to operate on each group independently. |
HAVING condition | Filters groups after aggregation — the group-level equivalent of WHERE. Unlike WHERE, HAVING can reference aggregate functions (e.g., HAVING COUNT(*) > 5). WHERE filters individual rows before grouping; HAVING filters aggregated groups after grouping. Both can be used together in the same query. |
TOP n | Limits the result set to the first n rows. T-SQL specific syntax (equivalent to LIMIT in MySQL/PostgreSQL). Add WITH TIES to include all rows tied for the last position. Without ORDER BY, the rows returned are non-deterministic. Use TOP (n) PERCENT to retrieve a percentage of rows. |
OFFSET n ROWS FETCH NEXT m ROWS ONLY | Standard SQL syntax for pagination. Skips n rows then returns the next m rows. Requires an ORDER BY clause (otherwise results are non-deterministic). More standard and portable than proprietary TOP. Use to implement paged API responses efficiently. Both OFFSET and FETCH values can be variables or expressions. |
Joins
| Syntax | Description |
|---|---|
INNER JOIN table2 ON condition | Returns only rows where the join condition is satisfied in both tables. Unmatched rows from either table are excluded. The most common and efficient join type. Multiple tables can be joined in a single query by chaining JOIN clauses. The ON condition typically matches primary key to foreign key. |
LEFT JOIN table2 ON condition | Returns all rows from the left (first) table and matching rows from the right table. Where no match exists in the right table, NULL is returned for all right-table columns. Useful for finding records without a match: add WHERE table2.id IS NULL to find orphaned records. |
RIGHT JOIN table2 ON condition | Returns all rows from the right (second) table and matching rows from the left table. Less commonly used than LEFT JOIN — most scenarios can be rewritten as a LEFT JOIN by reversing table order. Where no match exists in the left table, NULLs are returned for left-table columns. |
FULL OUTER JOIN table2 ON condition | Returns all rows from both tables. Where a row from either table has no match in the other, NULLs fill the unmatched columns. Useful for data reconciliation tasks where you need to see all records from both sides, including unmatched ones. Equivalent to a LEFT JOIN UNION ALL RIGHT JOIN WHERE one side is NULL. |
CROSS JOIN table2 | Produces the Cartesian product of two tables — every row from the left table paired with every row from the right table. Result set size is rows_left × rows_right. No ON condition. Useful for generating combinations, test data, or date dimension tables. Use with extreme caution on large tables — can produce millions of rows. |
Aggregate Functions
| Function | Description |
|---|---|
COUNT(*) | Counts all rows in the group (or table), including rows with NULL values. * means "count every row, regardless of content." The most commonly used aggregate function. Always returns an integer. Can be used without GROUP BY to count all rows in a table. |
COUNT(column) | Counts the number of non-NULL values in the specified column. Unlike COUNT(*), ignores NULL values. Useful for checking data completeness. Use COUNT(DISTINCT column) to count unique non-null values. The difference between COUNT(*) and COUNT(column) tells you how many NULLs exist. |
SUM(column) | Returns the total sum of all non-NULL numeric values in the column. Returns NULL if all values are NULL (not zero). Use COALESCE(SUM(column), 0) to get zero when all values are NULL. Can be used with DISTINCT: SUM(DISTINCT column) sums only unique values. |
AVG(column) | Returns the arithmetic mean of all non-NULL numeric values. Like SUM, ignores NULL values in the denominator — meaning it divides the sum by the count of non-NULL rows, not total rows. For NULL-aware averages, use SUM(column) / COUNT(*) to include NULLs as zero in the denominator. |
MIN(column) | Returns the smallest value in the column, ignoring NULLs. Works with numeric, date/time, and character data types. For dates, returns the earliest date. Useful in GROUP BY queries to find the first event per group. Can be used without GROUP BY to find the minimum across all rows. |
MAX(column) | Returns the largest value in the column, ignoring NULLs. Works with numeric, date/time, and character types. For dates, returns the most recent date. Commonly used to find the latest transaction, highest score, or most recent record per group in GROUP BY queries. |
String Functions
| Function | Description |
|---|---|
LEN(str) | Returns the number of characters in a string, excluding trailing spaces. Returns 0 for empty strings and NULL for NULL input. Note: counts characters (not bytes), so multi-byte characters count as 1. Use DATALENGTH(str) to get the actual byte size of the stored value. |
UPPER(str) | Converts all lowercase characters in the string to uppercase. Non-alphabetic characters are unchanged. Returns NULL for NULL input. Commonly used before comparisons in case-insensitive scenarios or for normalizing data during ETL. Collation settings may affect behavior with accented characters. |
LOWER(str) | Converts all uppercase characters in the string to lowercase. Non-alphabetic characters are unaffected. Returns NULL if the input is NULL. Often used in conjunction with UPPER() to normalize text data before loading into a data warehouse or performing case-insensitive deduplication. |
LTRIM(str), RTRIM(str), TRIM(str) | LTRIM() removes leading (left-side) spaces; RTRIM() removes trailing (right-side) spaces; TRIM() (SQL Server 2017+) removes both leading and trailing spaces or specified characters. Essential for cleaning user-entered or imported data where inconsistent spacing is common. |
SUBSTRING(str, start, len) | Extracts a substring starting at 1-based position start for len characters. If start + len exceeds the string length, returns the remainder of the string without error. Unlike most languages, T-SQL uses 1-based indexing. Returns an empty string if start exceeds the string length. |
REPLACE(str, old, new) | Replaces all occurrences of substring old with new in the string. Case-insensitive by default (determined by the column's collation). Returns NULL if any argument is NULL. Useful for data cleansing, fixing consistent data entry errors, and text transformation in ETL pipelines. |
CONCAT(str1, str2, ...) | Concatenates two or more strings into one. Treats NULL arguments as empty strings (unlike the + operator which propagates NULL). Supports up to 254 arguments. Added in SQL Server 2012. Preferred over + for robust concatenation when any input might be NULL. CONCAT_WS(sep, ...) adds a separator between values. |
str1 + str2 | The string concatenation operator in T-SQL. Concatenates two string expressions, but propagates NULL — if either operand is NULL, the result is NULL. Requires explicit conversion for non-string types: CAST(id AS VARCHAR) + ' ' + name. Use CONCAT() for NULL-safe concatenation of multiple values. |
CHARINDEX(sub, str) | Returns the 1-based starting position of the first occurrence of sub within str. Returns 0 (not -1) if the substring is not found. An optional third argument specifies the start position of the search. Case sensitivity is determined by the column's collation. Commonly used with SUBSTRING() to extract dynamic-length text segments. |
LEFT(str, n), RIGHT(str, n) | LEFT(str, n) returns the first n characters from the left side of the string. RIGHT(str, n) returns the last n characters. Both return NULL for NULL input and handle n greater than string length gracefully (returns the whole string). Useful for extracting fixed-length codes, prefixes, or suffixes. |
Date Functions
| Function | Description |
|---|---|
GETDATE() | Returns the current database server date and time as a datetime value (no timezone). For timezone-aware timestamps, use SYSDATETIMEOFFSET(). For higher precision, use SYSDATETIME() (datetime2). Use GETUTCDATE() for the UTC equivalent. Commonly used as a default column value for audit timestamps. |
DATEADD(unit, n, date) | Returns a new date/time value by adding n units to the specified date. Unit can be: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND. n can be negative for subtraction. E.g., DATEADD(DAY, -7, GETDATE()) returns last week's date. |
DATEDIFF(unit, date1, date2) | Returns the number of specified unit boundaries crossed between two dates. Always returns an integer — counts complete crossings of the boundary, not exact durations. E.g., DATEDIFF(DAY, '2024-01-31', '2024-02-01') → 1. For business days, a tally table or custom function is needed. |
DATEPART(unit, date) | Extracts a specific component of a date as an integer. Units: YEAR, QUARTER, MONTH, WEEK, DAY, WEEKDAY, HOUR, MINUTE, SECOND. Equivalent shorthand functions exist: YEAR(date), MONTH(date), DAY(date). Use in GROUP BY to aggregate by time period. |
YEAR(date), MONTH(date), DAY(date) | Convenience shorthand functions for extracting the year, month (1–12), or day (1–31) from a date value respectively. Equivalent to DATEPART(YEAR, date), etc. Return an integer. Commonly used in GROUP BY clauses for time-series aggregation (e.g., monthly totals) or in WHERE conditions to filter by specific periods. |
FORMAT(date, format) | Formats a date, number, or other value as a string using .NET format strings (e.g., FORMAT(GETDATE(), 'yyyy-MM-dd')). Flexible but significantly slower than CONVERT() for high-volume queries due to .NET CLR dependency. Use CONVERT() with a style code for performance-critical code. Culture parameter supports locale-specific formatting. |
CONVERT(type, value, style) | Converts a value from one data type to another with optional format style for dates. The style parameter specifies the date/time string format (e.g., 101 = MM/DD/YYYY, 103 = DD/MM/YYYY, 120 = YYYY-MM-DD HH:MM:SS). Faster than FORMAT(). Also used for explicit type casting: CONVERT(INT, '42'). |
Data Manipulation
| Statement | Description |
|---|---|
INSERT INTO table VALUES (...) | Inserts a row with values for every column in the table's defined order. Fragile — adding or reordering columns later will break this syntax. Values must match column count and data types exactly. For identity columns, use SET IDENTITY_INSERT table ON to supply explicit values. |
INSERT INTO table (cols) VALUES (...) | The preferred and safer INSERT form — explicitly lists the target columns, so column order doesn't matter and new columns with defaults are excluded automatically. Also supports inserting multiple rows: VALUES (r1c1, r1c2), (r2c1, r2c2). Use INSERT INTO ... SELECT ... to insert results of a query. |
UPDATE table SET col = val WHERE condition | Modifies existing rows matching the WHERE condition. Always include a WHERE clause — omitting it updates every row in the table (rarely intended). Test with a SELECT using the same WHERE clause before running. Use UPDATE ... FROM to update rows based on values from a JOIN with another table. |
DELETE FROM table WHERE condition | Removes rows matching the WHERE condition. Always verify the WHERE clause first — omitting it deletes all rows (use TRUNCATE for that). DELETE is logged row-by-row and fires DELETE triggers. Can be rolled back within a transaction. Use DELETE t FROM table t JOIN ... to delete based on joined table data. |
TRUNCATE TABLE table | Removes all rows from a table in a minimally-logged operation (much faster than DELETE for full-table removal). Resets identity columns to their seed value. Cannot be used when the table is referenced by a foreign key constraint. Cannot be rolled back easily in some configurations. Does not fire DELETE triggers. |
Table Operations
| Statement | Description |
|---|---|
CREATE TABLE table (col type, ...) | Creates a new table with the specified columns and their data types. Columns can include inline constraints (NOT NULL, PRIMARY KEY, DEFAULT, UNIQUE, CHECK). Multi-column constraints are defined at the table level. Consider schema prefix: CREATE TABLE dbo.TableName. Use IF NOT EXISTS for idempotent scripts. |
ALTER TABLE table ADD column type | Adds a new column to an existing table. For large production tables, adding a NOT NULL column without a default can lock the table. With a DEFAULT value, SQL Server 2012+ stores it as metadata without physically filling it (instant schema change). Use ALTER TABLE ... ALTER COLUMN to change an existing column's type or nullability. |
ALTER TABLE table DROP COLUMN column | Permanently removes a column from an existing table. Cannot drop a column that is part of a constraint, index, or computed column reference — remove those first. This is a destructive, irreversible operation; confirm data can be discarded or archived. On large tables, this can be a long-running operation. |
DROP TABLE table | Permanently deletes the table and all its data, indexes, triggers, and constraints. Irreversible without a backup. Cannot drop tables referenced by FOREIGN KEY constraints in other tables — drop those foreign keys first. Use DROP TABLE IF EXISTS table (SQL Server 2016+) for safe, idempotent drop scripts. |
PRIMARY KEY | Uniquely identifies each row in the table. Enforces both uniqueness and NOT NULL. A table can have only one primary key (which can span multiple columns — composite key). SQL Server automatically creates a clustered index on the primary key unless you specify NONCLUSTERED. Commonly an auto-incrementing IDENTITY column. |
FOREIGN KEY REFERENCES table(col) | Establishes a referential integrity constraint between two tables. The foreign key column's values must exist in the referenced table's column (or be NULL if nullable). Prevents orphaned child records and cascading issues. Configure cascade behavior: ON DELETE CASCADE, ON DELETE SET NULL, or ON DELETE NO ACTION. |
NOT NULL | A column constraint that prevents NULL values from being stored in that column. INSERT or UPDATE attempts that would store NULL raise an error. Columns defined as NOT NULL must have a value provided at INSERT (or have a DEFAULT). Adding NOT NULL to an existing column with NULL values requires first updating or removing those NULLs. |
UNIQUE | Enforces that all values in the column (or column combination) are distinct across all rows. Unlike PRIMARY KEY, a table can have multiple UNIQUE constraints, and UNIQUE columns can contain NULL (but only one NULL per column in SQL Server). Creates a unique index automatically. Used for alternate keys like email addresses or usernames. |
DEFAULT value | Specifies a value to use when no value is supplied during INSERT for that column. The default can be a literal, a function call (GETDATE()), or a system function. Column defaults simplify INSERT statements by omitting the column entirely. Can be named explicitly (CONSTRAINT DF_Name DEFAULT value) to allow later modification with ALTER TABLE. |
Control Flow
| Statement | Description |
|---|---|
IF condition BEGIN ... END | Conditional execution block. BEGIN...END groups multiple statements together (like braces in C#/Java). If the body is a single statement, BEGIN/END is optional but strongly recommended for clarity. Use IF...ELSE IF...ELSE for multi-branch logic. Commonly used in stored procedures and scripts for data validation and branching logic. |
CASE WHEN ... THEN ... END | A SQL expression for conditional value selection, usable in SELECT, WHERE, ORDER BY, and GROUP BY clauses. Two forms: searched CASE (CASE WHEN condition THEN value) for complex conditions, and simple CASE (CASE col WHEN val THEN result) for equality checks. Add ELSE default_value to handle unmatched cases. Returns NULL if no WHEN matches and ELSE is absent. |
WHILE condition BEGIN ... END | Executes a block of statements repeatedly as long as the condition is TRUE. Use BREAK to exit the loop early; CONTINUE to skip to the next iteration. T-SQL lacks a FOR loop — WHILE is the primary loop construct. Use sparingly in production — set-based operations are almost always faster than row-by-row loops in SQL. |
BEGIN TRY ... END TRY | Marks the start and end of a block where exceptions are caught. If any statement inside the TRY block raises an error (severity 11 or higher), control jumps immediately to the paired CATCH block. Errors below severity 11 or compilation errors are not caught. Nesting TRY/CATCH blocks is supported. |
BEGIN CATCH ... END CATCH | Handles errors caught from the preceding TRY block. Error details are accessible via: ERROR_MESSAGE(), ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_LINE(). Use THROW (SQL Server 2012+) or RAISERROR() to re-raise errors after logging. Can include ROLLBACK TRANSACTION for transactional error recovery. |
PL/SQL Quick Reference
Basic Structure
| Syntax | Description |
|---|---|
DECLARE ... BEGIN ... END; | The fundamental PL/SQL anonymous block structure. The optional DECLARE section declares variables, constants, and cursor definitions. The mandatory BEGIN...END section contains the executable statements. An optional EXCEPTION section handles errors. Blocks can be nested — inner blocks can have their own DECLARE sections and exception handlers. |
variable_name datatype; | Declares a variable in the DECLARE section. The variable is initialized to NULL by default until explicitly assigned. Variable names follow Oracle naming rules (cannot start with a number, case-insensitive). Local variables shadow package or global variables of the same name within their scope. |
variable_name datatype := value; | Declares and immediately initializes a variable. The := operator is the PL/SQL assignment operator (not =). The initial value expression is evaluated at block entry. This is equivalent to declaring the variable and then assigning in the BEGIN section, but more concise and self-documenting. |
CONSTANT name datatype := value; | Declares a constant that cannot be modified after initialization (attempting to do so causes a compile-time error). Must be initialized at declaration. Useful for magic numbers, lookup values, and configuration constants in procedures and packages. Constants are scoped to their declaring block. |
%TYPE | Anchors a variable's data type to a specific table column or another variable. E.g., emp_salary employees.salary%TYPE automatically adopts the same data type as the salary column. Prevents type mismatches when schema changes occur and makes code self-documenting. The type is resolved at compile time. |
%ROWTYPE | Declares a record variable whose structure mirrors an entire row of a table, view, or cursor. Each column becomes a field of the record, accessible via dot notation (e.g., rec.employee_id). Automatically adapts to schema changes. Commonly used with cursors: FOR rec IN cursor LOOP. |
DBMS_OUTPUT.PUT_LINE(text) | Writes a line of text to the DBMS_OUTPUT buffer, viewable in SQL*Plus or Oracle SQL Developer when output is enabled (SET SERVEROUTPUT ON). Used for debugging — not for production output. Buffer size defaults to 20,000 bytes. For large outputs, increase with DBMS_OUTPUT.ENABLE(buffer_size). |
Control Structures
| Syntax | Description |
|---|---|
IF condition THEN ... END IF; | Executes the enclosed statements only if the condition evaluates to TRUE. NULL conditions are treated as FALSE. The condition can be any boolean expression. Unlike many languages, PL/SQL requires the keyword THEN and terminates with END IF (two words). Can be nested for complex conditional logic. |
IF ... ELSIF ... ELSE ... END IF; | Multi-branch conditional statement. Note the spelling is ELSIF (not ELSE IF or ELSEIF). Conditions are evaluated in order — the first TRUE branch executes and the rest are skipped. The ELSE branch is optional and handles all unmatched cases. More readable than deeply nested IF statements. |
CASE var WHEN ... THEN ... END CASE; | A statement form of CASE (not to be confused with SQL's CASE expression). Simple CASE: CASE var WHEN val1 THEN ... WHEN val2 THEN .... Searched CASE: CASE WHEN condition1 THEN ... WHEN condition2 THEN .... ELSE clause handles unmatched values. Raises CASE_NOT_FOUND if no WHEN matches and there is no ELSE. |
LOOP ... EXIT WHEN condition; END LOOP; | The basic (infinite) loop that runs until an EXIT statement is encountered. EXIT WHEN condition exits when the condition is TRUE. Always ensure a reachable exit condition to prevent infinite loops. Can also use unconditional EXIT; typically inside an IF block. Most flexible loop construct in PL/SQL. |
WHILE condition LOOP ... END LOOP; | Executes the loop body zero or more times, checking the condition before each iteration. If the condition is FALSE or NULL on the first check, the body is never executed. Use EXIT WHEN or EXIT inside for early termination. Suitable when the number of iterations is not known in advance. |
FOR i IN 1..10 LOOP ... END LOOP; | Numeric FOR loop that automatically declares the loop counter i as an integer (no DECLARE needed) and iterates over the specified range. The counter increments by 1 each iteration. Add REVERSE after IN to iterate downward. The loop variable is read-only and cannot be modified inside the loop. Range bounds are evaluated once at loop start. |
EXIT; | Unconditionally exits the innermost loop, transferring control to the statement after END LOOP. Use EXIT label to exit a specific outer loop when loops are nested with labels. Typically used inside an IF block within a basic LOOP. The conditional form EXIT WHEN condition is preferred for clarity. |
CONTINUE; | Skips the remaining statements in the current loop iteration and proceeds to the next iteration (evaluating the loop condition or incrementing the counter). Use CONTINUE WHEN condition for conditional skip. Use CONTINUE label to continue an outer loop. Introduced in Oracle Database 11g. |
Cursors
| Syntax | Description |
|---|---|
CURSOR cursor_name IS SELECT ... | Declares an explicit cursor in the DECLARE section with a named SELECT query. Explicit cursors provide full manual control over the query lifecycle: OPEN, FETCH, CLOSE. Cursor parameters can be declared for dynamic filtering: CURSOR c(p_dept IN NUMBER) IS SELECT .... Named cursors can be reused and are more readable than implicit SQL attributes. |
OPEN cursor_name; | Executes the cursor's SELECT query and initializes the result set. Memory is allocated for the active set. Must be called before FETCH. Opening an already-open cursor raises an error. For parameterized cursors, pass arguments: OPEN cursor_name(param_value). Always balance every OPEN with a corresponding CLOSE. |
FETCH cursor_name INTO variable; | Retrieves the next row from the cursor's result set into the specified variable(s). Advances the cursor pointer by one row. If the query returns multiple columns, FETCH into a %ROWTYPE variable or a comma-separated list of variables. After the last row, subsequent FETCHes leave variables unchanged and set %NOTFOUND to TRUE. |
CLOSE cursor_name; | Releases the resources (memory and lock on the query's active set) held by the open cursor. Always close cursors explicitly when finished, especially in procedures and functions, to prevent resource leaks. Closing an already-closed or never-opened cursor raises INVALID_CURSOR exception. Use exception handlers to ensure cursors are closed even on error. |
%FOUND | A cursor attribute that evaluates to TRUE if the most recent FETCH returned a row, FALSE if it didn't (past end of result set), or NULL if the cursor hasn't been fetched yet. Also works on implicit cursors from DML statements (INSERT/UPDATE/DELETE): SQL%FOUND indicates whether rows were affected. |
%NOTFOUND | The complement of %FOUND — evaluates to TRUE when the last FETCH returned no row. The standard way to detect end-of-data in a LOOP: EXIT WHEN cursor%NOTFOUND. For implicit cursors after DML: SQL%NOTFOUND is TRUE if no rows were affected by the statement. |
%ROWCOUNT | For explicit cursors, returns the total number of rows fetched so far from the cursor. For implicit cursors (SQL%ROWCOUNT), returns the number of rows affected by the most recent INSERT, UPDATE, DELETE, or MERGE statement. Returns 0 if no rows were affected. Useful for logging, validation, and conditional processing. |
FOR record IN cursor LOOP ... END LOOP; | The cursor FOR loop automatically handles OPEN, FETCH (one row per iteration), and CLOSE — eliminating the need to manage them manually. Oracle implicitly declares the loop record variable matching the cursor's row structure. Also works with inline SELECT: FOR rec IN (SELECT ...) LOOP. The cleanest and safest pattern for row-by-row processing. |
Exception Handling
| Syntax | Description |
|---|---|
EXCEPTION WHEN ... THEN ... | The EXCEPTION section of a PL/SQL block handles errors raised during execution. Each WHEN clause specifies an exception name and the recovery actions. If an exception is raised and no handler exists in the current block, it propagates to the enclosing block. Exception handlers are tried in order — only the first matching handler executes. |
NO_DATA_FOUND | A predefined Oracle exception raised when a SELECT INTO statement returns no rows. Also raised by certain collection methods when accessing a non-existent element. Does not apply to cursor FOR loops or explicit cursors. Handle this to provide a graceful fallback when a lookup finds no matching record. |
TOO_MANY_ROWS | Raised when a SELECT INTO statement returns more than one row (only one row is allowed in SELECT INTO). This typically indicates a logic error — the query should have included additional WHERE conditions. Handle by either using a cursor for multi-row results or adding constraints to ensure uniqueness. |
ZERO_DIVIDE | Raised when an arithmetic division by zero is attempted (e.g., x := 10 / 0). To prevent this proactively, use a CASE expression or NULLIF: 10 / NULLIF(divisor, 0) returns NULL instead of raising an exception. Corresponds to Oracle error ORA-01476. |
OTHERS | A catch-all exception handler that matches any exception not matched by a preceding WHEN clause. Equivalent to a default/catch-all in other languages. Always obtain error details using SQLCODE and SQLERRM inside the OTHERS handler. Good practice: log the error and then re-raise with RAISE to preserve the original error details. |
RAISE exception_name; | Explicitly raises a named exception, immediately transferring control to the nearest matching exception handler. In an exception handler, bare RAISE; (without a name) re-raises the current exception while preserving the original error code and backtrace — the correct way to rethrow after logging. Custom exceptions must be declared in the DECLARE section as my_error EXCEPTION;. |
RAISE_APPLICATION_ERROR(code, msg) | Raises a user-defined error with a custom error code and message, which is returned to the calling application just like an Oracle system error. The code must be between -20999 and -20000 (Oracle's reserved range for user-defined errors). The message can be up to 2,048 characters. Use instead of generic exceptions to communicate domain-specific business rule violations. |
SQLCODE | A built-in function inside an exception handler that returns the numeric error code of the current exception. Returns 0 if no error has occurred. Returns 1 for user-defined exceptions. Negative values (like -1403 for NO_DATA_FOUND) correspond to Oracle system errors. Use to programmatically distinguish between different error types in an OTHERS handler. |
SQLERRM | A built-in function inside an exception handler that returns the error message text for the current exception (up to 512 characters). When called with SQLCODE as an argument (e.g., SQLERRM(SQLCODE)), returns the message for any Oracle error code. Commonly used with DBMS_OUTPUT or an error log table to capture diagnostic information. |
Procedures & Functions
| Syntax | Description |
|---|---|
CREATE PROCEDURE name (params) IS ... | Creates a named, stored procedure in the database. Procedures perform actions but do not return a value directly (output is through OUT parameters or DML side effects). Use CREATE OR REPLACE PROCEDURE to overwrite an existing version without dropping it first. Compiled and stored in the data dictionary for repeated execution with minimal overhead. |
CREATE FUNCTION name RETURN type IS ... | Creates a stored function that executes PL/SQL logic and returns a single value to the caller. Can be called within SQL statements (unlike procedures). Functions should be pure (no DML side effects in a SELECT context). The RETURN clause specifies the return data type; the RETURN statement provides the actual return value. |
param_name IN datatype | Declares a read-only input parameter. The caller passes a value in; the procedure/function cannot modify it. IN is the default mode — omitting a mode keyword defaults to IN. Parameters can have default values: param IN NUMBER DEFAULT 0, allowing callers to omit the argument. |
param_name OUT datatype | Declares an output parameter — the procedure assigns a value to it that the caller can read after the call. The initial value of an OUT parameter is NULL inside the called subprogram. The caller must pass a variable (not a literal or expression) as the argument. Allows procedures to "return" multiple values to the caller. |
param_name IN OUT datatype | A bidirectional parameter — the caller supplies a value (readable inside the subprogram) and the subprogram can also modify it (the modification is visible to the caller after the call). Useful for transformation scenarios where the input value is modified and returned. The caller must pass a variable, not a literal. |
RETURN value; | Exits the function immediately and returns the specified value to the caller. Functions must have at least one reachable RETURN statement on every execution path; the compiler warns about potentially missing returns. Procedures use RETURN (without a value) to exit early, like a void return in other languages. |
EXECUTE procedure_name(args); | Calls a stored procedure (abbreviated as EXEC in SQL*Plus and SQL Developer). In a PL/SQL block, procedures are called without the EXECUTE keyword — just procedure_name(args);. Named notation is supported for clarity: procedure_name(p1 => val1, p2 => val2), allowing arguments to be passed out of order. |
DROP PROCEDURE/FUNCTION name; | Permanently removes the specified procedure or function from the database. Any objects that reference the dropped subprogram are marked INVALID and must be recompiled or updated. Use DROP PROCEDURE IF EXISTS (Oracle 23c+) or check USER_OBJECTS before dropping. Consider CREATE OR REPLACE instead of dropping and recreating to preserve permissions. |
Triggers
| Syntax | Description |
|---|---|
CREATE TRIGGER name BEFORE/AFTER ... | Creates a trigger — a PL/SQL block that fires automatically in response to a specified database event. Use CREATE OR REPLACE TRIGGER to update without dropping. Triggers can fire BEFORE or AFTER DML events (INSERT, UPDATE, DELETE) or DDL events. System-level triggers also fire on database startup/shutdown and schema changes. |
BEFORE INSERT OR UPDATE ON table | Specifies the triggering events and timing. BEFORE triggers fire before the DML statement executes — useful for data validation, defaulting values, or modifying the data being inserted/updated. AFTER triggers fire after the statement — useful for auditing, cascading DML, or enforcing complex business rules that need the final committed state. |
FOR EACH ROW | Designates a row-level trigger that fires once for each individual row affected by the DML statement. Without FOR EACH ROW, the trigger fires once per statement (statement-level trigger) regardless of the number of rows affected. Row-level triggers have access to the :NEW and :OLD correlation identifiers; statement-level triggers do not. |
:NEW.column | In a row-level trigger, references the new value of a column as it will be stored after the DML operation. Available in INSERT (contains the value being inserted) and UPDATE triggers (contains the post-update value). Is NULL in DELETE triggers. In BEFORE triggers, you can modify :NEW values to change what gets saved to the table. |
:OLD.column | In a row-level trigger, references the value of a column as it existed before the DML operation. Available in UPDATE triggers (contains the pre-update value) and DELETE triggers (contains the value being deleted). Is NULL in INSERT triggers (no pre-existing row). Used in audit triggers to log what changed and from what value. |
DROP TRIGGER name; | Permanently removes a trigger from the database. The table the trigger was attached to remains unaffected. Alternatively, use ALTER TRIGGER name DISABLE to temporarily deactivate without dropping (useful for mass data loads to skip trigger overhead). Re-enable with ALTER TRIGGER name ENABLE. |
Collections
| Type | Description |
|---|---|
TYPE type_name IS TABLE OF datatype; | Defines a nested table type — an unbounded, ordered collection of elements of the same type. Elements are indexed by positive integers. Can be sparse (after deletion) and can be stored in database columns (unlike associative arrays). Must be initialized with a constructor before use: type_name() or type_name(val1, val2). |
TYPE type_name IS VARRAY(n) OF datatype; | Defines a variable-size array (VARRAY) with a maximum number of elements n. Elements are indexed from 1 to current COUNT. Unlike nested tables, VARRAYs cannot have gaps (no sparse access). Can be stored in database columns. Size limit must be specified at type definition time and cannot be exceeded without redefining the type. |
collection.COUNT | Returns the current number of elements in the collection. For nested tables and VARRAYs, this is the number of defined elements. For sparse nested tables (after deletions), COUNT is less than LAST - FIRST + 1. Returns NULL for uninitialized collections. Used as a loop bound: FOR i IN 1..col.COUNT LOOP. |
collection.EXISTS(i) | Returns TRUE if element at index i exists (is defined), FALSE if it does not. Crucial for sparse nested tables where some indices may have been deleted. Accessing a non-existent index without EXISTS check raises SUBSCRIPT_DOES_NOT_EXIST (ORA-06533). Use in loops over potentially sparse collections. |
collection.FIRST, collection.LAST | FIRST returns the index of the first defined element; LAST returns the index of the last defined element. Both return NULL for empty collections. For dense collections, FIRST is typically 1. For sparse nested tables after deletions, FIRST and LAST may not be 1 and COUNT respectively. Use in loops to handle sparse data safely. |
collection.EXTEND | Adds elements to the end of a nested table or VARRAY to make room for new values. EXTEND appends one null element; EXTEND(n) appends n null elements; EXTEND(n, i) appends n copies of element i. Must be called before assigning to new positions. Cannot be used on associative arrays (they auto-extend). |
collection.DELETE(i) | Removes the element at index i from a nested table, making that position sparse. DELETE (no arguments) removes all elements. DELETE(m, n) removes a range. After deletion, the index i no longer EXISTS. VARRAYs do not support element-level DELETE — only full DELETE. Freed memory is reclaimed by Oracle's PGA. |
Bulk Operations
| Syntax | Description |
|---|---|
BULK COLLECT INTO collection | Fetches multiple rows from a query into a PL/SQL collection in a single round-trip to the SQL engine, dramatically reducing context-switch overhead compared to row-by-row fetching. Works with SELECT INTO, FETCH INTO, and RETURNING INTO. Without a LIMIT clause, fetches all rows at once — use LIMIT for large datasets to control memory consumption. |
FORALL i IN ... INSERT/UPDATE/DELETE | Sends a batch of DML statements to the SQL engine as a single context switch, rather than one-by-one. Provides up to 100x performance improvement over row-by-row DML loops. The loop variable i is used to index into bind array collections. Use SAVE EXCEPTIONS to continue processing after DML errors and collect them in SQL%BULK_EXCEPTIONS. |
LIMIT n | When used with BULK COLLECT, restricts the number of rows fetched per call to n rows. Allows processing large result sets in manageable chunks (pagination pattern) without loading the entire result set into memory. Typically used with a cursor FETCH in a loop: FETCH c BULK COLLECT INTO col LIMIT 1000. Essential for scalable batch processing. |
SQL%BULK_ROWCOUNT(i) | After a FORALL statement, returns the number of rows affected by the i-th DML iteration. Zero means the ith iteration affected no rows. Used when you need to know which iterations were effective vs. no-ops. Indexed parallel to the FORALL loop's collection. Combined with SQL%ROWCOUNT for the total rows affected across all iterations. |
Apache Ecosystem Quick Reference
Apache Overview
| Component | Description & Purpose |
|---|---|
Apache Hadoop | Distributed storage and processing framework for large datasets across clusters. Provides HDFS for storage and MapReduce for computation. |
Apache Spark | Fast, distributed computing engine for big data processing. Offers in-memory computation, SQL support, and machine learning libraries (MLlib). |
Apache Kafka | Event streaming platform for building real-time data pipelines. Provides high-throughput pub-sub messaging. |
Apache Hive | Data warehouse on top of Hadoop. Uses HiveQL for querying and analyzing HDFS data using SQL-like syntax. |
Apache Pig | High-level platform for analyzing large datasets using Pig Latin language. Compiles to MapReduce jobs. |
Apache Hadoop Architecture
| Component | Description |
|---|---|
HDFS (Hadoop Distributed File System) | Distributed file system with NameNode (master) managing metadata and DataNodes storing actual data. High replication factor for fault tolerance. |
MapReduce | Programming model and execution framework for distributed processing. Map phase transforms input data, Reduce phase aggregates results. |
YARN (Yet Another Resource Negotiator) | Resource management and job scheduling layer. Separates resource management from processing, enabling multiple processing engines. |
Replication | Default 3x replication ensures data availability. NameNode maintains file system namespace; DataNode performs block creation and replication. |
Rack Awareness | Replica placement policy considers rack topology. Replicas spread across racks, balancing reliability and write bandwidth. |
Apache Spark Core Concepts
| Concept | Details |
|---|---|
RDD (Resilient Distributed Dataset) | Immutable distributed collection of objects. Foundation of Spark; provides fault tolerance through lineage tracking. |
DataFrame | Distributed collection with named columns, similar to relational tables. Enables SQL queries and optimizations through Catalyst optimizer. |
Transformations | Operations creating new RDDs/DataFrames from existing ones (lazy evaluation). Examples: map, filter, join, groupBy, reduceByKey. |
Actions | Operations returning values to driver program or writing to storage. Trigger actual computation. Examples: collect, count, take, saveAsTextFile. |
Partitioning | Data distributed across partitions for parallel processing. More partitions = more parallelism but more overhead. |
Shuffle | Data redistribution across partitions (expensive). Occurs in aggregations, joins, and groupBy operations. |
Persistence/Caching | Cache RDDs/DataFrames in memory to reuse across operations. Significant performance improvement for iterative algorithms. |
Hadoop & Spark Integration
| Integration Point | How They Work Together |
|---|---|
Storage Layer | Spark reads from and writes to HDFS. HDFS provides distributed, fault-tolerant storage; Spark provides faster processing than MapReduce. |
Data Locality | Spark (on YARN) respects data locality like Hadoop. Schedules tasks on nodes storing data, reducing network I/O. |
Resource Management | Both can run on YARN for dynamic resource allocation. YARN arbitrates cluster resources between multiple applications. |
Hive Integration | Spark SQL can query Hive metastore and HDFS data. Spark serves as faster execution engine than traditional Hive on MapReduce. |
Replacement Pattern | Spark often replaces MapReduce for data processing while keeping HDFS. 100x faster for iterative algorithms due to in-memory computation. |
Ecosystem Synergy | Hadoop ecosystem tools (Hive, Pig) adapted to work with Spark. MLlib, Spark SQL, and Spark Streaming leverage Spark's distributed computation engine. |
Apache Spark SQL & DataFrames
| Feature | Description |
|---|---|
SparkSession | Entry point for Spark SQL functionality. Replaces SQLContext and HiveContext in modern Spark. |
DataFrame.createOrReplaceTempView() | Register DataFrame as temporary SQL table for current session only. |
DataFrameWriter.mode() | Write mode: overwrite, append, ignore, error (default). Controls behavior when target exists. |
Catalyst Optimizer | Spark SQL query optimizer. Applies rule-based and cost-based optimization before execution. |
Tungsten | Project optimizing Spark execution. Improves memory management and CPU efficiency for better performance. |
Microsoft Azure Quick Reference
Azure Platform Overview
| Concept | Description |
|---|---|
Azure | Microsoft's cloud computing platform offering 200+ services. Supports compute, storage, databases, networking, analytics, AI, and hybrid cloud scenarios. |
Subscription | Billing and access boundary for Azure resources. One account can have multiple subscriptions. Separate cost tracking and access control per subscription. |
Resource Group | Logical container for related resources. All resources must belong to a resource group. Simplifies management, billing, and access control across multiple resources. |
Management Group | Hierarchical structure above subscriptions. Apply policies, RBAC, and budgets to multiple subscriptions simultaneously. Enterprise governance and compliance. |
Region & Availability Zones | Resources deployed in geographic regions. Availability Zones provide redundancy within a region. Pair regions for disaster recovery and data residency. |
Compute Services
| Service | Details & Use Cases |
|---|---|
Virtual Machines (VMs) | Infrastructure-as-a-Service. Full OS control (Windows/Linux). Scale sets for auto-scaling. Hybrid benefit for existing Microsoft licenses. Pay per minute of usage. |
App Service | Platform-as-a-Service for web apps and APIs. Built-in auto-scaling, CI/CD from GitHub/Azure DevOps. Supports .NET, Java, Node.js, Python, PHP, Ruby. Integrated deployment slots for staging. |
Azure Functions | Serverless event-driven computing. Supports C#, JavaScript, Python, Java, PowerShell. Triggers: HTTP, timers, queues, blobs, Cosmos DB, Event Grid, Service Bus. Durable Functions for stateful workflows. |
Container Instances | Run containers without managing orchestration. Quick startup, seconds billing. Pay per second. Good for batch jobs and one-off tasks. |
Azure Kubernetes Service (AKS) | Managed Kubernetes. Auto-updates, auto-scaling nodes, integrated monitoring. Virtual nodes for burst capacity. Integrated with Azure Container Registry (ACR). |
Service Fabric | Distributed systems platform. Deploy microservices reliably. Handles scaling, upgrades, fault tolerance. Node.js, Java, C# support. On-premises or cloud. |
Storage Services
| Service | Characteristics & Best For |
|---|---|
Azure Blob Storage | Object storage for unstructured data (documents, media, backups). Hot/Cool/Cold/Archive tiers. Lifecycle management for auto-tiering. Redundancy: LRS, GRS, RA-GRS, GZRS. |
Azure File Shares | Managed file shares (SMB/NFS protocols). Mount from Windows/Linux/Mac. Snapshot and backup support. Hybrid connectivity via Azure File Sync. |
Azure Queue Storage | Simple message queuing. Asynchronous processing. Decouple components. Ordering guarantees, dead-letter queue support. |
Azure Table Storage | NoSQL key-value store. Semi-structured data. Massively scalable. Partition and row key design for efficient querying. |
Azure Data Lake Storage | Hierarchical file system for big data analytics. Optimized for Spark, Hive, Hadoop. Fine-grained POSIX permissions. Efficient for ETL workloads. |
Database Services
| Service | When to Use & Features |
|---|---|
Azure SQL Database | Managed SQL Server in Azure. Single database or elastic pools. Automatic backups, point-in-time restore. DTU (compute/storage bundle) or vCore (granular control) tiers. High availability built-in. |
Azure Database for PostgreSQL/MySQL | Managed open-source relational databases. Flexible Server for modern requirements. Read replicas for scaling reads. Automatic backups and failover. |
Azure Cosmos DB | Globally distributed, multi-model NoSQL. SQL, MongoDB, Cassandra, Gremlin APIs. Multi-master replication, automatic failover. SLA 99.999% availability. Low latency worldwide. |
Azure Cache for Redis | In-memory data cache. Reduce database load. Sub-millisecond response times. Persistence options available. Cluster mode for scaling. |
Azure Synapse Analytics | Data warehouse + big data analytics. Dedicated SQL pools (MPP), serverless SQL pool, Spark pools. Integrated workspace for data engineers and analysts. |
Data & Analytics Platform
| Component | Role in Data Ecosystem |
|---|---|
Azure Data Factory | Cloud ETL/ELT service. Visual pipeline design. 90+ connectors to data sources. Schedule, trigger, and orchestrate workflows. Monitoring and error handling built-in. |
Event Hubs | Event ingestion at scale. 1M+ events/second per partition. Capture events to storage for retention. Kafka-compatible protocol for existing Kafka apps. |
Stream Analytics | Real-time event processing. SQL-like query language. Built-in ML functions. Output to storage, databases, dashboards, or external systems in real-time. |
Azure Databricks | Unified analytics platform. Apache Spark engine. Collaborative notebooks for data scientists/engineers. MLflow for ML lifecycle management. Delta Lake for reliability. |
Power BI | Business analytics and visualization. Connects to 100+ data sources. Interactive dashboards and reports. Premium capacity for enterprise scale. |
Networking & Connectivity
| Component | Purpose & Features |
|---|---|
Virtual Network (VNet) | Isolated network in Azure. Subnets for segmentation. Service endpoints for PaaS access. Network Security Groups (NSGs) for stateful firewall rules per subnet/NIC. |
VPN Gateway | Secure site-to-site or point-to-site connectivity. Encrypt traffic over internet. Multi-site scenarios. High availability via active-active deployment. |
ExpressRoute | Dedicated private connectivity to Azure. Bypass internet. Consistent bandwidth and lower latency. Partner-provided or direct connections available. |
Azure Load Balancer | Layer 4 (TCP/UDP) load distribution. Ultra-high performance. Cross-region load balancing (preview). Health checks for fault tolerance. |
Application Gateway | Layer 7 (HTTP) load balancing and WAF. SSL/TLS termination. Host-based routing. Cookie-based session affinity. |
Azure CDN | Content delivery network. Edge locations worldwide. Caching strategies for performance. DDoS Protection included. |
Security & Identity
| Service | Details & Implementation |
|---|---|
Azure Active Directory (Entra ID) | Identity and access management. RBAC for Azure resources. Conditional Access for risk-based auth. MFA, passwordless sign-in (Windows Hello, FIDO2). |
Azure Key Vault | Securely store secrets, keys, certificates. Hardware-backed HSM option. Audit logging. Integrate with services for transparent encryption. Soft-delete and purge protection. |
Azure Security Center | Cloud security posture management. Vulnerability scanning. Security recommendations. Threat protection across resources. Compliance monitoring. |
Azure Firewall | Managed firewall service. Application and network-level filtering. Threat intelligence. Centralized logging and monitoring. |
Web Application Firewall (WAF) | Protects web applications from common attacks (SQL injection, XSS, DDoS). Integrates with Application Gateway and CDN. Managed rule sets. |
Azure Defender | Threat protection across workloads. VM, container, database, and app protection. Integration with Security Center for unified dashboard. |
AI/ML & Cognitive Services
| Service | Capabilities & Use Cases |
|---|---|
Azure Machine Learning | End-to-end ML platform. Jupyter notebooks, AutoML, designer (no-code). Model training/deployment/monitoring. Batch and real-time scoring. |
Cognitive Services | Pre-built AI APIs requiring no ML expertise. Vision (OCR, object detection, face recognition), Language (NLP, sentiment, entity recognition), Speech (synthesis, recognition). |
Azure Bot Service | Build conversational AI. Integrates with Cognitive Services. Multi-channel deployment (Teams, Facebook, Slack). QnA Maker for FAQ bots. |
Form Recognizer | Extract text, tables, key-value pairs from documents. Custom model training. Prebuilt models for invoices, receipts, IDs. |
Azure Synapse Analytics + Spark | Run Spark notebooks for data science. MLlib for machine learning. Integrate with ML lifecycle tools. |
Integration & Messaging
| Service | Purpose & Scenarios |
|---|---|
Azure Service Bus | Reliable messaging between decoupled components. Queues and topics/subscriptions. Transactions, dead-lettering, deduplication. Enterprise-grade reliability. |
Logic Apps | Workflow orchestration with visual designer. 500+ pre-built connectors. Integration with Microsoft 365, Dynamics, Salesforce, etc. Serverless execution model. |
Event Grid | Event-driven architecture. Route events from any source to handlers. Decouples publishers from subscribers. Guaranteed delivery, dead-lettering. |
API Management | Publish, manage, secure APIs. Developer portal, versioning, rate limiting. Policy-based request/response transformation. Analytics and monitoring. |
Azure Integration Services (Enterprise) | B2B messaging (EDI, AS2, X12). SWIFT, HL7 protocols. Partner management. Map data between formats. |
DevOps & Deployment
| Service | Details & Workflow |
|---|---|
Azure DevOps | Integrated suite: Repos (Git), Pipelines (CI/CD), Boards (Agile), Test Plans, Artifacts (package management). |
Azure Pipelines | Build and deploy to any platform. YAML-defined pipelines. Multi-stage pipelines (build, test, deploy). Integration with GitHub Actions. |
Infrastructure as Code (IaC) | Azure Resource Manager (ARM) templates, Bicep (simplified syntax), Terraform. Version control deployments. Repeatable infrastructure. |
Azure Container Registry (ACR) | Managed private Docker registry. Geo-replication for disaster recovery. Vulnerability scanning. Image webhooks for CI/CD triggers. |
App Insights | Application monitoring. Performance metrics, logs, traces. Dependency mapping. Alerts based on metrics/patterns. |
Azure Architecture Patterns
| Pattern | Architecture & Components |
|---|---|
Hybrid Cloud | On-premises + Azure. Azure Stack for consistent experience. ExpressRoute for connectivity. Data sync via Azure File Sync. Single identity via Azure AD. |
Web Application | App Service (frontend) → SQL Database (transactional) + Blob Storage (media). CDN for static content. Application Gateway for load balancing. App Insights for monitoring. |
Serverless Application | API Management → Azure Functions → Cosmos DB. Event Grid triggers. Logic Apps for workflows. Azure Durable Functions for stateful logic. |
Data Lake & Analytics | Blob/Data Lake Storage → Data Factory (ETL) → Synapse Analytics → Power BI. Event Hubs for real-time. Stream Analytics for live processing. |
Microservices on AKS | Azure Container Registry → AKS deployment. Service Mesh (Istio/Linkerd). Service Bus for async communication. Azure Cosmos DB for data. |
Disaster Recovery | SQL Database geo-replication. Blob Storage geo-redundant replication. Azure Site Recovery for VM failover. Traffic Manager for failover routing. |
Cost Optimization & Governance
| Feature | Implementation & Benefit |
|---|---|
Azure Reservations | Prepay for 1 or 3 years. Save up to 72% vs pay-as-you-go. Reserved capacity for VMs, databases, software. Flexible purchase at subscription or management group level. |
Spot VMs | Use surplus compute capacity. Up to 90% discount vs regular VMs. Can be evicted. Good for non-critical workloads, batch jobs, testing. |
Azure Hybrid Benefit | Use existing Microsoft licenses (Windows Server, SQL Server, Enterprise agreements). Reduce cloud compute costs significantly. |
Cost Management + Billing | Monitor spending across subscriptions. Budget alerts. Cost analysis by resource/tags. Recommendations for optimization. |
Azure Policy | Enforce compliance standards. Restrict resource types, locations, sizes. Automatic remediation. Governance at scale across subscriptions. |
Management Groups | Organize subscriptions hierarchically. Apply policies/RBAC/budgets across multiple subscriptions simultaneously. Enterprise governance. |
Key Azure Advantages
| Strength | Details |
|---|---|
Enterprise Integration | Seamless integration with Microsoft 365, Dynamics 365, Power Platform. Active Directory for identity. Familiar tools for enterprises on Microsoft stack. |
Hybrid Cloud Leader | Azure Stack allows same Azure experience on-premises. ExpressRoute for hybrid connectivity. Consistent tooling and services. |
Compliance & Data Residency | 50+ compliance certifications. Sovereign clouds for specific regions (China, Government). Transparent privacy compliance. |
Global Scale | 60+ regions worldwide. Availability Zones for HA within region. Redundancy options (LRS, GRS, GZRS). Pair regions for DR. |
AI/ML Investment | Cognitive Services pre-built AI. Azure Machine Learning for custom models. Integration across platform. OpenAI partnership for latest LLMs. |
Google Cloud Platform (GCP) Quick Reference
GCP Overview & Core Services
| Service Category | Description & Key Services |
|---|---|
Compute | Compute Engine (IaaS VMs), App Engine (PaaS), Cloud Run (Serverless containers), Google Kubernetes Engine (GKE - container orchestration). |
Storage | Cloud Storage (object storage, like S3), Cloud SQL (managed relational databases), Cloud Firestore (NoSQL document database), Cloud Bigtable (wide-column database). |
Networking | Virtual Private Cloud (VPC), Cloud Load Balancing, Cloud CDN, Cloud Interconnect, Cloud VPN. Global network infrastructure. |
Analytics & Big Data | BigQuery (data warehouse), Cloud Dataflow (stream/batch processing), Cloud Pub/Sub (event streaming), Cloud Dataproc (managed Hadoop/Spark). |
AI/ML | Vertex AI (unified ML platform), AutoML, Dialogflow (conversational AI), Vision API, Natural Language API, Translation API. |
Identity & Security | Cloud Identity, Cloud IAM, Cloud KMS, Cloud Armor, Security Command Center. Centralized access and threat management. |
Compute Services Deep Dive
| Service | Details & Use Cases |
|---|---|
Compute Engine | Infrastructure-as-a-Service. Full control over VMs (custom OS, configurations). Auto-scaling, load balancing, persistent disks. Best for traditional applications. |
App Engine | Platform-as-a-Service. Deploy applications, auto-scaling handled. Standard (pre-configured runtimes) or Flexible (custom containers). Pay for uptime. |
Cloud Run | Serverless container platform. Deploy containerized applications, auto-scale to zero. Pay only for execution time. Event-driven workloads ideal. |
GKE (Kubernetes Engine) | Managed Kubernetes service. Deploy, manage, scale containerized applications at scale. Autopilot for simplified management. |
Storage & Database Services
| Service | Characteristics & Best For |
|---|---|
Cloud Storage | Scalable object storage for unstructured data. Storage classes: Standard (frequent access), Nearline, Coldline, Archive. Global edge caching with CDN integration. |
Cloud SQL | Managed relational database (PostgreSQL, MySQL, SQL Server). Automatic backups, replication, failover. High availability configuration available. |
Cloud Firestore | NoSQL document database. Real-time synchronization, offline support. Automatic scaling. Better than Realtime Database for new projects. |
Cloud Bigtable | Wide-column NoSQL database for analytics heavy lifting. Handles petabyte-scale, ultra-high throughput. Not ideal for OLTP transactions. |
BigQuery | Fully managed data warehouse. SQL queries on massive datasets. Serverless, no infrastructure to manage. Pay-per-query model. Blazingly fast analytics. |
Data & Analytics Pipeline
| Component | Role in Data Pipeline |
|---|---|
Cloud Pub/Sub | Event ingestion. Messages published to topics, subscribers receive in real-time or batch. Decouples producers/consumers. At-least-once delivery guarantee. |
Cloud Dataflow | Stream and batch processing (Apache Beam). Auto-scaling, windowing, side inputs. Built-in support for stateful transformations. |
Cloud Dataproc | Managed Hadoop and Spark clusters. Quick cluster setup, familiar tools. Integrates with BigQuery and Cloud Storage easily. |
BigQuery Integration | Central data warehouse receiving processed data. SQL queries for analysis. Exports to Storage for archival. Real-time streaming inserts supported. |
Networking & Security
| Component | Purpose & Features |
|---|---|
Virtual Private Cloud (VPC) | Isolated network within GCP. Custom IP ranges, subnets, routes. Firewall rules control traffic. Enables multi-tier application architecture. |
Cloud Load Balancing | Global load balancing across regions. HTTP(S), SSL, TCP, UDP support. Auto-scaling triggers based on load. |
Cloud Armor | DDoS and Layer 7 attack protection. Create security policies, block malicious traffic. Works with Load Balancing. |
Cloud KMS | Key management service. Encrypt data at rest and in transit. Audit logging of all key usage. Compliance certifications (HIPAA, PCI-DSS, etc.). |
Cloud IAM | Identity and access management. Granular role-based access control (RBAC). Service accounts for application authentication. |
Amazon Web Services (AWS) Quick Reference
AWS Core Services Overview
| Category | Key Services & Purpose |
|---|---|
Compute | EC2 (virtual machines), Lambda (serverless functions), Elastic Beanstalk (PaaS), ECS/EKS (container services), Auto Scaling Groups. |
Storage | S3 (object storage), EBS (block storage for EC2), EFS (elastic file system), Glacier (long-term archival), Storage Gateway (hybrid). |
Database | RDS (managed relational - MySQL, PostgreSQL, Oracle), DynamoDB (NoSQL), Redshift (data warehouse), Aurora (cloud-native relational). |
Networking | VPC (isolated networks), CloudFront (CDN), Route 53 (DNS), ELB/ALB/NLB (load balancing), VPN, Direct Connect. |
Analytics & Big Data | Kinesis (real-time data streams), Athena (SQL queries on S3), EMR (Hadoop/Spark), Glue (ETL), QuickSight (BI tool). |
Security & Compliance | IAM (access control), KMS (key management), Secrets Manager, AWS Config, CloudTrail (audit logs), GuardDuty (threat detection). |
EC2 (Elastic Compute Cloud) Details
| Concept | Description |
|---|---|
Instance Types | Optimized for different use cases: General (t3, m5), Compute (c5), Memory (r5), Storage (i3), GPU (p3). Find right balance of CPU, memory, networking. |
AMI (Amazon Machine Image) | Pre-configured OS and software template. Launch instances from AMI. Create custom AMIs from running instances for replication. |
Pricing Models | On-Demand (hourly, flexible), Reserved (1-3 year commitment, 40-70% discount), Spot (up to 90% discount, can be interrupted), Savings Plans. |
Security Groups & NACLs | Security Groups (stateful firewall at instance level), Network ACLs (stateless at subnet level). Control inbound/outbound traffic rules. |
EBS Volumes | Persistent block storage. Attach to EC2 instances. Create snapshots for backup/replication. Different volume types (gp3, io1, st1, sc1). |
Elastic IP | Static public IP address. Associate/disassociate with EC2 instances. Remains same across stop/start cycles. |
S3 (Simple Storage Service) Overview
| Feature | Details |
|---|---|
Buckets | Top-level containers for objects. Bucket names globally unique. Enable versioning, logging, encryption per bucket. |
Objects | Files stored in buckets. Each object has key (path), value (data), metadata, version ID. No directory structure (flat). |
Storage Classes | Standard (frequent access), Intelligent-Tiering (automatic optimization), Standard-IA (infrequent access, cheaper), Glacier (archival, retrieval delay), Deep Archive. |
Lifecycle Policies | Automatically transition/delete objects. Move to cheaper storage after X days. Expire logs/temp files automatically. |
Replication | Cross-Region Replication (CRR) or Same-Region (SRR). Real-time sync to another location for disaster recovery. |
Access Control | Bucket policies, ACLs, presigned URLs, Block Public Access. Fine-grained permission management. |
CloudFront Integration | S3 as CloudFront origin. Edge locations cache content globally for faster delivery. Reduced S3 costs via caching. |
Lambda (Serverless Computing)
| Aspect | Details |
|---|---|
Function Basics | Write code in supported languages (Python, Node.js, Java, Go, C#, Ruby). No infrastructure to manage. Auto-scaling, high availability built-in. |
Triggers | Invoke via API Gateway (HTTP), S3 events, DynamoDB streams, SNS, SQS, Kinesis, EventBridge (scheduled or event-driven). |
Execution Environment | Runtime container managed by AWS. 128MB - 10GB memory allocation. CPU scales with memory. Timeout: max 15 minutes. |
Pricing | Pay per request + duration (millisecond granularity). Free tier: 1M requests/month. Extremely cost-effective for variable workloads. |
Layers | Reusable code/libraries. Create layers for dependencies. Version management and compatibility checks available. |
Environment Variables & Secrets Manager | Pass config via environment variables. Secrets Manager encrypts sensitive data (DB passwords, API keys). |
RDS (Relational Database Service) Overview
| Feature | Description |
|---|---|
Supported Engines | MySQL, PostgreSQL, Oracle, SQL Server, MariaDB, Amazon Aurora (cloud-native). Choose based on compatibility and workload requirements. |
Automated Backups | Daily snapshots, transaction logs for point-in-time recovery. Retain for configurable period (7-35 days typical). |
Multi-AZ Deployments | Synchronous replication to standby in different AZ. Automatic failover on primary failure. High availability (99.95% uptime SLA). |
Read Replicas | Asynchronous replication within or across regions. Offload read traffic, reduce primary load. Promote to standalone if needed. |
Performance Insights | Monitor database load, identify bottlenecks. View wait events, top SQL queries. Optimize performance proactively. |
Encryption | At rest using AWS KMS. In transit using SSL/TLS. Transparent to applications, minimal performance impact. |
Aurora | Drop-in replacement for MySQL/PostgreSQL. 5x faster reads, 3x faster writes. Distributed, fault-tolerant, auto-scaling Aurora Serverless available. |
AWS Service Integration Patterns
| Pattern | How Services Work Together |
|---|---|
Web Application | Route 53 (DNS) → CloudFront (CDN) → ALB (load balance) → EC2/Lambda (compute) → RDS (database) + S3 (assets). Auto Scaling Groups adapt to traffic. |
Real-time Data Pipeline | Kinesis (ingest streams) → Lambda (transform) → S3 (store) → Athena/Redshift (analytics). EventBridge triggers Lambda on events. |
Serverless API | API Gateway (REST/WebSocket) → Lambda (logic) → DynamoDB (NoSQL) + SQS (async tasks). CloudWatch monitors, X-Ray traces requests. |
Data Lake | S3 data lake → Glue (ETL/catalog) → Athena (SQL queries) → QuickSight (visualization). Lake Formation manages permissions and governance. |
Disaster Recovery | RDS Multi-AZ + read replicas. S3 cross-region replication. Route 53 health checks failover. CloudFormation IaC for quick redeployment. |
CI/CD Pipeline | CodeCommit (version control) → CodeBuild (test/build) → CodeDeploy (deploy to EC2/Lambda). CloudWatch alarms trigger rollbacks on errors. |
AWS Security Best Practices
| Practice | Implementation & Benefit |
|---|---|
IAM Principle of Least Privilege | Users/roles have minimal permissions needed. Use managed policies as starting point, refine further. Regular audits detect over-permissions. |
MFA & Strong Authentication | Multi-factor authentication for all users. AWS SSO for centralized management. Reduces credential theft impact. |
Encryption Everywhere | At rest (RDS, EBS, S3 KMS), in transit (SSL/TLS). Manage keys in KMS. Transparent to applications, minimal overhead. |
VPC Isolation & NACLs | Place resources in private subnets by default. NAT gateways for outbound internet. Security groups whitelist necessary ports only. |
CloudTrail & CloudWatch Logs | Audit all API activity. Monitor logs for suspicious patterns. Set up alarms for critical events (root access, policy changes). |
Patch Management | SSM Patch Manager automates patching. EC2 Instance Metadata Service v2 prevents SSRF attacks. Keep AMIs current. |