SQL Difference
SQL Difference
SQL Difference
in
1. What are the Differences between TRUNCATE and Delete?
S.No Truncate Delete
2 Removes all rows from a table Can remove specific rows with
Where clause
4 Resets identity of the table Does not reset identity of the table
2. What are the differences between Primary key and Unique key?
S.No Primary Key Unique Key
2 Null values are not allowed. Allows only one null value.
3 We can have only one Primary We can have more than one unique
key in a table. key in a table.
4 Primary key can be made foreign Unique key cannot be made foreign
key into another table. key into another table.
3 The leaf nodes contain data The leaf node contains pointer to data
4 To create clustered index Sql To create non-clustered index Sql
server required more memory server requires less memory because
because the leaf pages in the the leaf pages will contain pointers to
tree structure will maintain actual actual data
data .
4. What are the differences between Stored Procedures and User Defined
Functions?
S.No Stored Procedures User Defined Functions
4 Stored Procedure need not return User Defined Functions should return
a value a value
5 Stored Procedures can return any User Defined Functions cannot return
datatype Image
Example : Example :
SELECT Col SELECT Col
FROM @Table1 FROM @Table1
UNION UNION ALL
SELECT Col SELECT Col
FROM @Table2 FROM @Table2
Result: Result:
1 1
2 2
3 3
5 2
5
1 Select statements are used for Cursors are used for row-level
table-level processing processing
1 Primary key uniquely identify a Foreign key is a field in the table that
record in the table. is primary key in another table.
2 Primary Key cannot accept null Foreign key can accept multiple null
values. values.
4 We can have only one Primary We can have more than one foreign
key in a table. key in a table.
1. What are the differences between Instead of Triggers and After Triggers?
1 Each table or view can have one A table can have several AFTER
INSTEAD OF trigger for each triggers for each triggering action.
triggering action (UPDATE,
DELETE, and INSERT)
2 INSTEAD OF triggers fire in place AFTER triggers fire after the
of the triggering action and before triggering action (INSERT, UPDATE,
constraints are processed. or DELETE) and after any constraints
are processed.
2. What are the differences between Views and User-Defined Functions?
) MAXVALUE 1000
NO CYCLE
NO CACHE
GO
SELECT @@IDENTITY
AS ‘Identity’
–OR
Select
SCOPE_IDENTITY() AS
‘Identity’
CYCLE;
Example :
CACHE 3;
Example :
[ID] int,
GO
–First Insert With Sequence
object
INSERT INTO
dbo.test_Sequence ([ID],
[Product Name]) VALUES
(NEXT VALUE FOR
[Ticket] , ‘MICROSOFT SQL
SERVER 2008′)
GO
INSERT INTO
dbo.test_Sequence ([ID],
[Product Name]) VALUES (2
, ‘MICROSOFT SQL
SERVER 2012′)
MAXVALUE 2000;
INCREMENT BY 2;
9 We cannot generate We can generate a range of
range from identity. sequence
values from a sequence
object with the help of
sp_sequence_get_range.
http://sqljunkieshare.com/2011/11/05/difference-between-temporary-tables-and-table-
variables/
http://sqlhints.com/2013/06/30/differences-between-raiserror-and-throw-in-sql-server/
2 BULK uses COPY instead
INSERT and BCP (which has the functionality
of both BCP and BULK
INSERT)
4 Advantages: Advantages:
5 Differences: Differences:
S.No IN EXISTS
2 The sub query will run The Outer query will ran first
first and then only outer and then only sub query.
query.
4 Example: Example:
3.Difference between Change Track and Change Data Capture – CDC in SQL
Server 2008
4.Difference between Index Rebuild and Index Reorganize in SQL Server 2005
3 TRY_CONVERT() and FO TRY_CONVERT() and FO
RMAT() functions: RMAT() functions:
TRY_CONVERT() and TRY_CONVERT() and
FORMAT() functions are FORMAT() functions are
not available in SQL Server newly included in SQL
2008 Server 2012
SELECT BusinessEntityID,
FirstName, LastName
FROM Person.Person
ORDER BY
BusinessEntityID
OFFSET 10 ROWS
FETCH NEXT 10 ROWS
ONLY;
ID FullName
1 Umar Ali
2 John Peter
3 Mohamed Iqbal
Data Model
Business Logic
Data Access
Output:
Command(s) completed
successfully.
(1 row(s) affected)
5 Performance: Performance:
Set does not provide Select has better
better performance over performance over set when
select when used for used for assigning values to
assigning values to multiple variables at the
multiple variables at the same time.
same time.
Assigning values to multiple
Assigning values to variables using Select:
multiple variables using 1
SET: 2
1 3
2 4
3 5
4 DECLARE @i INT,
5 @j INT,
6 @k INT
7 SELECT @i = 10,@j =
DECLARE @i INT, 20,@k = 30
@j INT,
@k INT
SET @i = 10
SET @j = 20
SET @k = 30
Example: Example:
DECLARE @firstName DECLARE @firstName
VARCHAR =‘UMAR’ NVARCHAR =‘UMAR’
SELECT SELECT
@firstNameFirstName,DAT @firstNameFirstName,DAT
ALENGTH(@firstName) ALENGTH(@firstName)
Length Length
Result: Result:
FirstName Length FirstName Length
U1 U2
5 If Optional Parameter n is If Optional Parameter n is
not not
specified in while using specified in while using
CAST/CONVERT CAST/CONVERT
functions: functions:
Example: Example:
DECLARE @firstName DECLARE @firstName
VARCHAR(35) =‘UMAR NVARCHAR(35) =‘UMAR
ASIA INDIA TAMIL NADU ASIA INDIA TAMIL NADU
CUDDALORE’ CUDDALORE’
SELECT CAST(@firstName SELECT CAST(@firstName
AS VARCHAR) AS NVARCHAR)
FirstName,DATALENGTH( FirstName,DATALENGTH(
CAST(@firstName AS CAST(@firstName AS
VARCHAR)) Length NVARCHAR)) Length
Result: Result:
FirstName Length FirstName Length
UMAR ASIA INDIA TAMIL UMAR ASIA INDIA TAMIL
NADU CUD 30 NADU CUD 60
LEN() CHARACTER_LENGTH()
Aliases: CHAR_LENGTH(),
LENGTH()
16 Licensing: Licensing:
SQL Server is not an MySQL is available for free
open source and since MySQL is an open
payment has to be source.
made to use SQL
Server.
19 Security: Security:
SQL Server provides MySQL provides only table
column level security. level security.
Works Fails
Example: Below Example: Below statement
statement to create a to create a table with table
table with table name name “Table” Fails.
“Table” succeeds. SET
SET QUOTED_IDENTIFIER
QUOTED_IDENTIFIER OFF GO
ON GO CREATE TABLE
CREATE TABLE dbo.”Table”
dbo.”Table” (id int,”Function”
(id int,”Function” VARCHAR(20)) GO
VARCHAR(20)) GO Error Message:
Msg 102, Level 15, State 1,
Line 1 Incorrect syntax
near ‘Table’.
Fails Works
Example: Below Example: Below Statement
statement fails. Works.
SET SET
QUOTED_IDENTIFIER QUOTED_IDENTIFIER
ON OFF
GO GO
SELECT “BIRADAR” SELECT “UMAR”
Error Message:
Msg 207, Level 16, State
1,
Line 1 Invalid column
name ‘UMAR’.
9999-12-31 9999-12-31
23:59:59.997 23:59:59.9999999
8 Bytes 6 to 8 bytes
Note: Parameter n is
optional and if it is not
specified then fractional
seconds precision is 7 digit
and it can be from 0 to 7
digit.
For fractional seconds
precision <3 6="6"
bytes="bytes" font="font"
takes="takes">
For fractional seconds
precision 3 or 4 it will take 7
bytes
For fractional seconds
precision >4 it will take 8
bytes
4 Usage: Usage:
7 Compliance: Compliance:
4 Example: Example:
3 Example: Example:
4 Ex: Ex:
Declare test Declare test VarChar(100);
Char(100); test="Test" -
test="Test" - Then "test" occupies only
Then "test" occupies 4+2=6 bytes. first four bytes
100 bytes first four for value and other two bytes
bytes with values and for variable length
rest with blank data information.
4.Difference between Sql Server 2005 and Sql Server 2008
S.No Sql Server 2005 Sql Server 2008
Difference between a table scan and an index scan in SQL Server Database
S.No Table Scan Index Scan
1 Abbreviation: Abbreviation:
2 Meaning: Meaning:
6 Type of Type of
update/insert/delete: update/insert/delete:
7 Normalization/Denomaliz Normalization/Denomaliz
ation: ation:
Normalization is Denormalization is
promoted (1st normal form, promoted (Dimension and
second normal form and Fact design).
third normal form).
12 Model: Model:
13 Orientation: Orientation:
14 Source: Source:
15 Motive: Motive:
4 Join: Join:
No need to use Complicated Join
complicated join
7 DB Structure: DB Structure:
Simple DB Structure Complicated DB Structure
8 Sample: Sample:
http://blog- http://blog-
mstechnology.blogspot.in/2 mstechnology.blogspot.in/2
010/06/bi-dimensional- 010/06/bi-dimensional-
model-star-schema.html model-snowflake-
schema.html
The MOLAP storage mode The ROLAP storage mode The HOLAP storage mode
causes the aggregations of causes the aggregations of combines attributes of both
the partition and a copy of the partition to be stored in MOLAP and ROLAP. Like
its source data to be stored indexed views in the MOLAP, HOLAP causes
in a multidimensional relational database that the aggregations of the
structure in Analysis was specified in the partition to be stored in a
Services when the partition partition’s data source. multidimensional structure
is processed. in an SQL Server Analysis
Services instance.
This MOLAP structure is Unlike the MOLAP storage HOLAP does not cause a
highly optimized to mode, ROLAP does not copy of the source data to
maximize query cause a copy of the source be stored. For queries that
performance. The storage data to be stored in the access only summary data
location can be on the Analysis Services data in the aggregations of a
computer where the partition folders. Instead, when partition, HOLAP is the
is defined or on another results cannot be derived equivalent of MOLAP.
computer running Analysis from the query cache, the
Services. Because a copy of indexed views in the data
the source data resides in source are accessed to
the multidimensional answer queries.
structure, queries can be
resolved without accessing
the partition’s source data.
Query response times can Query response is Queries that access source
be decreased substantially generally slower with data—for example, if you
by using aggregations. The ROLAP storage than with want to drill down to an
data in the partition’s the MOLAP or HOLAP atomic cube cell for which
MOLAP structure is only as storage modes. Processing there is no aggregation
current as the most recent time is also typically slower data—must retrieve data
processing of the partition. with ROLAP. However, from the relational
ROLAP enables users to database and will not be as
view data in real time and fast as they would be if the
can save storage space source data were stored in
when you are working with the MOLAP structure. With
large datasets that are HOLAP storage mode,
infrequently queried, such users will typically
as purely historical data. experience substantial
differences in query times
depending upon whether
the query can be resolved
from cache or aggregations
versus from the source
data itself.
CTE,TableVariable,TempTable Difference:
1. Temp Tables are physically created in the Tempdb database. These tables act as the normal
table and also can have constraints, index like normal tables.
2. CTE is a named temporary result set which is used to manipulate the complex sub-queries
data. This exists for the scope of statement. This is created in memory rather than Tempdb database.
You cannot create any index on CTE.
3. Table Variable acts like a variable and exists for a particular batch of query execution. It gets
dropped once it comes out of batch. This is also created in the Tempdb database but not the
memory.