Copyright By : Instagram: Curious_.
Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
SQL
Commands
DDL DML DCL TCL
Data Definition Data Manipulation Data Control Transaction Control
language language language Language
CREATE INSERT GRANT COMMIT
ALTER REVOKE ROLLBACK
UPDATE
SAVEPOINT
DROP DELETE
TRUNCATE
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Day 3:Sql Constraints
Will Post Tomorrow Follow For More
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Day 3:Keys in Sql
Will Post Tomorrow Follow For More
Day 5: Sql Basic
Quaries
Will Post Tomorrow Follow For More
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Day 6:SQL Clauses
Will Post Tomorrow Follow For More
Day 6: SQL Notes Basic to Advanced
Todays Topic: SQL Clauses
Group By
Having
Order By
Where By
What are SQL Clauses?
SQL clauses are the essential components of SQL
(Structured Query Language) statements.
They define the specific tasks you want the SQL
engine to perform, such as retrieving data, filtering
records, or modifying database tables.
Each clause serves a unique purpose and is used in
different contexts to manipulate or query the data
stored in a relational database.
Clauses define specific conditions within SQL
queries.
Common clauses include WHERE, GROUP BY, and
ORDER BY.
Used to refine query results based on criteria.
6.1 GROUP BY
The GROUP BY clause in SQL is used to group rows
that have the same values in specified columns into
aggregated data.
It is commonly used in conjunction with aggregate
functions like COUNT, SUM, AVG, MAX, and MIN to
perform calculations on each group of data.
How GROUP BY Works
When you use the GROUP BY clause, SQL groups
rows that have identical values in the specified
columns and then performs any specified aggregate
calculations on those groups.
The result is a single row for each group, containing
the grouped columns and the results of the
aggregate functions.
Syntax
column1: The column by which you want to group the
data.
aggregate_function(column2): An aggregate function
like COUNT, SUM, AVG, MAX, or MIN applied to
another column.
Example 1: Counting Records in Each Group
Suppose you have a table called sales with columns
product_id, quantity, and sale_date. You want to count
how many times each product has been sold.
This query groups the rows by product_id and counts
the number of records in each group.
Example 2: Summing Values in Each Group
Consider a table called orders with columns
customer_id, order_amount, and order_date. To find
out the total amount each customer has spent, you could
use:
This query groups the rows by customer_id and sums
the order_amount for each customer.
6.2 HAVING
The HAVING clause in SQL is used to filter groups
of rows that are created by the GROUP BY clause.
It is similar to the WHERE clause, but while WHERE
is used to filter individual rows before any grouping
is done, HAVING filters groups after the grouping
has been performed.
How HAVING Works
The HAVING clause is typically used in combination
with aggregate functions (such as COUNT, SUM,
AVG, MAX, MIN) to filter the results based on the
aggregate values.
This allows you to apply conditions to the
summarized data.
Syntax:
column1: The column by which you have grouped the
data.
aggregate_function(column2): An aggregate function
applied to another column.
condition: The condition you want to apply to the
grouped data.
Example 1: Filtering by Aggregate Value
Suppose you have a table called sales with columns
product_id, quantity, and sale_date. You want to find
products that have been sold more than 10 times.
This query groups the rows by product_id, counts the
number of sales for each product, and then filters out
products with 10 or fewer sales.
6.3 ORDER BY
This query groups the rows by product_id, counts the
number of sales for each product, and then filters out
products with 10 or fewer sales.
How ORDER BY Works
When you use the ORDER BY clause, SQL arranges
the rows returned by a query according to the values
in specified columns.
You can sort data numerically, alphabetically, or by
date, and you can specify multiple columns for
sorting.
Syntax
column1, column2, ...: The columns by which you want
to sort the data.
ASC: Sorts the data in ascending order (this is the
default).
DESC: Sorts the data in descending order.
Example 1: Sorting by a Single Column
Suppose you have a table called employees with columns
last_name, first_name, and hire_date. To get a list of
employees sorted by last_name in ascending order:
This query sorts the results by last_name alphabetically
from A to Z.
Example 2: Sorting by Multiple Columns
If you want to sort the employees first by last_name in
ascending order and then by first_name in descending
order:
This query sorts the list by last_name and then by
first_name within each last_name.
5.4 WHERE
The WHERE clause in SQL is used to filter records
based on specified conditions.
It is one of the most fundamental clauses in SQL,
allowing you to retrieve only the rows that meet
certain criteria.
The WHERE clause can be used in SELECT, UPDATE,
DELETE, and other SQL statements to refine the
results.
How WHERE Works
The WHERE clause filters rows from a table based
on the condition you specify. Only the rows that
satisfy the condition are included in the result set.
The condition can include operators like =, >, <, >=, <=,
<>, BETWEEN, LIKE, IN, IS NULL, AND, OR, and
NOT.
Syntax
column1, column2, ...: The columns you want to
retrieve.
table_name: The table from which you want to
retrieve the data.
condition: The criteria that the rows must meet to
be included in the result set.
Example 1: Filtering by a Single Condition
Suppose you have a table called employees with columns
employee_id, last_name, department, and salary. You
want to retrieve all employees who work in the "Sales"
department.
This query returns only the rows where the department
column is equal to "Sales".
Example 2: Using Multiple Conditions with AND
If you want to find all employees in the "Sales"
department who have a salary greater than 50,000:
Summary
GROUP BY Clause: Groups rows by specified columns
for aggregation. Used with aggregate functions.
HAVING Clause: Filters results of GROUP BY based
on aggregate conditions. Applied after grouping.
ORDER BY Clause: Sorts query results by one or
more columns in ascending or descending order.
WHERE Clause: Filters individual rows based on
specified conditions before any grouping or sorting.
Day 7:SQL Joins
Will Post Tomorrow Follow For More
Day 7: SQL Notes Basic to Advanced
Todays Topic: SQL Joins
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
Sql Joins:
A JOIN clause is used to combine rows from two or
more tables, based on a common field between
them
The join clause allows us to retrieve data from two
or more related tables into a meaningful result set.
We can join the table using a SELECT statement
and a join condition.
Combines rows from two or more tables based on
related columns.
Helps retrieve data that spans multiple tables.
Allows for more complex queries by linking table
data.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram Channel @Curious_Coder
The inner join is used to select all matching rows or columns
in both tables or as long as the defined condition is valid in
SQL.
The INNER JOIN keyword selects all rows from both the
tables as long as the condition is satisfied
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
table1: First table.
table2: Second table
matching_column: Column common to both the
tables.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
The LEFT JOIN is used to retrieve all records from
the left table (table1) and the matched rows or
columns from the right table (table2).
If both tables do not contain any matched rows or
columns, it returns the NULL.
LEFT JOIN is also known as LEFT OUTER JOIN.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
The SQL right join returns all the values from the
rows of right table.Copyrighted ByCodeWithCurious.com
: Instagram: Curious_.Programmer
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
It also includes the matched values from left table
but if there is no matching in both tables, it returns
NULL.
RIGHT JOIN is also known as RIGHT OUTER JOIN.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
FULL JOIN creates the result-set by combining
results of both LEFT JOIN and RIGHT JOIN.
The joined tables return all records from both the
tables and if no matches are found in the table, it
places NULL. It is also called a FULL OUTER JOIN.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
SELECT *
FROM Table-1; SELECT * FROM
Table-1 Table-2 Table-1 Table-2 table1
SELECT * INNER JOIN table2
FROM Table-2; ON table1.cn= table2.cn;
Select From two Tables Inner Join
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
SELECT * FROM SELECT * FROM
Table-
Table- Table- table1 Table- table1
1 FULL OUTER JOIN
1 2 RIGHT JOIN table2 2
table2
ON table1.cn= table2.cn;
ON table1.cn= table2.cn
WHERE condition;
Right Join Full Join
SELECT * FROM
Table- Table- table1
Table- 1 SELECT *
LEFT JOIN table2 1
1 22 2 FROM table1
ON table1.cn= table2.cn; 2
3 CROSS JOIN table2
Left Join
Cross Join
Day 8:SQL Aggregate Functions
Will Post Tomorrow Follow For More
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Day 10: SQL Notes Basic to Advanced
Todays Topic: SQL Indexes
Types of SQL Indexes
Creating Indexes
Dropping Indexes
Clustered vs. Non-Clustered Indexes
SQL indexes are database objects that improve the
speed of data retrieval operations on a table at the
cost of additional storage and maintenance overhead.
They work similarly to indexes in books, where they
help you quickly locate specific information without
scanning the entire content.
Improves query performance by speeding up data
retrieval.
Creates a data structure to quickly locate rows in
a table.
Can be applied to one or more columns in a table.
Helps in optimizing search operations, but may
impact write performance.
10.1 Types of Indexes:
Clustered Index: Determines the physical order of
data in the table. A table can have only one clustered
index. The primary key is usually a clustered index by
default.
Non-Clustered Index: Contains a separate structure
from the table's data, with pointers to the actual data
rows. A table can have multiple non-clustered indexes.
Unique Index: Ensures that all values in the index key
are unique. It is often used to enforce uniqueness
constraints on columns.
Composite Index: An index on multiple columns, which
helps in queries that filter or sort based on those
columns.
Full-Text Index: Used for efficient text searches
within large text columns, such as finding words or
phrases in a TEXT field.
Spatial Index: Used for spatial data types in
databases that support geographic data, enabling fast
querying of spatial information.
10.2 Creating Indexes:
Creating indexes in SQL involves defining an index on
one or more columns of a table to improve query
performance. Here’s a step-by-step guide to creating
various types of indexes:
1. Basic Index Creation
To create a basic index on a single column, use the following
syntax:
Example:
2. Unique Index
A unique index ensures that all values in the indexed
column(s) are unique. It is often used to enforce
uniqueness constraints.
Syntax:
10.2 Creating Indexes:
Creating indexes in SQL involves defining an index on
one or more columns of a table to improve query
performance. Here’s a step-by-step guide to creating
various types of indexes:
1. Basic Index Creation
To create a basic index on a single column, use the following
syntax:
Example:
2. Unique Index
A unique index ensures that all values in the indexed
column(s) are unique. It is often used to enforce
uniqueness constraints.
Syntax:
Example:
3. Composite Index
A composite index (or multi-column index) is created on
multiple columns. It is useful for queries that filter or
sort on multiple columns.
Syntax:
Example:
4. Clustered Index
A clustered index determines the physical order of data
in the table. A table can have only one clustered index.
Syntax:
Example:
Note: In some databases like MySQL, the primary key
is automatically used as the clustered index if you don’t
explicitly define one.
5. Full-Text Index
A full-text index is used for efficient searching of
text within large text columns.
SQL Syntax (SQL Server):
Example:
6. Spatial Index
A spatial index is used for spatial data types to enable
fast querying of geographic data.
SQL Syntax (SQL Server):
Example:
10.3 Dropping an Index
Dropping indexes is a straightforward process, but it's
important to understand the implications on your database
performance and structure. Here’s how you can drop
indexes in various SQL databases:
Syntax:
Example:
Considerations Before Dropping an Index
Impact on Performance: Dropping an index can slow down
queries that used it.
Dependencies: Verify the index is not part of a primary
key or unique constraint.
Testing: Test performance changes in a staging
environment first.
10.4 Clustered vs. Non-Clustered Indexes
Aspect Clustered Index Non-Clustered Index
Determines the physical order of Creates a separate structure
Definition
data in the table. from the table's data.
Only one clustered index per table Multiple non-clustered indexes
Number per Table
is allowed. can be created on a table.
Data rows are stored in the order Indexes are stored separately
Data Storage
of the clustered index key. from the data rows.
The index is the actual table with The index contains pointers to
Index Structure
sorted data. the actual data rows.
Typically used on primary keys or Can be used on any column(s) to
Key Usage
unique columns. improve query performance.
Efficient for range queries and Efficient for exact matches
Performance
sorting operations. and queries involving joins.
Alters the physical arrangement Does not alter the physical
Impact on Data
of the data on disk. arrangement of the data.
Often faster for queries involving Speeds up searches, especially
Query Speed
sorting and range queries. for columns frequently queried.
Storage Requires additional storage for
Can lead to fragmentation of data.
Overhead the index structure.
Maintenance involves updating
Updates to the index can be
Maintenance the index separately from the
costly in terms of performance.
data.
Best for columns frequently
Best for columns frequently used
Typical Use Case used in search conditions or
in sorting or range queries.
joins.
SQL Has Been Completed
Total 95 Pages
From Tomorrow I’ll Post Python Notes Series