SQL Server Interview Questions 3
SQL Server Interview Questions 3
All Resources
On-demand Webinars
Community
Home > Blog > SQL Server >
SQL Server Interview Questions
Are you curious about learning SQL Server interview questions? If that’s the case,
this blog is prepared for you. We have consolidated the mostly-asked SQL Server
interview questions from basics to advanced in this blog. It would help you to
navigate the blog based on your preparation level. We assure you that you will
crack the interview once you read the blog thoroughly. Well! Let's get started.
Rating: 4.7
280368
Share:
search here
For Experienced
Advanced
We have categorized SQL Server Interview Questions - 2022 (Updated) into 3 levels
they are:
Freshers
Experienced
Advanced
Below mentioned are the Top Frequently asked SQL Server Interview Questions and
Answers that will help you to prepare for the SQL Server interview. Let's have a
look at them.
4. What are Triggers, and mention their types in the SQL server?
If you want to enrich your career and become a professional in SQL Server, then
enroll in "SQL Server Online Training" - This course will help you to achieve
excellence in this domain.
2. What is a Relational Database?
Ans: A relational database is the collection of data that have an established
relationship with each other. It consists of rows and columns. Each row in a
database is a record, and columns have the attributes of data. Each row in a table
can be identified with a unique ID known as a primary key. And rows in a table can
be related to other tables using foreign keys.
One to one: When a row in a table has only one related row in another table
One to many: When a row in a table has multiple related rows in another table
Many to many: When a row has multiple related rows in another table and vice-versa.
MindMajix Youtube Channel
Free-form attributes
Domain-based attributes
File attributes
Related Article: SQL Server Tutorial for Beginner
8. What is SQL Server, and mention its core components?
Ans: SQL Server is an RDBMS developed by Microsoft. It has two core components –
database engine and SQLOS. The database engine has a relation engine that supports
processing queries, and managing database files, pages, indexes, etc. And SQLOS is
nothing but a SQL Operating system. SQLOS provides operating systems such as memory
and I/O management. It also performs the operations like exception handling and
synchronization services.
Autocommit transactions
Explicit transactions
Implicit transactions
Batch-scoped transactions
10. What is a Transaction Log, and why is it important?
Ans: Transaction log records all transactions and related database modifications of
every transaction. To be precise, it records the beginning of a transaction, the
changes during the transaction, and the last COMMIT or ROLLBACK of the transaction.
The transaction log is one of the vital components in database management since it
is used to retrieve the database to a consistent state if there is a system
failure.
Isolation: It ensures that each transaction takes place in complete isolation from
other transactions. It means that each transaction must run as if it is the only
transaction that is happening in the system.
Durability: It makes sure that every transaction must be recoverable when required.
Once a transaction is committed, it means that all the data updates have been made
in the database then, and they can be retrieved at any time by users.
INNER JOIN
LEFT (OUTER) JOIN
RIGHT (OUTER) JOIN
FULL (OUTER) JOIN
CROSS JOIN
Related Article: Different Types Of SQL Server Joins
17. What is Subquery in SQL Server?
Ans: It is known as subquery when a query is nested inside the statements such as
SELECT, INSERT, UPDATE, or DELETE. Besides, a nested query inside a subquery is
also known as a sub-query. Further, a subquery is also known as an inner query or
an inner select. Similarly, the statement that has a subquery is known as an outer
query or an outer select.
20. How can you relate a Foreign Key and a Primary Key?
Ans: A foreign key is a field (s) in a table that links the primary key with
another table. A foreign key is mainly used to prevent the loss of a link between
two tables. The table that has a primary key is known as the parent table, and a
table that has a foreign key is known as the child table. A Foreign key can link
with a unique key of another table as it links with a primary key.
Forward only
Static
Keyset
Dynamic
23. What are Triggers, and mention their types in the SQL server?
Ans: Triggers are the special stored procedures. When there is an event in the SQL
server, triggers will run automatically. There are three types of triggers: LOGON,
DDL, and DML.
LOGON Triggers: They are fired when a user establishes a LOGON event.
DDL Triggers: They are fired when there is a Data Definition Language (DDL) event.
DML Triggers: They are fired when there is a modification in data due to Data
Manipulation Language (DML).
24. When can you use Triggers in the SQL Server?
Ans:
Shared locks: It allows a page or rows only for reading. It restricts modifications
of data by concurrent transactions.
Exclusive locks: It allows exclusive transactions to modify a page or row using DML
statements such as INSERT, UPDATE, and DELETE.
Updated locks: It is used to avoid deadlocks. You can place a shared lock over a
resource that already has an updated lock.
30. What is SQL Server Profiler?
Ans: It is a graphical user interface used for monitoring an instance of the
database engine. It creates and manages traces and analyses the trace results when
there is a demand. Generally, trace files record events, which can be replayed
while diagnosing issues such as slow-running queries, finding the root cause of
problems, etc.
33. What is COALESCE in SQL server and mention its few properties?
Ans: It is an expression that evaluates arguments in a list and returns only the
first value that is not NULL.
Now, the COALESCE expression returns the first value 14, which comes first after a
NULL value.
BULK COPY in SQL server can be done in the following four modes:
Native mode data file: Bulk copy from a table or view into a table or view in the
same format.
Character mode data file: Bulk copy from a table or view into another table in a
different format
Bulk copying from a data file into a table or view
Loading data into program variables initially and then bulk copying into a table or
view.
35. What do you mean by Collation?
Ans: Collation refers to the pre-defined rules to sort and compare data, and it
also defines bit patterns that represent characters in a database. The rules help
determine how the data should be stored, accessed, and compared. In addition,
collation also provides case sensitivity and accent sensitivity for a dataset. SQL
servers can store objects that have different collations in a single database.
The below example shows the retrieval of all the data from a table using the SELECT
statement.
SELECT *
FROM DimEmployee
ORDER BY LastName;
Suppose you need to select a portion of a table, then the WHERE clause must be used
along with the SELECT statement to retrieve the required data. The below example
shows the meaning of the WHERE clause in a SELECT statement.
SIGN (numeric_expression)
This function returns values based on the following conditions:
Aggregate functions They work on a group of values and return a single value.
Analytic functions They work with a set of groups and can return multiple rows
for each group.
Ranking functions They return the ranking value for each row.
Rowset functions They return a value that can be used as the table reference.
Scalar functions They work on a single value and return a single value as well.
46. What is the difference between Stored Procedures and Functions?
Ans: In stored procedures, codes are usually compiled, and these compiled codes are
executed when the program calls them. But in functions, on the contrary, codes are
compiled and executed every time they are called.
47. Can you mention the different types of Queries in SQL Servers?
Ans:
Select Query: This query creates the SQL SELECT statement. It retrieves data from
tables or views.
Insert results: This query creates the SQL INSERT INTO….SELECT statement. It allows
copying rows from one table to another and copying rows within a table.
Insert Values: This query creates the SQL INSERT INTO….VALUES statement. It creates
new rows in tables and inserts values into them.
Update Query: This query creates SQL UPDATE…SET statement. It allows updating
values in multiple rows.
Delete Query: This query creates the SQL delete statement. It helps to remove rows
from a table.
Make table query: This query creates the SQL SELECT…INTO statement. It creates a
new table and rows in it.
48. What is the use of Database Engine in the SQL Server?
Ans: The database engine can store, process, and secure data. It creates database
objects such as stored procedures, views, and triggers; and executes the objects.
It processes queries, provides controlled access, and optimizes transactions in a
database. A SQL server can support up to 50 database engine instances in a single
computer.
49. How can you differentiate between UNION and UNION ALL?
Ans: UNION is the operator that combines two queries into a single result set using
select statements. Note that it functions based on specified conditions.
UNION ALL is the operator that combines two queries into a single result set,
extracting all the rows from the tables without any conditions.
INNER JOIN: This command helps to return records that are common to both the tables
LEFT JOIN: This command helps to return values that are common to each other along
with the complete records of the left table
RIGHT JOIN: This command helps to return values that are common to each other along
with the complete records of the right table
FULL JOIN: This command helps return all the tables' records when there is a match
between the two.
CROSS JOIN: It is known as Cartesian Join. This command returns all combinations of
each row from the tables. There is no specific condition for joining two tables.
51. What are the four types of physical JOIN operations?
Ans:
Nested loop joins: They are applied when one join input is small, and the other
join input is large because nested loop joins require minimum I/O and few
comparisons.
Merge joins: When two inputs are large and of the same size and sorted out based on
the join column, then merge join will provide a fast join operation.
Hash joins: They efficiently process large, non-indexed, and unsorted inputs. They
are also helpful in getting intermediate results in complex queries.
Adaptive joins: This join helps to decide when to switch to a Nested loop based on
a threshold value
52. What is the difference between JOIN and UNION in SQL server?
Ans: JOIN is the operator that combines data from many tables based on specific
conditions, which creates new columns. In contrast, UNION combines data from many
tables using SELECT statements, creating new rows.
The number of columns retrieved from the tables needn't be the same in JOIN
statements, whereas the number of columns retrieved from the tables must be the
same in UNION statements.
54. How can you use the SCOPE_IDENTITY function in the SQL server?
Ans: This function returns the last identity value inserted into an identity column
within the same scope. Here, the scope is nothing but a module, which will have a
stored procedure, trigger, batch, and function. If two statements exist in the same
stored procedure or batch or function, then it means that they are in the same
scope.
[
TOP (expression) [PERCENT]
[WITH TIES]
]
56. How can Deadlocks in the SQL server be resolved?
Ans: Deadlock occurs in a SQL server when two processes lock a single resource
simultaneously and wait for the other process to unlock the resource. Generally,
the SQL engine notices this type of incident and ends one of the processes
voluntarily, which helps to release the lock. Thus, the SQL engine allows one
process to complete successfully while stopping another process simultaneously.
On the other hand, global temporary tables are visible to any user. These tables
are deleted only when any user referencing these tables gets disconnected from the
SQL instance.
On the contrary, the CHARINDEX function helps identify a substring's position from
the specified string.
COMMIT: statement is executed to save the changes made on the current transaction;
after that, the transaction becomes permanent.
60. How can you distinguish between GETDATE and SYSDATETIME functions?
Ans: The GETDATE function returns the date and time of a location. In contrast, the
SYSDATETIME function returns the date and time with the precision of 7 digits after
the decimal point.
64. What are SQL Server Integration Services and their functions?
Ans: It is the platform for enterprise-level data integration and data
transformation services. It includes copying and downloading files, loading data
warehouses, managing SQL Server objects, and cleansing and mining data to solve
complex business problems quickly. Integration services consist of a group of
built-in tasks, graphical tools, and an SSIS catalog database.
65. Compare the difference between Clustered Index and the Non-clustered Index?
Ans:
Derived Attribute: It is the attribute that obtains values from other columns.
Derived Persistent Attribute: It is the stored derived attribute.
Computed Attributes: It is the attribute that is computed from internal system
values
67. What are the different levels of normalization, and explain them shortly?
Ans:
First Normal Form (1 NF): It avoids data duplication in a table. It creates a
specific table for the related data and uses the primary key to identify the data.
Second Normal Form (2 NF): It creates separate tables for the group of data that
belongs to multiple records. The tables are linked with foreign keys.
Third Normal Form (3 NF): It eliminates the fields that are not related to keys
Boyce Codd normal form /Fourth Normal form (BCNF or 4NF): It should be in the form
of the 3 NF. Also, there shouldn’t be any multi-valued dependencies.
68. What is the difference between DELETE and TRUNCATE commands?
Ans: The DELETE command removes a row (s) from a table based on given conditions,
whereas the TRUNCATE command removes the entire rows from a table. Also, changes
have to be manually COMMITTED after executing the DELETE command, whereas changes
are COMMITTED automatically after the execution of the TRUNCATE command.
On the other side, OLTP is the Online Transaction Processing and works on the
operational data. It manages ACID properties during transactions. It performs
faster than OLAP, so it is used to perform day-to-day activities such as online
ticket booking, messaging, etc.
FLOOR ( numeric_expression )
For example, refer to the following statement about using the FLOOR function.
This statement will return a value less than 22.35 or the same.
72. What is the use of SQL server locks, and what resources can be locked by server
locks?
Ans: If an exclusive lock locks a row in a table during a specific transaction,
that row cannot be accessed by other transactions. So, others can view or modify
that row only when the lock is released. Also, locks reduce concurrency in
transactions, so locks must be minimized to improve transaction performance.
Note that the resources such as AllocUnit, application, database, extent, file,
key, metadata, etc., can be locked using lock functions.
Simple Recovery Model: There is no log backup, and it eliminates the need to manage
the transaction log space.
Full Recovery Model: This model requires log backups. It doesn’t allow any work to
be lost for any reason.
Bulk-logged Model: This model requires log backups. It allows high-performance
bulk-copy operations.
77. What are the different types of backups used in SQL servers?
Ans:
79. What are User-Defined Functions, and why do we have to use them?
Ans: User-defined functions (UDFs) are routines that accept parameters, perform
complex functions, and return a value or the result set. There are three types of
UDFs – user-defined scalar functions, table-valued functions, and system functions.
Department_No Department_Name
101 ECE
102 EEE
103 CSE
104 IT
105 MECH
Employee Details:
FROM Department D
81. Write a query to display employee details with their department names?
Ans: SELECT employee.employee_no, employee.employee_name, employee.address,
employee.salary, employee.age, department.department_name
FROM department D
ON department.D_no=employee.D_no
82. Write a query to display employee details along with department_name and their
age between 21 to 25?
Ans: SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.d_name
FROM Department D
ON dept.D_no=emp.D_no
83. Write a query to display the employee details whose salary is above 23000 and
age is above 22 and working in the CSE department?
Ans: SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.d_name
FROM Department D
ON dept.D_no=emp.D_no
85. Can you mention the advantages of Stored Procedures over Dynamic SQL?
Ans:
87. What are the advantages of using stored procedures in an SQL server?
Ans:
Stored procedures provide faster performance since they are compiled and stored in
executable form.
A stored procedure is nothing but a piece of code that can be used many times,
increasing productivity.
It can be stored in a database server instead of on a client machine. As a result,
it increases the speed of query execution.
It can be used in many applications after the successful compilation
88. How can you hide an instance of the SQL Server database engine?
Ans: You can hide the SQL server database engine using the SQL Server configuration
manager. The following two steps can accomplish it:
Expand SQL server network configuration, right-click protocols and then select
properties.
Open the Flags tab, select ‘YES’ in the HideInstance box, and click OK to close the
dialogue box.
89. What are the different third-party tools used in SQL servers?
Ans: The following are the various third-party tools used in SQL servers:
Paginated Reports: They produce up-to-date reports with the support of updated
tools and new features.
Mobile Reports: This type has a format that adapts to different devices.
Web Portal: Paginated reports, excel workbooks, KPIs, and mobile reports can be
organized and viewed through the web portal. You can view the web portal using any
modern browser.
91. How can you alter a table schema in an SQL server?
Ans: The following syntax is used to transfer ‘securable’ between schemas.
<entity_type> : : =
{
object | Type | XML Schema Collection
}
Schema_name: It is the name of the schema where the securable is to be moved.
92. How does Intelligent Query Processing improve the performance of SQL Server
workloads?
Ans: The intelligent query processing (IQP) family has many features that improve
the performance of workloads with minimum implementation effort. Here, the various
IQP features are adaptive joins, batch mode on rowstore, approximate QP, etc. You
can automatically apply IQP to all server workloads by enabling compatibility
levels for databases.
Partially contained databases can retain crucial information while making data
transactions. So data can be accessed from databases even if there is missing data
during transactions.
It will be useful in ‘failover’ times when users use the 'Always On' availability
groups.
It helps developers detect ‘instance-level impacts’ and ‘instance-level concerns'
on databases.
96. Compare Triggers with Event Notifications?
Ans:
103. What is the use of views in SQL server, and mention its types?
Ans: The view is the virtual database table created by selecting a few rows and
columns from other tables. Rows and columns are selected through SQL query, which
creates a view as a result. And it helps to organize data by simplifying complex
queries, restricting access to data, and summarising data from many tables.
System-defined views
User-defined views
104. What is a Logon Trigger, and when do they get triggered?
Ans: When there is a logon event, Logon triggers initiate stored procedures. It
occurs when a user session is created with an instance of the SQL Server. Logon
triggers fire only when the authentication stage of logging is over. If the
authentication phase fails, logon triggers won't fire stored procedures.
The spinlock contention can be diagnosed by the tools such as performance meter,
SQL server extended events, and memory dumps. To resolve the spinlock contention
issues, the root cause must be identified first. A shorter code path will resolve
contention issues significantly. In addition, the best practices such as Fully
Qualified Names, Paremterised Queries, and LOCK_HASH contention.
108. What are the two execution modes in the Database Engine in the SQL server, and
what is the difference between the two modes?
Ans: The database engine in the SQL server can execute T-SQL statements in the
following two modes:
Row-mode execution
Batch-mode execution
Row-mode Execution: This mode of execution is applicable when data is stored in row
format. In this mode, when a query is executed, rows are read one by one with
respect to columns specified in the table schema. Then, the SQL server forms the
result set from the data of columns using the statements such as SELECT, JOIN, etc.
Batch-mode Execution: Multiple modes are executed together as a batch in this mode.
It operates on compressed data. It offers better parallelism, faster performance,
and increases memory throughput.
109. What are Data Cleansing and Data Matching in SQL server?
Ans:
Data Cleansing: This Data Quality Service (DQS) process identifies the incorrect or
invalid data in the SQL database and then corrects it; cleansing is carried out in
two steps such as computer-assisted and interactive. The computer-assisted method
uses the knowledge of the DQS knowledge base to process the data and suggests
corrections automatically. In the interactive method, the data steward approves,
rejects, and modifies the changes required to correct the incorrect or incomplete
data.
Data Matching: This DQS process supports reducing data duplication and increases
data accuracy. Data matching is carried out in two ways, such as creating a
matching policy in the knowledge base and performing a deduplication process. The
matching rules help to identify how the records are matching with each other and
the degree of similarity.
110. How can you ensure DQS security in the SQL server?
Ans: DQS security infrastructure is designed based on SQL server security
infrastructure. Here, the Data administrator provides access to DQS resources based
on the defined DQS roles. There are four DQS roles to manage the security of DQS
services. They are known as the database administrator, DQS administrator, DQS KB
Editor, and DQS KB Operator. A set of permissions are assigned with each DQS role
so that they can access the resources based on the given permissions.
The ntext, image, and text data types cannot be used in the select list of
subqueries.
If there is a column name in the WHERE clause of an outer query, then it should be
join-compatible with the column in the subquery select list.
The DISTINCT keyword cannot be used with subqueries that include GROUP BY
A view created by using a subquery cannot be updated
The COMPUTE and INTO clauses cannot be specified
113. What is Polybase, and why do you have to use it?
Ans: Polybase is the data virtualisation feature of the SQL server. This feature
supports connecting with external sources without installing client connection
software. So, we can query data from SQL Server, Oracle, Teradata, MongoDB, Hadoop
clusters, and Cosmos DB using T-SQL. Polybase allows data to stay in its original
location and format. So, it reduces the need for ETL in data movement.
114. What is an SSIS designer in SQL Server, and why do we use it?
Ans: It is a graphical tool used to manage integration service packages in SQL
servers. It allows for creating and maintaining the integration of service
packages.
EXCEPT: operator allows returning distinct rows from the left input query only.
INTERCEPT: operator allows returning distinct rows from both left and right input
queries.
{ <query_specification> | ( <query_expression> ) }
{ EXCEPT | INTERSECT }
{ <query_specification> | ( <query_expression> ) }
117. Explain the Pattern Matching in the SQL server?
Ans: SQL server uses LIKE operator to identify whether a character string matches a
specified pattern. A pattern may consist of regular characters as well as wildcard
characters. So, pattern matching is performed using wildcard characters and string
comparison characters. However, pattern matching using wildcard characters is more
flexible than string comparison operators.
The syntax for the LIKE operator for pattern matching is given as below:
120. How can you improve the performance of stored procedures in the SQL server?
Ans:
Using SET NOCOUNT ON messages, information messages can be prevented from the
result set. It will reduce network traffic and increase operational performance.