Mastering SQL
1. Relational Databases
A relational database organizes data into tables composed of rows and columns. Each table represents a
specific entity (e.g., Customers, Orders), and relationships are maintained through keys. The structure
ensures scalability, clarity, and consistency across applications.
These databases form the backbone of countless industries, from banking systems to social networks. Their
tabular format and relational capabilities allow for powerful and flexible data handling.
Importance:
• Tabular, organized data storage
• Efficient relationships between entities
• Reduces data redundancy
• Scales across enterprise systems
• Enhances data accuracy and access
2. Keys and Unique Values
Primary keys uniquely identify each row in a table. Foreign keys link one table to another, forming
relationships between datasets. Unique constraints ensure that certain values, like email addresses or
usernames, are never duplicated.
SQL Example:
CREATE TABLE Customers (
Customer_ID NUMBER PRIMARY KEY,
Email VARCHAR2(100) UNIQUE,
Name VARCHAR2(100)
);
Importance:
• Guarantees uniqueness
• Establishes relationships between tables
• Enforces data integrity
• Optimizes indexing
• Supports data normalization
pg. 1 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
3. Planning a Database
Designing a database before coding is vital. Identify entities, define relationships, and decide on
normalization levels. Planning involves deciding what data to store, how it will be accessed, and how tables
relate to one another.
Proper planning avoids redesign and facilitates scalability. It allows efficient querying, prevents redundancy,
and ensures consistency across all operations.
Importance:
• Helps avoid future restructuring
• Enhances database efficiency
• Improves user and developer experience
• Enables future growth and changes
• Supports long-term maintainability
4. Creating Tables
Tables are the foundation of a database. Defining them with appropriate data types and constraints ensures
that only valid data is stored. Fields such as VARCHAR2, NUMBER, and DATE are used depending on the type of
data.
SQL Example:
CREATE TABLE Products (
Product_ID NUMBER PRIMARY KEY,
Name VARCHAR2(100) NOT NULL,
Price NUMBER(10,2)
);
Importance:
• Ensures data structure integrity
• Enables constraints and validation
• Supports efficient queries
• Promotes organized data entry
• Helps enforce relationships
5. Defining Relationships
Relationships are established through keys. A foreign key in one table references a primary key in another,
enabling logical linking of data across multiple tables.
SQL Example:
CREATE TABLE Orders (
Order_ID NUMBER PRIMARY KEY,
pg. 2 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
Customer_ID NUMBER,
FOREIGN KEY (Customer_ID) REFERENCES Customers(Customer_ID)
);
Importance:
• Enforces referential integrity
• Simplifies complex queries
• Powers relational models
• Ensures data consistency
• Enables JOIN operations
6. Normalization
Normalization breaks large tables into smaller ones to eliminate redundancy. It follows steps (1NF, 2NF,
3NF, etc.) to ensure atomic data, eliminate repeating groups, and remove functional dependencies.
Normalized databases reduce anomalies and make updates more manageable and predictable.
Importance:
• Reduces duplicate data
• Enhances data consistency
• Optimizes updates and inserts
• Makes schema easier to manage
• Improves relational integrity
7. Denormalization
Denormalization is used to improve performance by combining tables for faster read access. It sacrifices
some consistency and storage efficiency to reduce the need for JOINs in complex queries.
Used in reporting systems or analytics dashboards where speed is a priority.
Importance:
• Boosts query performance
• Reduces JOIN complexity
• Useful for data warehouses
• Optimized for reading, not writing
• Improves dashboard responsiveness
8. Writing Queries
pg. 3 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
SQL queries retrieve, filter, and organize data. The SELECT statement, along with WHERE, ORDER BY, and
other clauses, allows you to pinpoint the exact data you need.
SQL Example:
SELECT Name FROM Customers WHERE City = 'Mumbai';
Importance:
• Enables custom data retrieval
• Core of all database interaction
• Supports business analysis
• Allows data filtering and sorting
• Easily integrates into applications
9. Sorting Results
Sorting results using ORDER BY allows presentation of data in logical formats. You can sort by one or more
columns in ascending (ASC) or descending (DESC) order.
SQL Example:
SELECT * FROM Orders ORDER BY Order_Date DESC;
Importance:
• Enhances data readability
• Supports top-N analytics
• Useful in financial or sales reporting
• Improves data navigation
• Critical in user-facing apps
10. JOINs Part 1: INNER JOIN
INNER JOIN returns only the rows with matching values in both tables. It’s the most common JOIN and is
widely used in applications like order tracking, reporting, and CRM systems.
SQL Example:
SELECT o.Order_ID, c.Name
FROM Orders o
INNER JOIN Customers c ON o.Customer_ID = c.Customer_ID;
This returns only those orders where the customer exists in the Customers table.
Importance:
• Combines data with common keys
pg. 4 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
• Most commonly used JOIN
• Perfect for transactional systems
• Prevents null or missing matches
• Efficient for structured data
11. JOINs Part 2: LEFT JOIN and RIGHT JOIN
LEFT JOIN returns all rows from the left table and matched rows from the right. If no match, NULLs are
returned. RIGHT JOIN does the reverse—returns all from the right and matched from the left.
SQL Example:
SELECT c.Name, o.Order_ID
FROM Customers c
LEFT JOIN Orders o ON c.Customer_ID = o.Customer_ID;
This shows all customers—even those who haven’t placed orders. Right JOIN is rarely used but works in
reverse.
Importance:
• Includes unmatched data
• Useful for audit reports
• Helps identify missing relationships
• Supports full context analysis
• Ideal for inclusive reports
12. JOINs Part 3: FULL OUTER JOIN and CROSS JOIN
FULL OUTER JOIN combines results of LEFT and RIGHT JOIN—returning all rows from both tables,
with NULLs where there's no match. CROSS JOIN returns the Cartesian product (every combination of
rows from both tables).
SQL Example – FULL OUTER JOIN:
SELECT c.Name, o.Order_ID
FROM Customers c
FULL OUTER JOIN Orders o ON c.Customer_ID = o.Customer_ID;
SQL Example – CROSS JOIN:
SELECT p.Name, s.Region
FROM Products p
CROSS JOIN Stores s;
Use FULL OUTER JOIN for reconciliation and data completeness. Use CROSS JOIN carefully—output
size can grow very large.
Importance:
pg. 5 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
• FULL JOIN helps in full comparisons
• CROSS JOIN is useful for testing combinations
• Handles unmatched data in both tables
• Essential for deep auditing or reporting
• Requires careful use due to data volume
13. Modifying Data
Modifying data uses INSERT, UPDATE, and DELETE commands. These allow you to change data
dynamically based on user actions or system requirements.
SQL Example:
UPDATE Products SET Price = 99.99 WHERE Product_ID = 104;
Importance:
• Keeps data accurate and up-to-date
• Enables transactional applications
• Supports automated processes
• Provides flexibility for business logic
• Can be used with triggers
14. Aggregate Functions
Functions like SUM(), COUNT(), AVG() are used to summarize data. Combined with GROUP BY, they provide
powerful analytics tools.
SQL Example:
SELECT Customer_ID, COUNT(*) FROM Orders GROUP BY Customer_ID;
Importance:
• Summarizes large data sets
• Helps create reports
• Useful in performance tracking
• Supports financial analysis
• Reduces need for external processing
15. Querying the Oracle Data Dictionary
Oracle provides system tables (USER_TABLES, ALL_TAB_COLUMNS, etc.) for querying schema-level metadata.
These are useful for understanding database structure dynamically.
pg. 6 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
SQL Example:
SELECT * FROM USER_TABLES;
Importance:
• Helps developers understand schema
• Supports documentation efforts
• Allows introspection and automation
• Used in schema comparison tools
• Useful for dynamic query generation
pg. 7 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited