SQL Comprehensive Guide
Introduction to SQL
SQL (Structured Query Language) is a standard language for accessing and manipulating databases. SQL is
used to perform tasks such as update data on a database, or retrieve data from a database.
SQL Data Types
Each column in a database table is required to have a name and a data type. Common SQL data types
include INT, VARCHAR, DATE, FLOAT, BOOLEAN.
DDL - Data Definition Language
DDL includes commands such as CREATE, ALTER, DROP, TRUNCATE which are used to define and
modify database schema.
DML - Data Manipulation Language
DML includes commands such as SELECT, INSERT, UPDATE, DELETE. These are used to manipulate data
in the database.
DCL - Data Control Language
DCL includes GRANT and REVOKE, which deal with the rights, permissions, and other controls of the
database system.
TCL - Transaction Control Language
TCL commands such as COMMIT, ROLLBACK, and SAVEPOINT manage changes made by DML
statements.
SELECT Statement
The SELECT statement is used to select data from a database. The data returned is stored in a result table.
WHERE Clause
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified
condition.
Operators in SQL
Page 1
SQL Comprehensive Guide
SQL includes arithmetic, comparison, logical, and special operators such as BETWEEN, IN, LIKE, IS NULL.
Joins in SQL
Joins are used to combine rows from two or more tables, based on a related column. Types: INNER JOIN,
LEFT JOIN, RIGHT JOIN, FULL JOIN.
GROUP BY and HAVING
GROUP BY is used to arrange identical data into groups. HAVING is used to filter groups based on a
condition.
ORDER BY
ORDER BY is used to sort the result-set in ascending or descending order.
Subqueries
A subquery is a query within another query. It is used to perform operations in multiple steps.
Views
A view is a virtual table based on the result-set of an SQL statement.
Indexes
Indexes are used to retrieve data from the database more quickly than otherwise. CREATE INDEX is the
command used.
Constraints
Constraints are used to specify rules for data in a table. Common constraints: NOT NULL, UNIQUE,
PRIMARY KEY, FOREIGN KEY, CHECK.
Normalization
Normalization is the process of organizing data to reduce redundancy and improve data integrity. Normal
forms: 1NF, 2NF, 3NF, BCNF.
Stored Procedures
Stored procedures are prepared SQL code that you can save and reuse. They can take parameters and
Page 2
SQL Comprehensive Guide
include logic.
Functions
SQL functions are similar to procedures, but they return a single value and can be used in expressions.
Triggers
Triggers are SQL statements that are executed automatically in response to certain events on a particular
table or view.
Transactions
A transaction is a unit of work that is performed against a database. Transactions ensure data integrity.
Error Handling
SQL provides mechanisms for error handling in procedures and scripts using TRY...CATCH blocks (in
T-SQL).
Case Statement
CASE is used to create conditional logic in SQL queries.
SQL Injection
SQL Injection is a code injection technique that might destroy your database. Always sanitize input data.
Performance Tuning
Performance tuning involves optimizing SQL queries, indexing, and database structure for speed and
efficiency.
Common Functions
Includes aggregate functions (SUM, COUNT), string functions (CONCAT, LENGTH), date functions (NOW,
CURDATE).
Advanced Joins
CROSS JOIN, SELF JOIN, and NATURAL JOIN are advanced join types used for specific cases.
Page 3
SQL Comprehensive Guide
Window Functions
Window functions perform calculations across a set of table rows that are somehow related to the current
row.
Pivot and Unpivot
PIVOT rotates table data from rows to columns. UNPIVOT does the reverse.
JSON and XML in SQL
Modern SQL supports querying and manipulating JSON and XML data types.
Best Practices
Use meaningful aliases, write readable queries, avoid SELECT *, use parameterized queries, and test with
real data.
Page 4