SQL Notes
SQL Notes
Notes
Table of Contents
1. Introduction to SQL
3. Data Types
5. Querying Data
7. Joins
9. Subqueries
10. Views
11. Indexes
12. Transactions
14. Triggers
It's declarative, meaning you specify what you want, not how to get it.
SQL is divided into several sub-languages: DDL, DML, DCL, and TCL.
Example:
Example:
Example:
Key clauses:
Example:
Example:
INNER JOIN: Returns records that have matching values in both tables
LEFT JOIN: Returns all records from the left table, and the matched records
from the right table
RIGHT JOIN: Returns all records from the right table, and the matched
records from the left table
FULL OUTER JOIN: Returns all records when there is a match in either left
or right table
Example:
The GROUP BY clause is used with aggregate functions to group the result-set
by one or more columns.
Example:
Example:
Views can simplify complex queries and provide an additional layer of security.
Types of indexes:
Single-column indexes
Multi-column indexes
Full-text indexes
Example:
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
DELIMITER //
CREATE PROCEDURE get_employee_by_id(IN emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = emp_id;
END //
DELIMITER ;
CALL get_employee_by_id(123);
Proper indexing
Query optimization
Example:
Full-text search
Recursive queries
SELECT
name,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY sal
ary DESC) as salary_rank
FROM employees;
This comprehensive guide covers the main aspects of SQL from basic to
advanced concepts. Remember that practice and hands-on experience are
crucial for mastering SQL. Good luck with your SQL course!