Sql Notes
Sql Notes
Sql Notes
SQL refresher
Introduction
Introduction to DBMS
SQL Keywords
SQL Data Types
SQL Operators
DDL (Data Definition Language) Functions in SQL
1. CREATE TABLE
DDL (Data Definition Language) Functions in SQL
1. CREATE TABLE
2. ALTER TABLE
3. DROP TABLE
4. TRUNCATE TABLE
Key Differences Between TRUNCATE TABLE and DELETE
DML (Data Manipulation Language) Commands in SQL
1. INSERT
2. UPDATE
3. DELETE
Query Clauses for Data Retrieval
1. JOIN
2. GROUP BY
3. ORDER BY
4. HAVING
Aggregate Queries in SQL
SQL refresher 1
Common Aggregate Functions
Using Aggregate Functions with GROUP BY
Filtering Groups with HAVING
Constraints and Assertions
Constraints
Assertions
Characteristics of Assertions:
Syntax of Assertions:
Example of an Assertion:
Constraints vs. Assertions
Views in SQL
Creating Views
Syntax
Example
Modifying Views
Syntax
Example
Dropping Views
Syntax
Example
Additional Notes
Joins
Sample Data
employees Table
departments Table
1. Inner Join
2. Left Join (or Left Outer Join)
3. Right Join (or Right Outer Join)
4. Full Outer Join
5. Self Join
6. Cross Join
Advanced Function
1. String Functions
CONCAT
LENGTH
SUBSTRING
REPLACE
UPPER
LOWER
2. Date and Time Functions
TIMESTAMP
SQL refresher 2
DATEPART
DATEADD
3. Mathematical Functions
FLOOR
ABS
MOD
ROUND
CEILING
4. Conditional Functions
CASE
NULLIF
COALESCE
Subqueries
1. Nested Subqueries
2. Correlated Subqueries
Indexes
What is an Index?
Types of Indexes
Advantages of Indexes
Disadvantages of Indexes
Example
Managing Indexes
Query Optimization
Transactions
1. SQL Transactions
BEGIN
COMMIT
ROLLBACK
SAVEPOINT
2. ACID Properties
3. Transaction Isolation Levels
Data Integrity and Security
Data Integrity
1. Data Integrity Constraints
Data Security
1. GRANT and REVOKE
Database Security Best Practices
Functions and Procedures
Stored Procedures
Key Features of Stored Procedures
Example of a Stored Procedure
Functions
SQL refresher 3
Key Features of Functions
Example of a Function
Comparison
Performance Optimizations
1. Query Analysis Techniques
1.1. Indexes
1.2. Optimizing Joins
1.3. Reducing Subqueries
1.4. Selective Projection
2. Additional Optimization Techniques
2.1. Query Execution Plans
2.2. Partitioning
2.3. Caching
Triggers in SQL
Types of Triggers
Components of a Trigger
Benefits of Using Triggers
Considerations
Introduction
SQL (Structured Query Language) is a standardized programming language
specifically designed for managing and manipulating databases. SQL is used to
communicate with a database to perform various tasks like querying data,
updating records, deleting data, and creating or modifying database structures
(like tables).
SQL is the backbone of most database systems, including popular ones like
MySQL, PostgreSQL, Oracle, and Microsoft SQL Server. Its simplicity and
powerful capabilities make it an essential tool for anyone working with data.
Here are some key points about SQL:
Data Manipulation: With SQL, you can retrieve, insert, update, and delete
data in a database.
Data Definition: SQL also allows you to define the structure of your data by
creating or altering tables, views, and other database objects.
SQL refresher 4
Data Control: SQL provides commands to control access to data, ensuring
only authorized users can perform certain actions.
Introduction to DBMS
A Database Management System (DBMS) is software that allows users to
store, retrieve, update, and manage data in a structured and efficient manner.
The primary purpose of a DBMS is to provide an organized way to handle large
amounts of information and ensure data consistency, security, and
accessibility.
Key functions of a DBMS include:
Data Storage: The DBMS provides a structured way to store data in tables,
rows, and columns.
Data Retrieval: Users can retrieve data using queries (written in SQL) to get
specific information from the database.
Data Integrity: The DBMS ensures that the data remains accurate and
consistent through rules and constraints.
Data Security: The DBMS controls access to the data, ensuring that only
authorized users can perform certain operations.
Data Backup and Recovery: The DBMS provides tools to back up data and
recover it in case of failures or errors.
SQL Keywords
SQL keywords are reserved words that have special meaning in SQL queries
and commands. They are used to define the structure of SQL statements and
specify the operations you want to perform on the database. Here are some
common SQL keywords:
SQL refresher 5
INSERT INTO table_name (column1, column2) VALUES (value
1, value2);
JOIN : Combines rows from two or more tables based on a related column.
SQL refresher 6
ORDER BY : Sorts the result set by one or more columns.
age INT;
name VARCHAR(50);
code CHAR(5);
description TEXT;
birth_date DATE;
created_at DATETIME;
price FLOAT;
SQL refresher 7
is_active BOOLEAN;
SQL Operators
SQL operators perform operations on data in SQL statements. Here are some
common operators:
+ : Addition
: Subtraction
: Multiplication
/ : Division
= : Equal to
SQL refresher 8
IN: Checks if a value is within a set of values.
1. CREATE TABLE
Purpose: Creates a new table in the database.
Syntax:
1. CREATE TABLE
Purpose: Creates a new table in the database.
Syntax:
SQL refresher 9
...
);
Example:
This command creates a table named employees with columns for employee ID,
first name, last name, and hire date.
2. ALTER TABLE
Purpose: Modifies an existing table's structure, such as adding, deleting, or
modifying columns.
Syntax (Adding a Column):
SQL refresher 10
ALTER TABLE employees DROP COLUMN email;
3. DROP TABLE
Purpose: Deletes an entire table and all of its data from the database.
Syntax:
Example:
This command removes the employees table from the database permanently.
4. TRUNCATE TABLE
Purpose: Removes all rows from a table but keeps the table structure for future
use. It is usually faster than DELETE because it does not generate individual row
delete operations.
Syntax:
Example:
This command deletes all rows in the employees table but retains the table
structure and its columns.
SQL refresher 11
TRUNCATE TABLE :
DELETE :
Removes rows one at a time and can be slower for large tables.
1. INSERT
Purpose: Adds new records to a table.
Syntax:
Example:
2. UPDATE
Purpose: Modifies existing records in a table.
Syntax:
SQL refresher 12
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE employees
SET last_name = 'Smith'
WHERE employee_id = 1;
3. DELETE
Purpose: Removes records from a table.
Syntax:
Example:
1. JOIN
Purpose: Combines rows from two or more tables based on a related column.
Types of Joins:
SQL refresher 13
LEFT JOIN (or LEFT OUTER JOIN ): Returns all rows from the left table and
matched rows from the right table.
RIGHT JOIN (or RIGHT OUTER JOIN ): Returns all rows from the right table and
matched rows from the left table.
FULL JOIN (or FULL OUTER JOIN ): Returns all rows when there is a match in one
of the tables.
2. GROUP BY
Purpose: Groups rows that have the same values into summary rows.
Syntax:
Example:
SQL refresher 14
GROUP BY department_id;
3. ORDER BY
Purpose: Sorts the result set by one or more columns.
Syntax:
Example:
4. HAVING
Purpose: Filters groups based on a specified condition, used in conjunction
with GROUP BY .
Syntax:
Example:
SQL refresher 15
This query shows departments with more than 10 employees.
Syntax:
Example:
This query returns the total number of employees.
2. SUM()
Syntax:
Example:
This query returns the total salary of all employees.
3. AVG()
SQL refresher 16
Syntax:
Example:
This query returns the average salary of employees.
4. MIN()
Syntax:
Example:
This query returns the lowest salary among employees.
5. MAX()
Syntax:
Example:
This query returns the highest salary among employees.
Syntax:
SQL refresher 17
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
Example:
Example:
SQL refresher 18
Constraints
Constraints are rules applied to database tables to enforce data integrity and
consistency. They are defined when creating or altering tables and ensure that
data adheres to specific requirements. Here are the main types of constraints:
Ensures that each row in a table has a unique identifier and that no null
values are allowed in the primary key column(s).
Syntax:
Ensures that the value in a column (or a set of columns) matches the
value in a column in another table, establishing a relationship between
the two tables.
Syntax:
3. Unique Constraint
Ensures that all values in a column (or a set of columns) are unique
across the table.
Syntax:
SQL refresher 19
email VARCHAR(100) UNIQUE
);
4. Check Constraint
Syntax:
Syntax:
6. Default Constraint
Syntax:
Assertions
Assertions are a type of constraint that is more complex and used to enforce
business rules at a higher level. Unlike column-level constraints, assertions can
SQL refresher 20
involve multiple tables and more complex conditions.
Characteristics of Assertions:
1. Complex Conditions: Assertions can involve complex conditions and
multiple tables.
2. Global Constraints: They are applied at the database level and ensure that
certain conditions hold true across the entire database.
Syntax of Assertions:
In SQL, the creation and management of assertions are not supported in all
RDBMS implementations directly as they are part of the SQL standard. For
those databases that support them, the syntax can be:
Example of an Assertion:
Suppose we want to ensure that the total salary paid to employees in a
department does not exceed a certain limit:
SQL refresher 21
Scope:
Complexity:
Support:
Views in SQL
A view in SQL is a virtual table that provides a way to present data from one or
more tables. Unlike a regular table, a view does not store data itself but displays
data dynamically based on a query defined when the view is created. Views are
useful for simplifying complex queries, providing a layer of security, and
presenting data in a specific format.
Creating Views
To create a view, you use the CREATE VIEW statement. This statement defines the
view's name and the query that determines what data the view will display.
Syntax
Example
SQL refresher 22
Create a view to display employee details from the employees table where the
department is 'Sales':
Modifying Views
To modify an existing view, you use the CREATE OR REPLACE VIEW statement. This
statement allows you to redefine the view with a new query or change its
structure.
Syntax
Example
Suppose we want to modify the SalesEmployees view to also include the hire date:
Dropping Views
To remove a view from the database, you use the DROP VIEW statement. This
statement deletes the view definition but does not affect the underlying tables
or data.
SQL refresher 23
Syntax
Example
To drop the SalesEmployees view:
Additional Notes
1. Views vs. Tables:
Views: Are virtual and do not store data but represent data from one or
more tables.
2. Updating Views:
Some views are updatable, meaning you can perform INSERT , UPDATE , or
DELETE operations on them if the view is based on a single table and
meets certain criteria.
Not all views are updatable. Views that involve joins, aggregations, or
complex queries may not support direct updates.
3. Materialized Views:
4. Security:
Joins
Sample Data
SQL refresher 24
employees Table
1 Alice 101
2 Bob 102
3 Charlie NULL
4 David 104
departments Table
department_id department_name
101 HR
102 IT
103 Finance
104 Marketing
1. Inner Join
SQL Query
Result
1 Alice HR
2 Bob IT
4 David Marketing
SQL refresher 25
SELECT employees.employee_id, employees.employee_name, depa
rtments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Result
1 Alice HR
2 Bob IT
3 Charlie NULL
4 David Marketing
Result
1 Alice HR
2 Bob IT
4 David Marketing
SQL refresher 26
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
Result
1 Alice HR
2 Bob IT
3 Charlie NULL
4 David Marketing
5. Self Join
SQL Query
Assuming there's a manager_id column in the employees table to indicate the
manager:
Result
Employee1 Employee2
Alice Bob
6. Cross Join
SQL Query
SQL refresher 27
FROM employees
CROSS JOIN departments;
Result
1 Alice HR
1 Alice IT
1 Alice Finance
1 Alice Marketing
2 Bob HR
2 Bob IT
2 Bob Finance
2 Bob Marketing
3 Charlie HR
3 Charlie IT
3 Charlie Finance
3 Charlie Marketing
4 David HR
4 David IT
4 David Finance
4 David Marketing
Advanced Function
Certainly! Here are the examples and results for each SQL advanced function:
1. String Functions
CONCAT
Example
Result
SQL refresher 28
greeting
Hello World
LENGTH
Example
Result
length
11
SUBSTRING
Example
Result
part
Hello
REPLACE
Example
Result
new_string
Hello SQL
UPPER
SQL refresher 29
Example
Result
uppercase_string
HELLO WORLD
LOWER
Example
Result
lowercase_string
hello world
TIMESTAMP
Example
Result
current_timestamp
2024-08-12 15:30:00
DATEPART
Example
SQL refresher 30
Result
year
2024
DATEADD
Example
Result
new_date
2024-08-22
3. Mathematical Functions
FLOOR
Example
Result
floored_value
123
ABS
Example
Result
absolute_value
123.456
SQL refresher 31
MOD
Example
Result
remainder
ROUND
Example
Result
rounded_value
123.46
CEILING
Example
Result
ceiling_value
124
4. Conditional Functions
CASE
Example
SQL refresher 32
SELECT employee_id,
CASE
WHEN department_id = 101 THEN 'HR'
WHEN department_id = 102 THEN 'IT'
ELSE 'Other'
END AS department_name
FROM employees;
Result
employee_id department_name
1 HR
2 IT
3 Other
4 Other
NULLIF
Example
Result
result
NULL
COALESCE
Example
Result
result
default
SQL refresher 33
Subqueries
Subqueries are queries nested inside another query and can be categorized
into two main types: nested subqueries and correlated subqueries. Here's a
detailed explanation of both:
1. Nested Subqueries
Definition: A nested subquery is a query embedded within another SQL query.
The subquery is executed once, and its result is used by the outer query.
Characteristics:
Often used to retrieve a single value or a set of values that the outer query
will use.
Example:
To find the names of employees who have the highest salary in their
department:
SELECT name
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees e2
WHERE e2.department_id = employees.department_id
);
Explanation:
The inner query retrieves the maximum salary for each department.
The outer query selects the names of employees whose salary matches
this maximum.
SQL refresher 34
2. Correlated Subqueries
Definition: A correlated subquery is a subquery that references columns from
the outer query. It is executed repeatedly, once for each row processed by the
outer query.
Characteristics:
Can be used to compare each row in the outer query to the results of the
subquery.
Example:
Using the same employees and departments tables, to find employees whose
salary is higher than the average salary of their department:
SELECT name
FROM employees e1
WHERE salary > (
SELECT AVG(salary)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
Explanation:
The inner query calculates the average salary for each department using
e1.department_id from the outer query.
The outer query retrieves the names of employees whose salary is greater
than the average salary of their respective department.
Indexes
Indexes are critical components in database management systems (DBMS) that
enhance the speed and efficiency of data retrieval operations. They work
similarly to an index in a book, allowing the database to find specific rows of
data quickly without scanning the entire table.
What is an Index?
SQL refresher 35
An index is a database object that improves the speed of data retrieval
operations on a database table. It maintains a sorted list of values from one or
more columns of the table and helps quickly locate rows based on those
values.
Types of Indexes
1. Single-Column Index:
Usage: Speeds up queries that filter or sort data based on this column.
table.
3. Unique Index:
Definition: Ensures that the values in the indexed column(s) are unique
across the table.
4. Full-Text Index:
SQL refresher 36
Definition: Used for searching large text data, allowing for efficient full-
text searches.
5. Bitmap Index:
Example: Creating a bitmap index on a gender column with only 'M' and
'F' values.
6. Clustered Index:
7. Non-Clustered Index:
Definition: A separate structure from the table that points to the rows in
the table. A table can have multiple non-clustered indexes.
Usage: Improves performance for queries that use columns not covered
by the clustered index.
SQL refresher 37
CREATE NONCLUSTERED INDEX idx_last_name ON employees (la
st_name);
Advantages of Indexes
1. Speed: Indexes significantly improve query performance by reducing the
amount of data scanned.
Disadvantages of Indexes
1. Storage Overhead: Indexes consume additional disk space.
Example
Consider a customers table with columns customer_id , name , and email . To
improve the performance of queries that search by email , you can create an
index:
will perform faster as the database can quickly locate the row based on the
indexed email column, rather than scanning the entire table.
Managing Indexes
1. Creating Indexes
SQL refresher 38
Purpose: To speed up data retrieval operations by creating a structured data
path to quickly access rows.
Considerations:
Which Columns to Index: Index columns that are frequently used in WHERE
Example:
Methods:
Database Performance Tools: Use built-in tools to monitor index usage and
performance metrics.
Methods:
-- Rebuild Index
ALTER INDEX idx_customer_name ON customers REBUILD;
SQL refresher 39
-- Reorganize Index
ALTER INDEX idx_customer_name ON customers REORGANIZE;
4. Dropping Indexes
Purpose: To remove indexes that are no longer needed, freeing up resources
and reducing overhead.
Example:
5. Index Maintenance
Updating Statistics: Ensure that database statistics are up-to-date for the
optimizer to make informed decisions.
Query Optimization
1. Understanding Query Execution Plans
Purpose: To analyze how the database executes a query and where
optimizations can be made.
Index Coverage: Ensure indexes cover columns used in WHERE , JOIN , and
ORDER BY clauses.
SQL refresher 40
3. Query Refactoring
Purpose: To rewrite queries for better performance.
Methods:
Use Proper Joins: Use appropriate join types and ensure they are indexed.
Example:
-- Instead of
SELECT * FROM orders WHERE customer_id = 123;
-- Use
SELECT order_id, order_date FROM orders WHERE customer_id =
123;
Check Join Order: Analyze the order of joins for optimal performance.
5. Optimizing Subqueries
Purpose: To ensure subqueries do not degrade performance.
Methods:
Example:
-- Subquery
SELECT name FROM customers WHERE id IN (SELECT customer_id
FROM orders WHERE amount > 100);
-- Join
SELECT c.name FROM customers c JOIN orders o ON c.id = o.cu
stomer_id WHERE o.amount > 100;
SQL refresher 41
6. Managing Query Caching
Transactions
SQL transactions are essential for ensuring data integrity and consistency in
database operations. They allow multiple operations to be executed as a single
unit, ensuring that either all operations are completed successfully or none are,
thus preserving the integrity of the database. Here’s an overview of SQL
transactions, including the concepts of BEGIN , COMMIT , ROLLBACK , SAVEPOINT , ACID
properties, and transaction isolation levels.
1. SQL Transactions
SQL refresher 42
Definition: A transaction is a sequence of one or more SQL operations
executed as a single unit. Transactions ensure that the database remains in a
consistent state, even in the event of errors or system failures.
BEGIN
Purpose: Marks the start of a transaction.
Example:
BEGIN;
COMMIT
Purpose: Finalizes a transaction, making all changes made during the
transaction permanent.
Example:
COMMIT;
Explanation: After a COMMIT , all changes made during the transaction are saved
to the database, and the transaction is completed successfully.
ROLLBACK
Purpose: Undoes all changes made during the transaction, reverting the
database to its previous state.
Example:
ROLLBACK;
Explanation: After a ROLLBACK , any changes made during the transaction are
discarded, and the database returns to the state it was in before the transaction
began.
SAVEPOINT
Purpose: Sets a point within a transaction to which you can later roll back,
allowing partial rollback within a transaction.
Example:
SQL refresher 43
SAVEPOINT savepoint_name;
To Rollback to a Savepoint:
2. ACID Properties
ACID stands for Atomicity, Consistency, Isolation, and Durability. These
properties ensure reliable processing of database transactions.
Example:
SQL refresher 44
Read Committed: Ensures that a transaction can only read data that has
been committed by other transactions. This prevents dirty reads but allows
non-repeatable reads.
Example:
Repeatable Read: Ensures that if a transaction reads a value, it will see the
same value if it reads it again, preventing dirty reads and non-repeatable
reads. However, it can still be affected by phantom reads.
Example:
Example:
SQL refresher 45
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(50)
);
Example:
Unique Constraint:
Example:
Check Constraint:
Example:
SQL refresher 46
CHECK (price > 0)
);
Example:
Data Security
Data Security focuses on protecting data from unauthorized access and
ensuring it is only accessible to those with appropriate permissions.
Example:
REVOKE:
Example:
SQL refresher 47
Authentication and Authorization
Data Encryption
Regular Backups
Patch Management
Database Configuration
SQL refresher 48
Secure Database Configuration: Adjust settings to enhance security.
Example:
Access Controls
Data Masking
SQL refresher 49
2. Performance: They can improve performance by reducing the amount of
information sent between applications and the database and by allowing
the database to optimize execution plans.
4. Error Handling: They can include error handling mechanisms, which allows
for more robust and controlled execution.
CALL GetEmployeeDetails(101);
Functions
Functions are similar to stored procedures but are designed to return a single
value. They are used to perform calculations or transformations and return a
result.
SQL refresher 50
Key Features of Functions
1. Return Value: Functions return a single value, which can be used in SQL
queries or expressions.
Example of a Function
Creating a Function:
Modifying a Function:
Dropping a Function:
SQL refresher 51
Comparison
Stored Procedures:
Functions:
Performance Optimizations
1. Query Analysis Techniques
1.1. Indexes
Indexes are database objects that speed up the retrieval of rows by providing
quick access to data based on the values in one or more columns.
Types of Indexes:
Unique Index: Ensures that the values in the indexed column(s) are
unique.
Example:
Usage:
Indexes are automatically used by the database optimizer for SELECT
SQL refresher 52
statements. However, they can slow down INSERT, UPDATE, and DELETE
operations due to the overhead of maintaining the index.
LEFT JOIN: Returns all rows from the left table and matched rows from
the right table.
RIGHT JOIN: Returns all rows from the right table and matched rows
from the left table.
Optimize Join Conditions: Ensure that join conditions use indexed columns.
Example:
Tip: Avoid using complex joins with multiple tables unless necessary, as they
can significantly impact performance.
Use Joins Instead: Often, using joins can be more efficient than
subqueries.
SQL refresher 53
Example:
Example:
*Avoid SELECT ***: Selecting all columns can result in fetching more data
than required, impacting performance.
SQL refresher 54
Use Database Tools: Most database systems offer tools to view and
analyze execution plans (e.g., EXPLAIN in MySQL or EXPLAIN ANALYZE in
PostgreSQL).
2.2. Partitioning
Partitioning divides large tables into smaller, more manageable pieces while
maintaining them as a single logical table.
Types of Partitioning:
Example:
2.3. Caching
Caching stores the results of frequent queries in memory to reduce the need to
execute the same query multiple times.
Triggers in SQL
SQL refresher 55
Triggers are special types of stored procedures in SQL that automatically
execute or "fire" in response to certain events on a table or view. They are used
to maintain data integrity, enforce business rules, or automate tasks within the
database.
Types of Triggers
1. BEFORE Trigger
Syntax:
Example:
2. AFTER Trigger
SQL refresher 56
Use Case: Update other tables or logs after a change has been
committed.
Syntax:
Example:
3. INSTEAD OF Trigger
Syntax:
SQL refresher 57
Example:
This trigger inserts data into an archive table instead of the main employees
table.
Components of a Trigger
Trigger Name: A unique name for the trigger within the database.
Trigger Event: The database event that activates the trigger (e.g., INSERT ,
UPDATE , DELETE ).
Trigger Timing: Specifies whether the trigger should run BEFORE or AFTER
the event.
Trigger Action: The code that executes in response to the event, typically
written in SQL.
Auditing and Logging: Track changes and maintain logs for auditing
purposes.
Considerations
SQL refresher 58
Performance Impact: Triggers can affect database performance, especially
if they execute complex logic or are triggered frequently.
Recursive Triggers: Be cautious with triggers that might call other triggers,
leading to recursive actions and potential performance issues.
SQL refresher 59