–Section– | —SQL Queries– |
Part 1: Basic Selection Queries | Select, Order By, Distinct, Limit, Count |
Part 2: Aggregate Functions SQL | Sum, Avg, Min, Max, Group By, Having |
Part 3: String Functions SQL | Upper, Lower, Left, Right, Length, Position, Substring, Concat |
Part 4: Conditional Queries | Case, Coalesce, Cast, Replace |
Part 5: Combining Data Queries | Union, Union All, Except, Intersect, Date |
Part 6: Window Functions | Over, Partition BY, Order By |
Part 7: Ranking Functions | Rank, Dense Rank, Row Number, NTile |
Part 8: Insert Queries | Insert data using fix data, select query, Stored Proc, CTE |
Part 9: Update Queries | Update data using from, joins, subquery, CTE, conditions |
Part 10: Common Table Expression | Single CTE, Multiple CTE, Recursive CTE, Merge |
Part 11: User Defined Functions | Scalar Functions, Drop, List, Create, Call UDFs |
Part 12: Table Functions | Inline and Multi Statement Table Functions, Cross Apply, Outer Apply |
Part 13: SQL SubQuery | SubQuery in where, from, select and Correlated Subquery |
Part 14: SQL Joins | Inner Join, Left Join, Right Join, Cross Join, Self Join, Multiple Joins |
Part 15: SQL Wildcards | Wild Card Filters with Where Clause |
Part 16: SQL Range Operators | Not, In, Like, Between, And, Or |
Part 17: SQL Windows and Range | Rows, Range, Window Definition, Modification, Referencing, Select Into |
[SQL Stored Procedures] | SQL Stored Procedures Cheat Sheet |
[SQL with Java JDBC] | Java JDBC for SQL and Stored Procedures |
Download all SQL Queries Free!

Part 1: Basic Selection Queries
Select | Order By | Distinct | Limit | Count
Part 2: Aggregate Functions SQL –
Sum | Avg | Min | Max | Group By | Having
Part 3: String Functions SQL –
Upper | Lower | Left | Right | Length | Position | Substring | Concat
Part 4 Conditional Queries –
Case | Coalesce | Cast | Replace
Part 5: Combining Data Queries –
Union | Union All | Except | Intersect | Date
Part 6: Window Functions –
Over | Partition BY | Order By
Part 7: Ranking Functions – Rank, Dense Rank, Row Number, NTile
Part 8: Insert Query – Insert data using fix data, select query, Stored Proc, CTE
Clause | Type | Query | Meaning | Output |
---|---|---|---|---|
Insert | with value | insert into accounts (id, account_name) values (10,’tony-stark’) | insert a record – with fixed hardcoded values | |
Insert | with multiple values | insert into accounts (id, account_name) values (10,’tony-stark’),(11,’peter-parker’) | insert multiple records – with different fixed values | |
Insert | with select | insert into accounts (id, account_name) select account_id, name from third_party_accounts join…. | insert multiple/bulk records – by using data from another table – by forming a result with joins on different tables | |
Insert | with procedure | insert into accounts (id, account_name) execute dbo.getEmployeeAccounts | insert multiple/bulk records – by using data returned from a stored procedure | |
Insert | with cte | with cte as ( select account_id,name from …join…) insert into accounts select account_id,name from cte | insert multiple/bulk records – by using data from a cte using with clause – cte can be created with multiple tables, joins |
Part 9: Update Query – Update data using from, joins, subquery, CTE, conditions
Part 10: Common Table Expression – Single CTE, Multiple CTE, Recursive CTE, Merge
Part 11: User Defined Functions – UDF, Scalar Functions, Drop, List, Create, Call UDFs
Clause | Type | Query | Meaning | Output |
---|---|---|---|---|
function | scalar | create function addfunc (@param1 int, @param2 int) returns int as begin return @param1 + @param2 end | function benefits – modular code .. can be used in select – can be used in where clause to allow complex conditions – can be faster than join and group by scalar function can return only one value back | |
function | scalar function call | select jan_sale, feb_sale, dbo.addfunc(jan_sale, feb_sale) as jf_sale from sales; declare @jf_sale int @jf_sale = addfunc 120 200 select @jf_sale | functions can be used in select statements and execute command
stored procedure can only be used in execute command | jan_sale | feb_sale | jf_sale 120 . . .| 200 . . .| 320 |
function | scalar | create function tranCount (@customer_id int) returns int as begin declare @tran_count int select @tran_count = count(*) from transactions where customer_id = @customer_id return @tran_count end | ||
function | scalar function call | select customer_id, tranCount(customer_id) as tran_count from transactions; | customer_id | tran_count 201 . . . . | 12 202 . . . . | 18 | |
function | drop | drop function tranList; | ||
function | list | select * from sys.objects where name = ‘tranCount’ | lists all database objects including functions | name . . . | type | type_desc tranCount .| FN . | SQL_SCALAR_FUNCTION |
function | drop + create | if exists (select * from sys.objects where name = ‘tranCount’) drop function tranCount go create function tranCount (….) returns int as begin …. return …. end |
3 comments on SQL Queries – All in One