Here’s a summary of SQL (Structured Query Language) Notes, covering essential concepts,
commands, and best practices:
1. Overview of SQL
SQL is a standard programming language used for managing and manipulating relational
databases.
Types of SQL Commands:
o DDL (Data Definition Language): Defines and manages database schema (e.g.,
CREATE, ALTER, DROP).
o DML (Data Manipulation Language): Manages data within schema objects (e.g.,
SELECT, INSERT, UPDATE, DELETE).
o DCL (Data Control Language): Controls access to data (e.g., GRANT, REVOKE).
o TCL (Transaction Control Language): Manages database transactions (e.g., COMMIT,
ROLLBACK, SAVEPOINT).
2. Basic SQL Commands
SELECT: Used to query data from a database.
SELECT column1, column2 FROM table_name;
INSERT INTO: Adds new rows into a table.
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
UPDATE: Modifies existing data in a table.
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
DELETE: Removes rows from a table.
DELETE FROM table_name
WHERE condition;
CREATE TABLE: Defines a new table along with its columns and data types.
CREATE TABLE table_name (
column1 datatype,
column2 datatype
);
ALTER TABLE: Modifies an existing table (e.g., adding a column).
ALTER TABLE table_name
ADD column_name datatype;
DROP TABLE: Deletes a table from the database.
DROP TABLE table_name;
3. Clauses and Keywords
WHERE: Filters records based on a specified condition.
SELECT * FROM table_name
WHERE condition;
ORDER BY: Sorts results based on one or more columns (ascending or descending).
SELECT * FROM table_name
ORDER BY column1 ASC;
GROUP BY: Groups rows sharing a property and is often used with aggregate functions.
SELECT column, COUNT(*)
FROM table_name
GROUP BY column;
HAVING: Filters the results of a GROUP BY query (like WHERE but for groups).
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > 10;
LIMIT: Restricts the number of records returned (or TOP in SQL Server).
SELECT * FROM table_name
LIMIT 10;
4. Joins
INNER JOIN: Returns only matching rows between two tables.
SELECT column1, column2
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and matched rows from
the right table. If there’s no match, NULL values are returned for columns from the right
table.
SELECT column1, column2
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
RIGHT JOIN (or RIGHT OUTER JOIN): Similar to the LEFT JOIN, but returns all rows from the
right table.
SELECT column1, column2
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
FULL JOIN (or FULL OUTER JOIN): Returns all rows from both tables, with NULLs for
unmatched rows from either side.
SELECT column1, column2
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
5. Aggregate Functions
COUNT(): Counts the number of rows in a table.
SELECT COUNT(*) FROM table_name;
SUM(): Returns the sum of a numeric column.
SELECT SUM(column_name) FROM table_name;
AVG(): Returns the average value of a numeric column.
SELECT AVG(column_name) FROM table_name;
MIN(): Returns the smallest value in a column.
SELECT MIN(column_name) FROM table_name;
MAX(): Returns the largest value in a column.
SELECT MAX(column_name) FROM table_name;
6. Subqueries
Subquery: A query inside another query (can be used in SELECT, INSERT, UPDATE, DELETE,
etc.).
SELECT column1
FROM table_name
WHERE column2 = (SELECT column2 FROM table_name2 WHERE condition);
Correlated Subquery: Subquery that references columns from the outer query.
SELECT column1
FROM table1 t1
WHERE column2 = (SELECT column2 FROM table2 t2 WHERE t1.column1 = t2.column1);
7. Indexes
Index: Improves the speed of data retrieval operations on a table at the cost of additional
storage and slower writes.
CREATE INDEX index_name ON table_name (column1, column2);
8. Normalization and Denormalization
Normalization: The process of organizing data in the database to reduce redundancy and
dependency by dividing large tables into smaller, related tables.
o 1NF (First Normal Form): Remove duplicate rows and ensure each column contains
atomic values.
o 2NF (Second Normal Form): Ensure all non-key attributes depend on the primary
key.
o 3NF (Third Normal Form): Ensure there is no transitive dependency.
Denormalization: The process of introducing redundancy into a database by combining
tables to improve query performance, often used in data warehousing.
9. Transactions and ACID Properties
Transactions: A transaction is a set of SQL operations that execute as a unit, ensuring the
integrity of the database.
ACID Properties:
o Atomicity: All operations in a transaction are completed, or none are.
o Consistency: The database moves from one valid state to another.
o Isolation: Transactions do not interfere with each other.
o Durability: Once a transaction is committed, it cannot be undone.
Transaction Commands:
o COMMIT: Saves changes made in the current transaction.
o COMMIT;
o ROLLBACK: Reverts changes made in the current transaction.
o ROLLBACK;
o SAVEPOINT: Sets a point in the transaction to which you can roll back.
o SAVEPOINT savepoint_name;
10. Views, Triggers, and Stored Procedures
Views: Virtual tables based on the result of a SELECT query. They simplify complex queries
and can improve security.
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
Triggers: Automatically execute a set of SQL commands in response to certain events (e.g.,
INSERT, UPDATE, DELETE).
CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
-- trigger logic
END;
Stored Procedures: Predefined SQL code that can be executed with specific parameters.
CREATE PROCEDURE procedure_name (parameters)
BEGIN
-- SQL logic
END;
11. Best Practices for SQL
Use Aliases: To simplify column and table references, especially when using joins.
SELECT t1.column1, t2.column2
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id;
*Avoid SELECT : Specify only the columns you need to improve performance and clarity.
SELECT column1, column2 FROM table_name;
Use Indexes Wisely: Index frequently queried columns, but avoid over-indexing as it can slow
down insert/update operations.
Optimize Queries: Use EXPLAIN (or similar) to analyze query performance and find
bottlenecks.
Handle NULLs Appropriately: Be aware of how NULL values are treated in SQL comparisons
and functions.
SQL is a fundamental skill for anyone working with relational databases, and mastering its core
components will enable you to query, modify, and optimize data efficiently. Let me know if you’d like
more detail on any specific SQL topic!