Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
33 views9 pages

Difference

Uploaded by

mithu241210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views9 pages

Difference

Uploaded by

mithu241210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Difference

What is the difference between clustered and non-clustered index?


A clustered index is an index that rearranges the table in the order of the index itself. Its leaf nodes contain
data pages. A table can have only one clustered index.
A non-clustered index is an index that does not re-arrange the table in the order of the index itself. Its leaf
nodes contain index rows instead of data pages. A table can have many non-clustered indexes.

What’s the difference between a primary key and a unique key?


The primary key is a column whose values uniquely identify every row in a table. Primary key values can
never be reused. They create a clustered index on the column and cannot be null.
A Unique key is a column whose values also uniquely identify every row in a table but they create a non-
clustered index by default and it allows one NULL only.

What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?


Both specify a search condition but the HAVING clause is used only with the SELECT statement and typically
used with GROUP BY clause.
If the GROUP BY clause is not used, then the HAVING clause behaves like a WHERE clause only

What is the difference between a Local and a Global temporary table?


Local Temporary Tables Global Temporary Tables
Local temporary tables are only visible to that Global temporary tables are visible to all SQL Server
session of SQL Server that has created it. sessions.
Local temporary tables are automatically Global temporary tables are dropped when the last
dropped when the session of the SQL Server session of SQL Server referencing to the global
that has created it is closed. temporary tables is closed.
Local temporary tables are prefixed with a Global temporary tables are prefixed with double
single pound ‘#’ symbol. pounds ‘##’ symbol.
SQL Server appends some random numbers at SQL Server doesn’t append any random numbers at
the end of the local temporary table name. the end of the global temporary table name.

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 is the difference between DELETE and TRUNCATE?


The basic difference in both is DELETE command is DML command and the TRUNCATE command is DDL.
DELETE command is used to delete a specific row from the table whereas the TRUNCATE command is used
to remove all rows from the table.
We can use the DELETE command with WHERE clause but cannot use the TRUNCATE command with it.

What is the difference between DROP and TRUNCATE?


TRUNCATE removes all rows from the table which cannot be retrieved back, DROP removes the entire
table from the database and it also cannot be retrieved back.

What is the difference between Nested Subquery and Correlated Subquery?


Subquery within another subquery is called Nested Subquery. If the output of a subquery depends on
column values of the parent query table then the query is called Correlated Subquery.
What is the difference between UNION and UNION ALL?
UNION: To select related information from two tables UNION command is used. It is similar to JOIN
command.
UNION All: The UNION ALL command is equal to the UNION command, except that UNION ALL selects all
values. It will not remove duplicate rows, instead it will retrieve all rows from all tables.

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

What is the difference between COMMIT and ROLLBACK?


Every statement between BEGIN and COMMIT becomes persistent to database when the COMMIT is
executed. Every statement between BEGIN and ROOLBACK are reverted to the state when the ROLLBACK
was executed.

Which operation is faster COMMIT or ROLLBACK? Why?


It’s obviously COMMIT is faster than ROLLBACK. Let me explain with an example: Let’s say we opened a
transaction and updated 8000 records:
Commit: It’s completed quickly as the operation is already completed and it just marks those dirty pages as
committed and when checkpoint happens all those dirty pages will be written to disk.
Rollback: The operation is already updated 8000 records if we need to rollback then again all
these updates has to be rolled back which means there are another 8000 log records will be written to LDF
which will take time when compared to commit.

What is the difference between varchar and nvarchar types?


Varchar and nvarchar are same but the only difference is that nvarhcar can be used to store Unicode
characters for multiple languages and it also takes more space when compared with varchar.

What is the difference between GETDATE and SYSDATETIME?


Both are same but GETDATE can give time till milliseconds and SYSDATETIME can give precision till
nanoseconds. SYSDATE TIME is more accurate than GETDATE

What is the difference between a UNION and a JOIN?


