SQL Final Minimal Print
SQL Final Minimal Print
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
FROM customers
WHERE country = 'Italy'
DROP UPDATE
ALTER DELETE
DROP UPDATE
ALTER DELETE
DROP UPDATE
ALTER DELETE
OPTIONAL: If no columns are specified, SQL expects values for all columns
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE <condition>
SELECT
FirstName
LastName
FROM Employees 2nd SELECT Statement
JOIN Clause
WHERE Clause
GROUP BY Clause
ORDER BY
can be used only at the
end to sort the final Result ORDER BY FirstName
SQL Course | SET Operators
SET RULES
•
•
•
Returns All rows from Returns All rows from Return unique rows in Return only the
both sets, elimination both sets, including first set that are not in common rows
duplicates duplicates second table between two sets
SELECT FirstName, LastName SELECT FirstName, LastName SELECT FirstName, LastName SELECT FirstName, LastName
FROM Customers FROM Customers FROM Customers FROM Customers
SELECT FirstName, LastName SELECT FirstName, LastName SELECT FirstName, LastName SELECT FirstName, LastName
FROM Employees FROM Employees FROM Employees FROM Employees
Single-Row Multi-Row
Functions Functions
Manipulation String
Calculation
Extraction
UPPER RIGHT
LOWER SUBSTRING
TRIM
REPLACE
Single-Row Multi-Row
Functions Functions
Single-Row Multi-Row
Functions Functions
Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query
YEAR CAST
DATEPART
DATENAME
DATETRUNC
EOMONTH
DAY(date)
Runs outer query
DATEPART(part,
Runs outer query date)
MONTH(date)
Runs outer query
DATENAME(part,
Runs outer query date)
YEAR(date)
Runs outer query
DATETRUNC(part,
Runs outer query date)
EOMONTH(date)
Runs outer query
FORMAT(value,
Runs outer query format [,culture])
Optional
Examples
FORMAT(OrderDate,'dd/MM/yyyy')
Runs outer query
FORMAT(OrderDate,'dd/MM/yyyy','ja-JP')
Runs outer query
FORMAT(1234.56,
Runs outer query 'D' ,'fr-FR')
Syntax
CONVERT(data_type,
Runs outer query value [,style])
Optional
Examples
CONVERT(INT,
Runs outer query'124')
CONVERT(VARCHAR,
Runs outer query OrderDate, '34')
Default Style = 0
Syntax
CAST(value AS data_type)
Runs outer query
Examples
CAST('123' AS INT)
Runs outer query
CAST('2025-08-20'
Runs outer query AS DATE)
Syntax
DATEADD(part,
Runs outer query interval, date)
Examples
DATEADD(year,
Runs outer query 2, OrderDate)
DATEADD(month,
Runs outer query -4, OrderDate)
Syntax
DATEDIFF(part,
Runs outer query start_date, end_date)
Examples
DATEDIFF(year,
Runs outer query OrderDate, ShipDate)
DATEDIFF(day,
Runs outer query OrderDate, ShipDate)
Syntax
ISDATE(value)
Runs outer query
ISDATE('2025-08-20')
Runs outer query
ISDATE(2025)
Runs outer query
Single-Row Multi-Row
Functions Functions
returns NULL if both values are equal; otherwise, it returns first value.
In SQL, use IS NULL instead of = NULL to correctly filter rows with NULL values.
CASE
END
High
Sales
100
CASE
WHEN Sales >= 100 THEN 'High' 50
Medium
WHEN Sales >= 50 THEN 'Medium'
75
ELSE 'Low'
90
END
150
Low
CASE
WHEN Country = 'Germany' THEN 'DE' CASE
WHEN Country = 'F' THEN 'Female'
WHEN Country = 'France' THEN 'FR'
WHEN Country = 'M' THEN 'Male'
WHEN Country = 'Italy' THEN 'IT'
ELSE 'n/a'
ELSE 'n/a'
END
END
Germany DE F Female
France FR M Male
END END
Data
Types
Compute aggregates but keep details of individual rows at the same time
It collapse groups of rows into a single row It doesn’t collapse rows into a single row
(Group-Level-Calculations) (Row-Level-Calculations)
COUNT(expr) COUNT(expr)
SUM(expr) SUM(expr)
Aggregate MAX(expr) Aggregate
Functions
MAX(expr)
Functions
MIN(expr) MIN(expr)
MIN(expr) MIN(expr)
ROW_NUMBER()
RANK()
Rank DENSE_RANK()
Functions CUME_DIST()
PERCENT_RANK()
NTILE(n)
LEAD(expr,offset,default)
Value LAG(expr,offset,default)
(Analytics)
Functions FIRST_VALUE(expr)
FIRST_VALUE(expr)
SQL Course | Window Functions Basics |GROUP BY vs WINDOW
SQL Course | Window Functions Basics |GROUP BY vs WINDOW
Window
Syntax
Over Clause
Calculation used
on the Window
Window
Function
ROW_NUMBER()
RANK()
LEAD(expr,offset,default)
Value LAG(expr,offset,default)
(Analytics)
Functions FIRST_VALUE(expr)
FIRST_VALUE(expr)
Calculation used
on the Window
Window
Function
Function
Expression
Conditional Logic SUM(CASE WHEN Sales > 100 THEN 1 ELSE 0 END) OVER (ORDER BY OrderDate)
SUM(expr)
Aggregate MAX(expr)
Functions Numeric
MIN(expr)
MIN(expr)
ROW_NUMBER()
RANK()
LEAD(expr,offset,default)
Value LAG(expr,offset,default)
(Analytics) All Data Type
Functions FIRST_VALUE(expr)
FIRST_VALUE(expr)
SQL Course | Window Functions Basics |Expression
Window
Syntax
Function
Expression
Function Partition
Expression Clause
Calculation is
done individually
on each window
Window
(2)
SUM(expr)
Aggregate MAX(expr)
Functions Numeric
MIN(expr)
MIN(expr)
ROW_NUMBER()
RANK()
LEAD(expr,offset,default)
Value LAG(expr,offset,default)
(Analytics) All Data Type
Functions FIRST_VALUE(expr)
FIRST_VALUE(expr)
SUM(expr)
Aggregate MAX(expr) Optional
Functions Numeric
MIN(expr)
MIN(expr)
ROW_NUMBER()
RANK()
LEAD(expr,offset,default)
Value LAG(expr,offset,default)
(Analytics) All Data Type Required
Functions FIRST_VALUE(expr)
FIRST_VALUE(expr)
Window
Syntax
2# Window 2# Window
Frame
SUM(expr)
Aggregate AVG(expr) Optional Optional
Functions Numeric
MAX(expr)
MIN(expr)
ROW_NUMBER()
RANK()
LEAD(expr,offset,default)
Not allowed
Value LAG(expr,offset,default)
(Analytics) All Data Type Required
Functions FIRST_VALUE(expr) Optional
Should be
FIRST_VALUE(expr) used
Frame
ORDER BY Used?
Runs outer query
Yes
SUM(expr) NumericValues
MAX(expr) NumericValues
COUNT(expr) Returns the number of Rows in a window COUNT(*) OVER (PARTITION BY Porduct)
SUM(expr) Returns the sum of values in a window SUM(Sales) OVER (PARTITION BY Porduct)
Aggregate
AVG(expr) Returns the average of values in a window SUM(Sales) OVER (PARTITION BY Porduct)
Functions
MIN(expr) Returns the minimum value in a window SUM(Sales) OVER (PARTITION BY Porduct)
MAX(expr) Returns the maximum value in a window SUM(Sales) OVER (PARTITION BY Porduct)
Caps 20 3
Caps 10 3 3 Orders for Caps
Caps 5 3
Gloves 30 3
Gloves 70 3 3 Orders for Gloves
Gloves 40 3
Column
Caps 20 3 Caps 20 3
Caps 10 3 Caps 10 3
Caps 5 3 Caps 5 3
This Row
This Row Gloves 30 3 won’t be 30
Gloves 2
is counted counted
Gloves 70 3 Gloves 70 2
Gloves NULL 3 Gloves NULL 2
* Is not allowed!
Caps 20 35
Caps 10 35 20 + 10 + 5= 35
Caps 5 35
Gloves 30 140
Gloves 70 140 30 + 70 + 40= 140
Gloves 40 140
Perform calculations on a set of rows and return a single aggregated value for each row
Caps 20 20 + 10 + 5= 35 Caps 20 35
Caps 10 35 Caps 10 35
Caps 5 Caps 5 35
* Is not allowed!
Caps 20 11
20 + 10 + 5
Caps 10 11 = 11
3
Caps 5 11
Gloves 30 46
30 + 70 + 40
Gloves 70 46 = 46
3
Gloves 40 46
Caps 20 11 Caps 20 11
20 + 10 + 5 20 + 10 + 5
Caps 10 11 = 11 Caps 10 11 = 11
3 3
Caps 5 11 Caps 5 11
Gloves 30 50 Gloves 30 50
30 + 70 30 + 70 + 0
Gloves 70 50 = 50 Gloves 70 50 = 33
2 3
Gloves NULL 50 Gloves 0 50
Returns the minimum value in a window Returns the maximum value in a window
Gloves 30 30 30 70
30 is the Gloves
70 is the
Gloves 70 30 lowest sales Gloves 70 70 highest sales
Gloves 40 30 for Gloves Gloves 40 70 for Gloves
Jan 20 20 Jan 20 20
Current Current
Feb 10 30 Feb 10 30
Row Row
Mar 30 Mar 30
Apr 5 Apr 5
Jun 70 Jun 70
Jul 40 Jul 40
Jan 20 20 Jan 20 20
Feb 10 30 Feb 10 30
Current Current
Row Mar 30 60 Row Mar 30 60
Apr 5 Apr 5
Jun 70 Jun 70
Jul 40 Jul 40
Mar 30 60 Mar 30 60
Current Current
Row Apr 5 65 Row Apr 5 45
Jun 70 Jun 70
Jul 40 Jul 40
Jan 20 20 Jan 20 20
Feb 10 30 Feb 10 30
Mar 30 60 Mar 30 60
Apr 5 65 Apr 5 45
Current Current
Row Jun 70 135 Row Jun 70 105
Jul 40 Jul 40
Jan 20 20 Jan 20 20
Feb 10 30 Feb 10 30
Mar 30 60 Mar 30 60
Apr 5 65 Apr 5 45
Jun 70 135 Jun 70 105
Current Current
Row Jul 40 175
Row
Jul 40 115
Month Sales
Jan 20
Highest Lowest Average
Feb 10 Total Value Value
Current Row Mar 30 175 70 5 29
SUM MAX MIN AVG
Apr 5
Jun 70
Jul 40
ROW_NUMBER()
RANK()
DENSE_RANK() Empty
Rank Optional Required Not allowed
Functions
CUME_DIST()
PERCENT_RANK()
NTILE(n) Number
ROW_NUMBER() Assign a unique number to each in a window ROW_NUMBER() OVER (ORDER BY Sales)
RANK() Assign a rank to each row in a window, with gaps RANK() OVER (ORDER BY Sales)
DENSE_RANK() Assign a rank to each row in a window, without gaps DENSE_RANK() OVER (ORDER BY Sales)
Rank
Functions calculates the cumulative distribution of a value within
CUME_DIST() a set of values
CUME_DIST() OVER (ORDER BY Sales)
PERCENT_RANK() Returns the percentile ranking number of a row. PERCENT_RANK() OVER (ORDER BY Sales)
Sales Rank
100 1
80 2 Row_Number() assigns a
We have
a tie here! 80 3
unique rank to each of row
50 4
20 5
Sales Rank
100 1
Assign a rank to each row with in a window, but does not leave gaps in the ranking
Sales Rank
100 1
80 2 DENSE_RANK () assigns a
We have
a tie here! 80 2 same rank for both of them
Yes No
Leave ROW_NUMBER()
Runsgaps in query
outer ranking?
Yes No
RANK() DENSE_RANK()
Divides the rows into a specified number of approximately equal groups (buckets)
Number of Buckets
NTILE
Sales
(2) Number of Rows
Bucket Size =
100 1 (Nr of Rows in each Bucket) Number of Buckets
Bucket (1)
80 1
4
80 1
Bucket Size = =2
2
Bucket (2)
50 2
Divides the rows into a specified number of approximately equal groups (buckets)
Number of Buckets
NTILE
Sales
(2) Number of Rows
Bucket Size =
100 1 (Nr of Rows in each Bucket) Number of Buckets
Bucket (1) 80 1
5
80 1 Bucket Size = = 2.5
2
50 2
Bucket (2)
20 2 Larger groups come first then smaller groups
Divides the rows into a specified number of approximately equal groups (buckets)
Number of Buckets
NTILE
Sales
(3) Number of Rows
Bucket Size =
100 1 (Nr of Rows in each Bucket) Number of Buckets
Bucket (1)
80 1
5
80 1 Bucket Size = = 1.7
3
Bucket (2)
50 2
Sales Dist
Number of Rows less than or equal to X
Current Row
CUME_DIST(x) =
20 0,2 Total Number of Rows
50
60 1
CUME_DIST(20) = = 0,2
5
80
100
Sales Dist
Number of Rows less than or equal to X
CUME_DIST(x) =
20 0,2 Total Number of Rows
50 0,4
100
Sales Dist
Number of Rows less than or equal to X
CUME_DIST(x) =
20 0,2 Total Number of Rows
50 0,4
60 0,6 5
CUME_DIST(100) = =1
5
80 0,8
Current Row 100 1 It returns values greater than 0 and less and equal to 1
60 3 0
PERCENT_RANK(20) = =0
4
80 4
100 5
100 5
60 3 0,5 4
PERCENT_RANK(100) = =1
4
80 4 0,75
50 2 0,25
60 3 0,5
LEAD(expr,offset,default) Returns the value from a previews row LEAD(Sales,2,0) OVER (ORDER BY OrderDate)
Value LAG(expr,offset,default) Returns the value from a subsequent row LAG(Sales,2,0) OVER (ORDER BY OrderDate)
(Analytics)
Functions FIRST_VALUE(expr) Returns the first value in a window FIRST_VALUE(Sales) OVER (ORDER BY OrderDate)
LAST_VALUE(expr) Returns the last value in a window FIRST_VALUE(Sales) OVER (ORDER BY OrderDate)
LEAD(expr,offset,default)
Not allowed
Value LAG(expr,offset,default)
(Analytics) All Data Type Optional Required
Functions FIRST_VALUE(expr) Optional
Should be
LAST_VALUE(expr) used
Jan 100
Feb 50
Mar 75
Jun 150
Jul 110
Aug 180
Return a specific value in a window to be compared with the value of current row
Feb 50
Apr 90
Current Row Jun 150
Jul 110
Sep 200
Partition By Order By
Is Optional Is Required
Feb 10 30 Feb 10 20
Current Current
Row Mar 30 5 Mar 30 10
Row
Apr 5 Apr 5
Feb 10 30 Feb 10 20
Mar 30 5 Mar 30 10
Current Current
Row Apr 5 NULL Apr 5 30
Row
Main Query
Subquery
SELECT
Column1,
FROM table1
Subquery
Rules
Only Scalar Subqueries are allowed to be used
FROM table1
Rules Subquery
Only Scalar Subqueries are allowed to be used
> Greater than WHERE Sales > (SELECT AVG(Sales) FROM ORDERS)
< Less than WHERE Sales < (SELECT AVG(Sales) FROM ORDERS)
>= Greater than or equal to WHERE Sales >= (SELECT AVG(Sales) FROM ORDERS)
<= Less than or equal to WHERE Sales <= (SELECT AVG(Sales) FROM ORDERS)
Subquery in
WHERE Clause
Comparison Operators
Subquery in
WHERE Clause
In Operator
Main Query
FROM table1
Subquery
NOT IN Checks if a value does not matches any value in a list WHERE Sales NOT IN (SELECT …)
NOT EXISTS Checks if subquery returns no rows WHERE NOT EXISTS (SELECT …)
ANY Returns true if a value matches any value in a list. WHERE Sales < ANY (SELECT …)
ALL Returns true if a value matches all values in a list. WHERE Sales > ALL (SELECT …)
Subquery in
WHERE Clause
In Operator
Subquery in
WHERE Clause
ALL Operators
Main Query
FROM table1
WHERE column < ALL( SELECT column FROM table1 WHERE condition )
Subquery
Main Query
FROM table1
WHERE column < ANY( SELECT column FROM table1 WHERE condition )
Subquery
Definition Subquery is independent of the main query Subquery is dependent of the main query
Executed once and its result is used by the main query Executed for each row processed by the main query
Execution
Can be executed on its Own Can't be executed on its Own.
Performance Executed only once leads to better Performance Executed multiple times leads to bad Performance
Usage Static Comparisons, Filtering with Constants Row-by-Row Comparisons, Dynamic Filtering
FROM Table2
FROM Table1
Subquery
Run outer
Runs Subquery
query
SELECT *
SELECT o.*
FROM Orders
FROM Orders o
WHERE CustomerID IN
Syntax JOIN Customers c
(SELECT CustomerID
ON [Link] = [Link]
FROM Customers
AND [Link] = 'USA'
WHERE Country = 'USA')
Readability Not easy to read & maintain easy to read & maintain
Best
Practices
Useful with Larg tables Useful with small tables
WITH CTE-Name AS
(
SELECT … CTE Query
FROM … - CTE Definition -
WHERE …
)
SELECT …
FROM CTE-Name Main Query
- CTE Usage -
WHERE …
WITH CTE-Name1 AS
(
CTE Query
- CTE Definition - SELECT …
FROM …
WHERE …
)
, CTE-Name2 AS
(
CTE Query
SELECT …
- CTE Definition - FROM …
WHERE …
)
SELECT …
Main Query FROM CTE-Name1
- CTE Usage - JOIN CTE-Name2
WHERE …
SQL Course | Multiple CTE
SQL Course | Nested CTE
Nested CTEs
WITH CTE-Name1 AS
(
SELECT … CTE Query
Standalone CTE - CTE Definition -
FROM …
WHERE …
)
, CTE-Name2 AS
(
NESTED CTE SELECT … CTE Query
- CTE Definition -
FROM CTE-Name1
WHERE …
)
SELECT …
Main Query
FROM CTE-Name2 - CTE Usage -
WHERE …
WITH CTE-Name AS
(
SELECT …
Anchor
FROM …
Query
WHERE …
CTE Query
- CTE Definition - UNION ALL
SELECT …
Recursive
FROM CTE-Name Query
WHERE [Break Condition]
)
SELECT …
Main Query FROM CTE-Name
- CTE Usage -
WHERE …
DDL
Statement
CREATE VIEW VIEW-NAME AS
(
SELECT …
FROM …
Query WHERE …
)
DDL
Statement
CREATE TABLE NAME AS
SELECT …
(
SELECT … INTO New-Table
FROM …
Query FROM …
WHERE …
WHERE …
)
MySQL | Postgres | Oracle Sql Server
SELECT …
INTO # New-Table
FROM …
WHERE …
Sql Server
END
Stored Procedure
Execution (Call) EXEC ProcedureName
BEGIN TRY
Execute
-- SQL statements that might cause an error
TRY
END TRY
Error
Error?
BEGIN CATCH
-- SQL statements To Handle The Error Execute
No CATCH
END CATCH Error
End
Start
Value is Yes
NULL?
Update
No To Zero
End