Difference
Difference
If defined inside a compound statement a local temporary table exists only for the duration of that
statement but a global temporary table exists permanently in the database but its rows disappear when
the connection is closed.
What are the differences between Stored Procedure and the dynamic SQL?
Stored Procedure is a set of statements which is stored in a compiled form. Dynamic SQL is a set of
statements that dynamically constructed at runtime and it will not be stored in a Database and it simply
execute during run time.
What is the difference between SUBSTR and CHARINDEX in the SQL Server?
The SUBSTR function is used to return specific portion of string in a given string. But, CHARINDEX function
gives character position in a given specified string.
SUBSTRING('Smiley',1,3)
Gives result as Smi
CHARINDEX('i', 'Smiley',1) Gives 3 as result as I appears in 3rd position of the string
A checkpoint is responsible for pushing dirty pages in the buffer pool out to disk at a specified interval.
Normally a dirty page is simply a page that SQL SERVER has changed in memory, but has not yet been
written to disk. The pages need to be written to disk regularly in order to ensure that when SQL SERVER
restarts, that crash recovery will not take so long. It can be used manually.
On the other hand the lazy writer will push dirty pages to disk for an entirely different reason because it
needs to free up memory in the buffer pool. This happens when sql server comes under memory pressure.
As far as I am aware this is controlled by an internal process and there is no setting for it.
Blocking occurs when a query has an exclusive lock on a row or a resource such as an entire table or an
index and some other query needs exclusive access also. Blocking is a natural process in SQL. When 2
different processes try to access or modify the same data, each process locks the data it is accessing and
when the work of the process is over the lock is released.
The DMV and DMFs are two different entity but both are stored in query cache or in query plans
sometimes these are also stored in CLR Data, Extended Events, Resource Governor info, etc. ‘s are views,
DMF’s are functions. The terms Dynamic Management Views (DMVs) and Dynamic Management Functions
(DMFs) were introduced in SQL 2005.
What is the difference between STUFF and REPLACE functions in Sql server ?
The Stuff function is used to replace characters in a string. This function can be used to delete a certain
length of the string and replace it with a new string.
Syntax – STUFF (string_expression, start, length, replacement_characters)
Ex – SELECT STUFF(‘I am a bad boy’,8,3,’good’)
Output – “I am a good boy”
REPLACE function replaces all occurrences of the second given string expression in the first string
expression with a third expression.
Syntax – REPLACE (String, StringToReplace, StringTobeReplaced)
Ex – REPLACE(“Roopesh”,”pe”,”ep”)
Output – “Rooepsh” – You can see PE is replaced with EP in the output.
What is the Difference between the functions COUNT and COUNT_BIG ?
Both Count and Count_Big functions are used to count the number of rows in a table and the only
difference is what it returns. Count returns INT datatype value where as Count_Big returns BIGINT
datatype value. Count is used if the rows in a table are less where as Count_Big will be used when the
numbenr of records are in millions or above.
Count – Select count(*) from tablename
Count_Big – Select Count_Big(*) from tablename
What is the difference between dropping a database and taking a database offline?
Drop database deletes the database along with the physical files; it is not possible to bring back the
database unless you have a backup of the database. When you take a database offline, the database is not
available for users, it is not deleted physically, it can be brought back online.
An Active-Active cluster is a failover cluster configured in a way that both the cluster nodes are active at
any given point in time. That is, one Instance of SQL Server is running on every of the nodes always; once
one amongst the nodes encompasses a failure, both the Instances run on the only one node till the
unsuccessful node is remarked (after fixing the difficulty that caused the node failure). The instance is then
unsuccessful over back to its selected node.
High-Performance Mode is to ensure that the Principal database run faster, by not waiting for the
Mirrored database to commit the transactions. There is a small likelihood of knowledge loss and conjointly
the reflected info may be insulating material behind (in terms being up so far with the Principal database) .
What are the differences between Stored Procedure and the dynamic SQL?
Stored Procedure is a set of statements which is stored in a compiled form. Dynamic SQL is a set of
statements that dynamically constructed at runtime and it will not be stored in a Database and it simply
execute during run time.
What is the difference between index Rebuild and Index Reorganize operations?
Index fragmentation can be resolved by rebuilding and reorganizing SQL Server indexes regularly.
The Index Rebuild operation removes fragmentation by dropping the index and creating it again,
defragmenting all index levels, compacting the index pages using the Fill Factor values specified in rebuild
command, or using the existing value if not specified and updating the index statistics using FULLSCAN of
all the data.
The Index Reorganize operation physically reorders leaf level pages of the index to match the logical order
of the leaf nodes. The index reorganizes operation will be always performed online. Microsoft recommends
fixing index fragmentation issues by rebuilding the index if the fragmentation percentage of the
index exceeds 30%, where it recommends fixing the index fragmentation issue by reorganizing the index if
the index fragmentation percentage exceeds 5% and less than 30%.
What is the difference between a Heap table and a Clustered table? How can we identify if the table is a
heap table?
A Heap table is a table in which, the data rows are not stored in any particular order within each data page.
In addition, there is no particular order to control the data page sequence, that is not linked in a linked list.
This is due to the fact that the heap table contains no clustered index.
The heap table can be identified by querying the sys.partitions system object that has one row per each
partition with index_id value equal to 0. You can also query the sys.indexes system object also to show the
heap table index details, that shows, the id of that index is 0 and the type of it is HEAP.
A clustered table is a table that has a predefined clustered index on one column or multiple columns of the
table that defines the storing order of the rows within the data pages and the order of the pages within the
table, based on the clustered index key.
What is the main difference between a Non-clustered index that is built over a Heap table and a Non-
clustered index that is built over a Clustered table?
If a Non-Clustered index is built over a Heap table or view (read more about SQL Server indexed views,
that have no Clustered indexes) the leaf level nodes of that index hold the index key values and Row ID
(RID) pointers to the location of the rows in the heap table. The RID consists of the file identifier, the data
page number, and the number of rows on that data page.
On the other hand, if a Non-clustered index is created over a Clustered table, the leaf level nodes of that
index contain Non-clustered index key values and clustering keys for the base table, that are the locations
of the rows in the Clustered index data pages.
What is the difference between select count(*) and count(1) in sql server?
There is no difference.
It is very common perception that the Count(1) perform better compared to Count(*), however it is not the
case. If you test by looking at the execution plan, you will see same action being performed by both the
commands and same number of rows being scanned. The time taken may be slightly different in terms of
CPU usage for count(*) , but is almost same as count(1).
Views Tables
It is a virtual table that is extracted from a A table is structured with a set number of columns and a
database. boundless number of rows.
Views do not hold data themselves. A table contains data and stores the data in databases.
A view is also utilized to query certain A table holds fundamental client information and the
information contained in a few distinct cases of a characterized object.
tables.
In a view, we will get frequently queried In a table, changing the information in the database
information. changes the information that appears in the view
Connections – this is the actual physical connection established at the lower protocol level with all of its
characteristics sys.dm_exec_connections . There is a 1:1 mapping between a Session and a Connection.
DBCC INDEXDEFRAG can be considerably faster than running DBCC DBREINDEX on a relatively
unfragmented index, but a large amount of fragmentation can cause INDEXDEFRAG to run considerably
longer than DBREINDEX, which may or may not outweigh the benefit of its online capabilities.
To improve the clustering of pages, rebuild the index.
DBCC DBREINDEX rebuilds an index for a table or all indexes defined for a table. It can rebuild all of the
indexes for a table in one statement.
DBCC DBREINDEX is not supported for use on system tables.
VARCHAR VARCHAR(MAX)
Bytes It consumes 1 byte per character. It consumes 1 byte per character.
Data Length The maximum number of characters for this The maximum number of characters for this data type can
data type can hold up to 8000 characters by hold unlimited by defining as NVARCHAR(MAX) MAX
defining as indicates that the maximum storage size is (2147483647)
VARCHAR(8000). 2^31-1 bytes (2 GB). The storage size is the actual length of
the data entered + 2 bytes.
Data Used to store variable length value as String. Used to store variable length value as String.
Storage
Type
Index Index can be created on VARCHAR(N) data Index can’t be created on VARCHAR(MAX) data type even
type if needed. though it is needed. It throws the exception error when we
create an index on this data type.
Storage SQL server will use the normal data pages to SQL server will use the normal data pages to store the value
store the data i.e. it stores the value in a in a row but if it could not then it will store the value out of
row. row. i.e. It uses the normal data pages until the content
actually fills 8k of data. When overflow happens on it, data
Content is dynamic (Variable Length Size is stored as old TEXT Data type and a pointer is replacing the
Variable) and null, manage storage space old content.
efficiently with VARCHAR.
Does not allow UNICODE characters and content is variable
size for storing.
RAISERROR vs THROW
RAISERROR generates an error message and initiates error processing for the session.
RAISERROR can either reference a user-defined message stored in the SYS.messages catalog view or build a
message dynamically.
The message is returned as a server error message to the calling application or to an associated CATCH block
of a TRY…CATCH construct.
RAISERROR introduced in SQL Server 7.0. It supports up to SQL Server 2012.
RAISERROR can’t be used in the SQL Server 2014’s Natively compiled Stored Procedures.
THROW raises an exception and transfers execution to a CATCH block of a TRY…CATCH construct in SQL
Server 2012 and above versions.
THROW introduced in SQL Server version 2012.
THROW can be used in the SQL Server 2014 and above versions.
Microsoft suggests using THROW instead of using RAISERROR
What is difference between table alias and column aliases do they affect performance.
Usually, when the name of the table or column is very long or complicated to write, aliases are used to refer them. ...
In the above example, col1 and tab1 are the column alias and table alias, respectively. They do not affect the
performance at all.
A programmer can use an alias to temporarily assign another name to a table or column for the duration of a SELECT
query. Column alias is also called SQL aliases. They used to give a table ,or a column in a table and a temporary
name, they are often used to make column names more readable.
How do you know when to use a table alias when not using a table alias?
If a table appears more than once in the FROM clause, you need table aliases in order to keep them distinct. Self
joins are common in cases where a table contains a foreign key that references the primary key of the same table
What is the difference between slow running and long running queries?
1. If your Query is expected to touch a lot of data and is expected to run for an extended duration - it’s a long
running query.
2. If you query is expected to completed quickly (due to the volume of data being touched of the nature of the
query itself), but its running longer than expected - then it’s a slow running query.
3. The Key differentiation is "expected" run time versus "actual" run time. It’s also important to set realistic
expectations on the "expected" run time.
4. This is a very generic/high level explanation. A slow running query will ultimately change into long running
one due to blocking which it might face as it is unable to perform specified time within the threshold.