A JOIN selects columns from 2 or more tables. A UNION selects rows.

What is the difference between function and stored procedure?


A Function must return a value while stored procedure can return zero or n value.
Functions can have only input parameter while procedures can have input/ output parameters.
Functions take one mandatory input parameter while stored procedures may take 0 to n input parameter.
Try-catch block can handle exceptions. In the stored procedure, while you can't use try-catch in functions.
What is the difference between checkpoint and lazy writer?
Both the checkpoint and lazy writer are two events but they are totally different. both of these
mechanisms only push dirty pages to disk.

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.

What is the difference between deadlock and blocking?


Deadlocking occurs when two queues simultaneously request exclusive access to two resources. It occurs
when one process is blocked and waiting for a second process to complete its work and release locks, while
the second process at the same time is blocked and waiting for the first process to release the lock. In
deadlock two or more transaction have a resource locked means in other words you can say when two or
more than two process have a locked resources then deadlock occurs.

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.

What is the difference between row store and column store?


Row stores have the ability to write data very quickly, whereas a column store is awesome at aggregating
large volumes of data for a subset of columns. ... This makes columnar databases a good choice in a query-
heavy environment. But you must make sure that the queries you run are really suited to a columnar
database.

What is the difference between DMV and DMF in SQL Server?


These both terms (DMV and DMF) returns server state information and these can be used for the
monitoring purpose of the health state of a server instance also can be used in diagnosis of the problems
and performance tuning.

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 Hot and Cold Backup?


Performing backup while the database is online is called Hot backup. Stopping SQL server service and
copying MDF and LDF files is called cold backup which is not really happens in production.

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.

Difference between Mirroring and Replication:


The prior difference between mirroring and replication is that mirroring refers to copy a database to
another location whereas replication includes the copy of data and database objects from one database to
another database.

Difference between Mirroring and Alwayson:


Database mirroring for disaster recovery (asynchronous) or for high availability(synchronous). AlwaysOn,
allows up to two synchronous replicas and two asynchronous replicas to be simultaneously active.

Difference between Mirroring and Clustering:


Clustering means more than one database server configured for the same user connection. When users
connect, one of the server's responds and connects based on availability. ... Mirroring means, one has
many configured databases on the same server.

What is difference between logical join and physical join?


Logical Joins: These joins are simple joins that we apply in our SQL queries, like INNER JOIN, RIGHT/LEFT
OUTER JOIN, CROSS JOIN, OUTER APPLY, etc. 2. ... Instead these are implemented inside SQL Server engine
as operators or algorithms to implement the Logical Joins. Their types are Nested Loop, Merge and Hash.

What Is The Difference Between Lock, Block And Deadlock?


Lock: DB engine locks the rows/page/table to access the data which is worked upon according to the
query.
Block: When one process blocks the resources of another process then blocking happens. Blocking can be
identified by using
SELECT * FROM sys.dm_exec_requests where blocked <> 0
SELECT * FROM master..sysprocesses where blocked <> 0
Deadlock: When something happens as follows: Error 1205 is reported by SQL Server for deadlock.

Difference between RTO, RPO and RLO?


Recovery point objective (RPO) is used to determine the maximum amount of time between the Last
available backup and potential failure point. Recovery point objective (RPO) helps in determining the
amount of data that the business can manage to Lose in the event of a failure.
Recovery time objective (RTO) is used to determine the maximum time a data recovery process will take.
It is defined by the amount of time the business can afford for the site or service to be unavailable.
Recovery Level objective (RLO) is the objective that defines the granularity with which you must be able to
recover data — whether you must be able to recover the whole farm, Web application, site collection, site,
list or library, or item.

What is the difference between Active-Passive and Active-Active clustering setup?


An Active-Passive cluster may be a failover cluster designed in an exceeding manner that just one cluster
node is active at any given time. The other node, called the Passive node is always online but in an idle
condition, waiting for a failure of the Active Node, upon which the Passive Node takes over the SQL Server
Services and this becomes the Active Node, the previous Active Node now being a Passive Node.

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.

