SQL Tutorial
SQL Tutorial
1. Schema
schema is a logical structure that organizes and groups database objects, such
as tables, views, indexes, stored procedures, functions, and more. It serves as a
namespace within a database, allowing for better management, organization, and
control of database objects.
1. Key Features of a Schema
1. Namespace:
o Objects within a schema are uniquely identified by their name and the
schema they belong to.
o Example: schema_name.table_name.
2. Separation:
o Schemas allow the logical separation of objects within a database,
making it easier to manage and secure them.
3. Ownership and Security:
o Each schema is owned by a specific database user or role.
o Permissions can be assigned at the schema level to control access to all
objects within the schema.
4. Multi-Schema Support:
o Many databases allow multiple schemas in a single database, such as in
PostgreSQL, SQL Server, MySQL, and Oracle.
2. Database:
3. Table:
Collection of rows and columns, Data’s are presented in the rows and
columns.
4. Few Databases:
1. Oracle
2. MS SQL
3. MySQL – invented by Oracle
4. DB2
5. Sybase
6. MongoDB
7. PostgreSQL
5. Why Database?
Before 1970, they stored in flat file.
In Database, almost single line which fetch the data you need
1. Fast
2. Reliable
3. Secure
4. Better memory conceptions
6. Stored procedure
A stored procedure is a prepared SQL code that you can save, so the
code can be reused over and over again. ... You can also pass parameters to a stored
procedure, so that the stored procedure can act based on the parameter value(s)
that is passed
BLOB CLOB
The full form of Blob is a Binary Large The full form of Clob is Character Large
OBject. OBject.
This is used to store large binary data. This is used to store large textual data.
This stores values in the form of This stores values in the form of
binary streams. character streams.
Using this you can stores files like Using this you can store files like text
videos, images, gifs, and audio files. files, PDF documents, word documents
etc.
MySQL supports this with the MySQL supports this with the following
following datatypes: datatypes:
TINYBLOB TINYTEXT
BLOB TEXT
BLOB CLOB
MEDIUMBLOB MEDIUMTEXT
LONGBLOB LONGTEXT
The Blob object in JDBC points to the The Blob object in JDBC points to the
location of BLOB instead of holding its location of BLOB instead of holding its
binary data. character data.
Category Keywords
2. Identifiers
Definition: Names used to identify database objects such as tables, columns,
databases, schemas, indexes, views, and constraints.
Purpose: Enable users to reference database objects in queries.
Examples:
o Table names: employees, sales_data
o Column names: employee_id, salary
o Schema names: hr, finance
Rules for Identifiers
1. Naming Conventions:
o Should begin with a letter (a–z, A–Z).
o Can include letters, numbers (0–9), and underscores (_).
o Avoid starting with numbers or using special characters like $, @, #.
2. Case Sensitivity:
o SQL keywords are case-insensitive (SELECT is the same as select).
o Identifiers' case sensitivity depends on the database (e.g., MySQL is
case-insensitive by default, PostgreSQL is case-sensitive).
3. Reserved Words:
o Avoid using SQL keywords as identifiers unless enclosed in delimiters
(e.g., backticks in MySQL or double quotes in PostgreSQL).
SELECT "SELECT" FROM my_table; -- Using a keyword as a column name
4. Length Restrictions:
o Varies by database, but typically identifiers should not exceed 128
characters.
o
2. Practical Examples
Keywords in Action
SELECT employee_id, first_name, salary
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;
Keywords: SELECT, FROM, WHERE, ORDER BY, DESC.
Identifiers in Action
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
salary DECIMAL(10, 2)
);
Identifiers: employees, employee_id, first_name, salary.
2. Clause
A clause is a component of an SQL statement. It defines specific parts of the SQL
statement and gives additional instructions or conditions.
Common SQL Clauses:
SELECT: Specifies the columns to retrieve.
Example: SELECT name, salary FROM employees;
FROM: Specifies the source table.
Example: FROM employees
WHERE: Filters rows based on a condition.
Example: WHERE salary > 5000
GROUP BY: Groups rows sharing a value into summary rows.
Example: GROUP BY department
HAVING: Filters groups after aggregation.
Example: HAVING COUNT(*) > 5
ORDER BY: Sorts the result.
Example: ORDER BY name ASC
3. Predicate
A predicate is a condition or logical expression used within clauses like WHERE,
HAVING, or ON (in joins). It evaluates to TRUE, FALSE, or NULL.
Examples of Predicates:
Comparison Predicate:
Example: salary > 5000
Range Predicate:
Example: age BETWEEN 30 AND 50
Membership Predicate:
Example: department IN ('IT', 'HR')
Null Predicate:
Example: manager_id IS NULL
Pattern Matching Predicate:
Example: name LIKE 'J%'
Existence Predicate:
Example: EXISTS (SELECT 1 FROM departments WHERE manager_id =
[Link])
Example:
Multi-line Comments
Enclosed between /* and */. Use for longer explanations or block comments.
Example:
Best Practices for Comments
Be concise but meaningful: Explain the "why," not just the "what."
🚫 -- This is a query.
✅ -- Retrieve IT employees earning above 5000.
Avoid redundant comments: Don’t state the obvious.
🚫 -- Select name and salary from employees.
✅ -- Fetch employee details for salary analysis.
Update comments when code changes: Outdated comments can be
misleading.
b. Capitalize Keywords
Write SQL keywords in uppercase for clarity.
Example:
c. Use Line Breaks for Logical Segments
Break long statements into multiple lines for better readability.
Example:
Example:
9. Data Types:
DOUBLE
PRECISION(size, d)
b. BULK INSERT
Here’s a detailed overview of BULK INSERT and data import/export techniques
in MySQL, focusing on efficient methods to handle large datasets.
Example:
Import data from [Link] into a table named employees:
Example:
Export the employees table to a CSV file:
Export a Table:
g. Granting Permissions:
Ensure the MySQL user has the necessary permissions:
8. Practical Workflow
h. a. Import Workflow:
1. Prepare the file ([Link]):
2. Create a table:
b. Export Workflow:
1. Export the table to a file:
a. Transaction Management
Transaction management is a crucial aspect of database systems and other
applications that involve data manipulation. It ensures that data remains consistent
and reliable even when multiple operations are performed concurrently or in the
face of system failures.
What is a Transaction?
In the context of databases, a transaction is a sequence of one or more
operations performed as a single logical unit of work. These operations could
include:
Reading data: Retrieving information from the database.
Writing data: Inserting, updating, or deleting data in the database.
ACID Properties
To guarantee data integrity, transactions must adhere to the ACID properties:
Atomicity: A transaction is treated as a single, indivisible unit of work. Either
all operations within the transaction are completed successfully, or none are.
If any part of the transaction fails, the entire transaction is rolled back, and the
database is restored to its previous state.
Consistency: A transaction must maintain the database's integrity constraints.
It ensures that the database transitions from one valid state to another. If a
transaction violates any constraints, it is rolled back.
Isolation: Transactions should be isolated from each other, meaning that
concurrent transactions should not interfere with each other's execution. Each
transaction should operate as if it were the only transaction running on the
database.
Durability: Once a transaction is committed (successfully completed), the
changes made to the database are permanent and will survive even system
failures such as power outages or crashes.
Why is Transaction Management Important?
Transaction management is essential for several reasons:
Concurrency Control: In environments where multiple users or applications
access the database simultaneously, transaction management prevents data
corruption and ensures that transactions are executed in a consistent and
predictable manner.
Error Recovery: If a system failure occurs during a transaction, transaction
management allows the database to recover to a consistent state by rolling
back any incomplete transactions.
Data Integrity: By enforcing the ACID properties, transaction management
guarantees that data remains accurate and reliable, even in the face of errors
or concurrent access.
2. COMMIT
Permanently saves changes made during the transaction.
Syntax:
3. ROLLBACK
Reverts all changes made during the transaction since the last commit.
Syntax:
4. SAVEPOINT
Creates a checkpoint within a transaction that you can roll back to without
rolling back the entire transaction.
Syntax:
5. ROLLBACK TO SAVEPOINT
Reverts changes to the specified savepoint.
Syntax:
6. RELEASE SAVEPOINT
Removes a savepoint.
Syntax:
7. SET AUTOCOMMIT
By default, MySQL runs in autocommit mode, where each statement is
treated as a transaction and committed automatically.
To disable autocommit:
To enable autocommit:
Using SAVEPOINT
Explanation: The rollback reverts to step1, but changes before the savepoint
remain.
7. Benefits of Transactions
Prevents partial updates.
Ensures data integrity.
Allows for error handling in complex operations.
Provides greater control over database operations.
Rollback Example:
Delete Truncate
1 It is possible to delete only particular Truncate can only delete all the
record or all the records records, it is not possible to delete
particular record
2 Delete operation is slower than Truncate Operation is very faster
Truncate
3 Deleted recorded can be rolled back. With Truncate command, it is not
possible to roll back the records.
13.
14. Constraints:
SQL constraints are used to specify rules for data in a table.
[Link]
Will not allow the duplicate values.
[Link] Null
The column will not all the null values (it means column should have
some value)
[Link] Key
Combination of both Unique and Not Null
[Link] Key
The FOREIGN KEY constraint prevents invalid data from being inserted
into the foreign key column, because it has to be one of the values
contained in the parent table.
[Link]
[Link]
2. Logical Operators:
And, or and Not
4. In, not in
6. Wildcards
16. Join
Joining the two or more than the tables and retrieve required no of columns from
the tables.
1. Inner Join
The INNER JOIN is one of the most commonly used joins in SQL. It retrieves
records that have matching values in both tables being joined. If a row in one table
does not have a corresponding row in the other table, it will not be included in the
result.
Syntax:
2. Key Characteristics
Returns rows where there is a match in both tables.
Rows without matches in either table are excluded from the result.
Example
Tables:
Another Example:
employees:
1 Alice 10
2 Bob 20
3 Charlie NULL
departments:
department_id department_name
10 HR
20 IT
30 Finance
name department_name
Alice HR
Bob IT
Explanation:
o Alice and Bob have matching department_id values in the departments
table.
o Charlie does not have a matching department_id, so their row is
excluded.
3. Alias for Simplicity
You can use table aliases to make the query easier to read:
Syntax:
Example:
7. Right Join
Syntax:
Example:
Syntax:
Example:
9. Cross Join
The CROSS JOIN is used to combine every row from one table with every row
from another table, resulting in a Cartesian product of the two tables. Unlike other
types of joins, it does not require any condition to match rows.
Syntax
Example
Tables:
products:
product_id product_name
1 Laptop
2 Smartphone
regions:
region_id region_name
102 Europe
product_name region_name
Laptop Europe
Smartphone Europe
Explanation:
o Each row in the products table is combined with every row in the
regions table.
Result:
product_name region_name
Laptop Europe
Smartphone Europe
Explanation: The result is limited to combinations where the region is Europe.
15. Self Join
Syntax:
Example:
Example:
17. What is the difference between Equi Join and Inner Join in SQL?
An equijoin is a join with a join condition containing an equality operator. An equijoin returns only the rows that have
equivalent values for the specified columns.
An inner join is a join of two or more tables that returns only those rows (compared using a comparison operator) that
satisfy the join condition.
2. Ex:
SELECT employee_number, name
FROM employees emp
WHERE salary > (SELECT AVG(salary)
FROM employees
WHERE department = [Link]);
In the above case, for each employee, the inner query calculates the average salary
for their department.
MySQL provides a wide range of numeric functions that allow you to perform
mathematical calculations and manipulate numeric data. Here's a summary of
commonly used numeric functions in MySQL:
a. Arithmetic Functions
Function Description Example Result
ABS(x) Returns the absolute value of x. ABS(-10) 10
POW(x, y) or
POWER(x, y)
Returns x raised to the power of y. POW(2, 3) 8
d. Random Numbers
Function Description Example Result
e. Bitwise Functions
Function Description Example Result
BIT_AND(x, y) Performs a bitwise AND operation on x and y. BIT_AND(5,3) 1
Usage Example
Result:
2. String Functions:
separator.
Extracts a substring
SUBSTRING(str, pos, SUBSTRING('MySQL', 2,
len) starting at pos for len 3)
ySQ
characters.
Removes
TRIM(str) or leading/trailing spaces
TRIM(' MySQL ') MySQL
TRIM([remstr] FROM str) or specified characters
from a string.
LTRIM(str) Removes leading spaces. LTRIM(' MySQL') MySQL
REPLACE('Hello
REPLACE(str, from_str, Replaces occurrences of Hello
World', 'World',
to_str) from_str with to_str. SQL
'SQL')
Converts a string to
UPPER(str) UPPER('mysql') MYSQL
uppercase.
Converts a string to
LOWER(str) LOWER('MySQL') mysql
lowercase.
Function Description Example Result
INITCAP(str) CONCAT(UPPER(LEFT('mysql
(Not native; Capitalizes the first letter
',1)),LOWER(SUBSTRING('m Mysql
use CONCAT) of each word. ysql',2)))
e. String Comparison
Function Description Example Result
pattern.
Usage Example
Result:
3. Date Functions
MySQL provides a variety of date and time functions to handle, format, and
manipulate date and time values. Here's a detailed list of commonly used date
functions:
d. Formatting Dates
Function Description Example Result
Formats a date
DATE_FORMAT(date, DATE_FORMAT('2024-12- 30-Dec-
according to the given
format) 30', '%d-%b-%Y') 2024
format.
Converts a string to a
STR_TO_DATE(str, STR_TO_DATE('30-12- 2024-
date using the
format) 2024', '%d-%m-%Y') 12-30
specified format.
e. Common Format Specifiers:
Specifier Description Example
%y Year (2 digits). 24
%i Minutes. 25
%s Seconds. 36
%p AM or PM. PM
f. Calculating Differences
Function Description Example Result
Usage Example
Result:
23. CallableStatement
CallableStatement interface is used to call the stored procedures and functions
must not have the return type. must have the return type.
We can call functions from the procedure. Procedure cannot be called from
function.
Exception handling using try/catch block can Exception handling using try/catch
be used in stored procedures. can't be used in user defined
functions.
25. Transactions
Transaction represents a single unit of work
The ACID properties describes the transaction management well. ACID stands for
Atomicity, Consistency, Isolation and Durability.
Atomicity means either all successful or none.
Consistency ensures bringing the database from one consistent state to another
consistent state.
Isolation ensures that transaction is isolated from other transaction.
Durability means once a transaction has been committed, it will remain so, even in
the event of errors, power loss etc.
A Common Table Expression (CTE) in SQL is a temporary result set that you can
reference within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs are often
used to simplify complex queries, improve readability, and organize query logic.
28. Views
In PostgreSQL (and many other relational database management systems), a view is
a virtual table that is defined by a SQL query. Unlike a physical table, a view does not
store data itself but rather provides a way to present data from one or more tables in
a specific format. Views can simplify complex queries, encapsulate business logic,
and enhance security by restricting access to specific data.
29. Sequence
a sequence is a database object used to generate a sequence of unique integer
values. Sequences are often used to generate unique primary key values for tables
automatically
Index
an index is a database object that enhances the speed of data retrieval operations on
a table at the cost of additional storage space and overhead during data modification
operations (inserts, updates, and deletes). Indexes are essential for optimizing query
performance, especially for large datasets.
Types of Indexes
B-tree Index:
The default and most common type of index.
Suitable for equality and range queries.
Supports =, <, <=, >, >=, and BETWEEN operators.
Hash Index:
Suitable for equality comparisons (=).
Not as commonly used because it does not support range queries.
GIN (Generalized Inverted Index):
Suitable for indexing composite values, like arrays, JSONB, and full-text search.
Efficient for containment queries (e.g., checking if an array contains a specific
value).
GiST (Generalized Search Tree):
Suitable for complex data types, such as geometric data types and full-text
search.
Supports various types of queries depending on the operator class.
SP-GiST (Space-Partitioned Generalized Search Tree):
Suitable for data that can be divided into non-overlapping partitions.
Useful for certain types of geometric and text search operations.
BRIN (Block Range INdex):
Suitable for very large tables where the data has some natural ordering.
Efficient for range queries and less storage-intensive.
Expression Index:
An index on the result of an expression or function, rather than directly on
column values.
Partial Index:
An index that covers only a subset of rows in a table, based on a specified
condition.
30. Cursor
a cursor is a database object used to retrieve a set of rows generated by a query and
to process them one at a time. Cursors are particularly useful when dealing with
large datasets where you want to process each row individually without loading the
entire result set into memory at once.
Key Characteristics of Cursors
1. Row-by-Row Processing:
o Cursors allow you to fetch and process rows one at a time, which is
useful for operations that require iterative processing of each row in a
result set.
2. Memory Efficiency:
o By not loading the entire result set into memory, cursors help manage
memory usage efficiently, especially with large datasets.
3. State Management:
o Cursors maintain their position within the result set, allowing you to
fetch subsequent rows sequentially.
Declaring and Using Cursors
Cursors are typically used within PostgreSQL functions and stored procedures. Here’s
a basic outline of how to work with cursors:
1. Declare a Cursor:
o Define a cursor to hold the result set of a query.
2. Open the Cursor:
o Execute the query and establish the result set for the cursor.
3. Fetch from the Cursor:
o Retrieve rows from the cursor one at a time or in blocks.
4. Close the Cursor:
o Release the cursor and associated resources.
Example
Here’s a detailed example demonstrating the use of a cursor in a PostgreSQL
function:
1. Creating a Sample Table:
31. Windows Functions
In SQL, window functions are a powerful tool that allows you to perform calculations
across a set of rows that are related to the current row, without causing rows to
become grouped as with standard aggregate functions. This means you can access
and compare data from multiple rows while retaining the individual rows in the
result set.
Key Concepts
Window: The "window" refers to the set of rows on which the function
operates. This window is defined relative to the current row.
OVER() Clause: This clause is essential for window functions. It defines the
window by specifying how the rows are partitioned and ordered.
PARTITION BY: This clause divides the rows into partitions or groups. The
window function is applied to each partition independently.
ORDER BY: This clause specifies the order of rows within each partition. This is
crucial for functions that depend on the order of rows, like ROW_NUMBER()
or LAG().
Types of Window Functions
1. Aggregate Window Functions: These functions perform aggregate
calculations (like SUM(), AVG(), COUNT(), MIN(), MAX()) over a window of
rows.
o Example: Calculate the running total of sales for each day.
2. Ranking Window Functions: These functions assign a rank to each row within
a partition based on a specified order.
o ROW_NUMBER(): Assigns a unique sequential integer to each row
within a partition.
o RANK(): Assigns a rank to each row within a partition, with gaps in the
ranking if there are ties.
o DENSE_RANK(): Assigns a rank to each row within a partition, without
gaps in the ranking even if there are ties.
o NTILE(n): Divides the rows in each partition into n approximately equal
groups and assigns a group number to each row.
3. Value Window Functions: These functions access values from other rows
within the window.
o LAG(column, offset, default): Accesses the value of a column from a
row that is offset rows before the current row.
o LEAD(column, offset, default): Accesses the value of a column from a
row that is offset rows after the current row.
o FIRST_VALUE(column): Returns the first value of a column in the
window.
o LAST_VALUE(column): Returns the last value of a column in the
window.
Example
Let's say you have a table called "Employees" with columns like "Department,"
"EmployeeID," and "Salary."
SQL
SELECT
Department,
EmployeeID,
Salary,
AVG(Salary) OVER (PARTITION BY Department) AS AverageDepartmentSalary
FROM
Employees;
This query calculates the average salary for each department and displays it
alongside each employee's salary. The PARTITION BY Department clause ensures that
the average salary is calculated separately for each department.
Benefits of Window Functions
Simplified Queries: Window functions can simplify complex queries that
would otherwise require subqueries or self-joins.
Improved Performance: In many cases, window functions can be more
efficient than equivalent queries using subqueries or joins.
Enhanced Data Analysis: Window functions enable advanced data analysis
tasks like calculating running totals, moving averages, and rankings.
Window functions are a valuable tool in SQL for performing complex calculations and
analysis on data. They provide a concise and efficient way to access and compare
data from multiple rows without losing the individual row context.
32. SQLSTATE
Class P0 — PL/pgSQL
Error
P0000 plpgsql_error
P0001 raise_exception
P0002 no_data_found
P0003 too_many_rows
P0004 assert_failure