SQL - Comprehensive Notes
SQL (Structured Query Language) - Comprehensive Notes
1. Introduction to SQL
- SQL is a standard language used for storing, manipulating, and retrieving data in databases.
- Works with RDBMS (Relational Database Management Systems).
2. Features of SQL
- Simple and easy to learn.
- Supports Data Definition, Manipulation, and Control.
- Provides high-performance query processing.
3. Types of SQL Commands
- Data Definition Language (DDL): Defines structure of the database.
- CREATE: Creates database objects like tables.
- ALTER: Modifies existing database objects.
- DROP: Deletes database objects.
- TRUNCATE: Removes all rows from a table.
- Data Manipulation Language (DML): Manipulates data in tables.
- SELECT: Retrieves data.
- INSERT: Adds new rows.
- UPDATE: Modifies existing rows.
- DELETE: Removes rows.
- Data Control Language (DCL): Controls user access.
- GRANT: Provides privileges to users.
- REVOKE: Removes privileges.
- Transaction Control Language (TCL): Manages transactions.
- COMMIT: Saves changes permanently.
- ROLLBACK: Reverts changes.
- SAVEPOINT: Sets points for partial rollbacks.
4. SQL Constraints
- PRIMARY KEY: Ensures unique values for a column.
- FOREIGN KEY: Links two tables.
- UNIQUE: Ensures all values in a column are different.
- NOT NULL: Ensures a column cannot have NULL values.
- CHECK: Ensures values meet specific criteria.
- DEFAULT: Sets default values for a column.
5. SQL Joins
- Combines data from two or more tables.
- INNER JOIN: Matches rows from both tables.
- LEFT JOIN: Retrieves all rows from the left table and matching rows from the right.
- RIGHT JOIN: Retrieves all rows from the right table and matching rows from the left.
- FULL OUTER JOIN: Retrieves all rows from both tables.
6. Aggregate Functions
- Performs calculations on a set of values.
- COUNT(): Counts rows.
- SUM(): Calculates the sum of values.
- AVG(): Calculates average value.
- MAX(): Retrieves the maximum value.
- MIN(): Retrieves the minimum value.
7. SQL Clauses
- WHERE: Filters records.
- GROUP BY: Groups data for aggregation.
- HAVING: Filters aggregated data.
- ORDER BY: Sorts data in ascending or descending order.
- LIMIT: Restricts the number of rows returned.
8. Subqueries
- A query inside another query.
- Types: Single-row, Multi-row, and Correlated Subqueries.
9. SQL Indexing
- Improves performance by reducing query execution time.
- Types: Clustered, Non-clustered, Unique, Composite.
10. Transactions in SQL
- Properties (ACID):
- Atomicity: Ensures all operations in a transaction succeed or none do.
- Consistency: Maintains database integrity.
- Isolation: Prevents transactions from interfering.
- Durability: Ensures committed changes are permanent.
11. Views in SQL
- Virtual tables created by a query.
- Advantages: Simplifies complex queries, improves security.
12. Stored Procedures and Functions
- Stored Procedure: A reusable SQL code block.
- Function: Returns a single value, used in SQL statements.
13. SQL and NoSQL
- SQL: Structured query language for relational databases.
- NoSQL: Non-relational databases for unstructured data.
14. Advanced SQL Topics
- Triggers: Executes automatically in response to events.
- Cursors: Allows row-by-row processing of query results.
- Window Functions: Operates over a set of rows related to the current row.
15. Popular SQL Databases
- MySQL, PostgreSQL, Microsoft SQL Server, Oracle, SQLite.
Conclusion
SQL is a powerful language for managing relational databases. Its simplicity and versatility make it
an essential skill for database professionals.