What is the difference between the 2 operating modes of Database Mirroring?


High-Safety Mode is to make sure that the Principal and reflected info area unit synchronal state, that is
the transactions are committed at the same time on both servers to ensure consistency, but there is/might
be a time lag.

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 a RID Lookup and a Key Lookup?


A RID Lookup operation is performed to retrieve the rest of columns that are not available in the index
from the heap table based on the ID of each row.
A Key Lookup operation is performed to retrieve the rest of columns that are not available in the index
from the Clustered index, based on the Clustered key of each row,

What is the difference between PAD_INDEX and FILLFACTOR?


FILLFACTOR isused to set the percentage of free space that the SQL Server Engine will leave in the leaf level
of each index page during index creation. The FillFactor should be an integer value from 0 to 100, with 0 or
100 is the default value, in which the pages will be filled completely during the index creation.
PAD_INDEX is used to apply the free space percentage specified by FillFactor to the index intermediate
level pages during index creation.

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.

Difference between CTE and Temp Table and Table Variable


CTEs...
 Are unindexable (but can use existing indexes on referenced objects)
 Cannot have constraints
 Are essentially disposable VIEWs
 Persist only until the next query is run
 Can be recursive
 Do not have dedicated stats (rely on stats on the underlying objects)
#Temp Tables...
 Are real materialized tables that exist in tempdb
 Can be indexed
 Can have constraints
 Persist for the life of the current CONNECTION
 Can be referenced by other queries or subprocedures
 Have dedicated stats generated by the engine
 As far as when to use each, they have very different use cases. If you will have a very large result
set, or need to refer to it more than once, put it in a #temp table. If it needs to be recursive, is
disposable, or is just to simplify something logically, a CTE is preferred.
Also, a CTE should never be used for performance. You will almost never speed things up by using a CTE,
because, again, it's just a disposable view. You can do some neat things with them but speeding up a query
isn't really one of them.

What is the difference between select count(*) and count(1) in sql server?
There is no difference.

Select Count(*) from Table_Name


Select Count(1) from Table_Name

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).

Same IO, same plan, the works

State the differences between views and tables.

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

Difference between Session and Connection in SQL Server?


Sessions – when the client application connects to SQL Server the two sides establish a “session” on which
to exchange information. Strictly speaking a session is not the same as the underlying physical connection,
it is a SQL Server logical representation of a connection. But for practical purposes, you can think of this as
being a connection (session =~ connection). See sys.dm_exec_sessions. This is the old SPID that existed in
SQL Server 2000 and earlier. You may sometimes notice a single session repeating multiple times in a DMV
output. This happens because of parallel queries. A parallel query uses the same session to communicate
with the client, but on the SQL Server side multiple worker (threads) are assigned to service this request.
So if you see multiple rows with the same session ID, know that the query request is being serviced by
multiple threads.

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.

What is the difference between DBCC INDEXDEFRAG and DBCC REINDEX?


DBCC REINDEX drops the index and creates the index again. DBCC INDEXDEFRAG is an online operation, it
does not hold long-term locks that can block running queries or updates. With INDEXDEFRAG the index is
always available, unlike DBREINDEX.

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.

What is the Difference between VARCHAR and VARCHAR(MAX) Datatypes?

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 the 2 operating modes of Database Mirroring.


1. High-Safety Mode is to ensure that the Principal and Mirrored database are synchronized state, that is the
transactions are committed at the same time on both servers to ensure consistency, but there is/might be a
time lag.
2. High-Performance Mode is to ensure that the Principal database runs faster, by not waiting for the Mirrored
database to commit the transactions. There is a slight chance of data loss and also the Mirrored database
can be lagging behind (in terms of being up to date with the Principal database) if there is a heavy load on
the Mirrored Server.

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.

You might also like