Rank, Dense - Rank & Row - Number
Rank, Dense - Rank & Row - Number
Rank, Dense - Rank & Row - Number
Select Name, Salary, Gender, Row_Number() Over (Order by salary desc) as RowNumber,
From employees.
With CTE As (
Select Name,Gender, salary row_number() Over (Partition by Name, gender, salary order by Name,
gender, salary ) row_num From Employee
Union: combine two or more result set into a single set, without duplicate.
Union All: combine two or more result set into a single set, with duplicate.
Index
Table scan: SQL server search data sequentially one by one from the start to the last record of
the table.
Index Scan/ Seek in SQL server: SQL server search the data by using B-Tree structure.
Clustered Index: Clustered index defines the order in which data is physically stored in the table.
Non Clustered Index: Data is stored in one place and the index is stored in another place, and the index
will have the pointer of actual data.
Session is current database connection. The scope is the current query or current stored procedure.
View in SQL
Limitations:
We can’t use order by clause with views without specifying For XML, OFFSET, and TOP.
Trigger:
It is a special kind of stored procedure that automatically get executed before or after Insert, Update,
and Delete in a table.
1. Instead of trigger: If you want to add some logic instead of DML operation.
2. After Trigger: If you want to add some logic after DML operation.
E.g.
DDL Trigger:
On Database
For Alter_Table
As
Begin
Rollback transaction
End
CTE
Common table expression is a temporary result set that reference with in select, Insert, Update, and
Delete.
With CTEExample as
Join Union
Combines the data based on matched Combines the result-set of two or more select
condition. statements.
It combines the data into new columns. It combines the data in a new row.
Number of column selected for each table can Number of columns selected for each table should be
be different. same.
Data type of selected columns can be different. Data type of selected columns should be same.
Table variable (@table) is created in the memory. Whereas, a Temporary table (#temp) is created in
the tempdb database. However, if there is a memory pressure the pages belonging to a table
variable may be pushed to tempdb.
Table variables cannot be involved in transactions, logging or locking. This makes @table faster then
#temp. So table variable is faster then temporary table.
Temporary tables are allowed CREATE INDEXes whereas, Table variables aren’t allowed CREATE
INDEX instead they can have index by using Primary Key or Unique Constraint.
Table variable can be passed as a parameter to functions and stored procedures while the same
cannot be done with Temporary tables.
Temporary tables are visible in the created routine and also in the child routines. Whereas, Table
variables are only visible in the created routine.
Temporary table allows Schema modifications unlike Table variables.
Temporary Tables are physically created in the tempdb database. These tables act as the normal
table and also can have constraints, index like normal tables.
Table Variable acts like a variable and exists for a particular batch of query execution. It gets
dropped once it comes out of batch. It is created in the memory database but may be pushed out to
tempdb.
Use Table variable, if you have less than 1000 rows otherwise go for Temporary tables.