SQL Server Interview Questions Developers PDF
SQL Server Interview Questions Developers PDF
By
Rafat Sarosh, Mechele Gruhn
SQL Interview Questions 2
This document, including sample applications herein, is provided for informational purposes only and
neither Azan International nor the Authors makes any warranties, either express or implied, in this
document. Information in this document, including samples, URL and other Internet Web site references,
is subject to change without notice. The entire risk of the use or the results of the use of this document
remains with the user.
The primary purpose of a sample is to illustrate a concept, or a reasonable use of a particular statement or
clause. Most samples do not include all of the code that would normally be found in a full production
system, as a lot of the usual data validation and error handling is removed to focus the sample on a
particular concept or statement. Technical support is not available for these samples or for the provided
source code.
Unless otherwise noted, the example companies, organizations, products, people, and events depicted
herein are fictitious and no association with any real company, organization, product, person, or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Azan International.
2002-2004 Rafat Sarosh. All rights reserved.
Active Directory, ActiveX, BackOffice, CodeView, Developer Studio, FoxPro, JScript, Microsoft,
Microsoft Press, Microsoft SQL Server, MSDN, MS-DOS, Outlook, PivotChart, PivotTable, PowerPoint,
Visual Basic, Visual C++, Visual Studio, Win32, Windows 2000, Windows, and Windows NT are either
registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective
owners.
SQL Interview Questions 3
SQL Interview Questions 4
Contents
Q. During a recent migration project, John inserted 10,000 records in a table that has an Identity
Column called ticketID, which automatically increases by 1 each time a record is inserted in the
table. A month after the database went live; John noticed that record with ticketID 5123 has some
incorrect information. So John deletes this record and decides to re-insert this record in the table. He
wants to re-use the ticketID 5123. He needs to achieve this while the database is in production. What
should he do?................................................................................................................................ 86
Q. Jenny wants to export data to Pivot table in Excel spreadsheet from a table in SQL Server. This
data changes frequently. She wants to automate the process of updating the Excel spreadsheet using
the SQL Job Scheduler. What tool is the best choice for the task?.................................................. 87
Q. You have written an application in VB which uses SQL 7 for its database. You have used many
stored procedure and triggers to make your application fast. However, users complain that saving
records take too much time. To rectify the problem, you start the profiler and create a trace using the
trace wizard. How would you go about identifying the cause of the problem? ................................ 87
Q. You have a table with employee information that rarely changes. However this table is used from
many applications in the organization to validate the data and to produce reports. What would be the
optimal fill factor to choose for indexes created on this table? ....................................................... 87
Q. What is the difference between a fill factor of 100 and 0?......................................................... 87
Q. How will you know when statistics on a table are obsolete? ...................................................... 89
Q. Explain different backup plans. ................................................................................................ 90
Q. What is a full backup?.............................................................................................................. 90
Q. Explain a differential backup. .................................................................................................. 90
Q. Explain an incremental backup. ............................................................................................... 90
Q. What is Log shipping? ............................................................................................................. 91
Q. Every night you run a full backup. After every 3 three hours you make a differential backup. Every
hour you make an incremental backup. In a worst-case scenario, how much work you can lose? .. 91
Q. Explain a Checkpoint? ............................................................................................................. 91
Q. Explain an Automatic Checkpoint............................................................................................ 91
Q. How you can list all the tables in a database?............................................................................ 92
Q. How can you list all the columns in a database? ....................................................................... 92
Q. How can you list all the table constraints in a database? ........................................................... 92
Q. What are the advantages DTS has over bcp? ............................................................................ 92
Q. How do you rebuild an identity column?................................................................................... 92
Q. Create a DTS package to produce a text file using the UPDATE STATISTICS command for the
tables in a database with obsolete statistics. For example, the file content should look like: ........... 93
Q. How can you transfer data from a text file to a database table? Or how can you export data from a
table to a comma delimited (CSV) file? Or how can you import data from MS Access to a table in a
database? Or how can you export data from a table to an Excel file? ............................................102
Q. When does the auto update index statistics feature in SQL server turn itself on?......................102
Q. What specific conditions database should meet, before you can Bulk copy data into it using BCP.
....................................................................................................................................................102
Replication .....................................................................................................................................103
Q. What is database replication? What are the different types of replication you can set up in SQL
Server? ........................................................................................................................................103
Q. Which database stores information about replication? .............................................................103
Q. Your company has 50 branches all over the country. All the branches, including the Head Office
have SQL Server as the database. Every night all 50 branches upload certain information to the Head
Office. Which Replication topology is best suited for the above scenario?.....................................103
Q. Which of the following option(s) is(are) an inappropriate usage of merge replication: a company
time sheet database, a static look up table, a high transaction based application that requires real time
replication to subscribers or a company information price list that is updated at remote sites and
replicated to a central database.....................................................................................................103
Q. What are the restraints imposed on the table design by a Merge Replication? ..........................104
Security ..........................................................................................................................................105
SQL Interview Questions 9
Q. A user is a member of the Public role and the Sales role. The Public role has select permission on
all the tables. The Sales role does not have select permission on some of the tables. Will the user be
able to select from all tables? .......................................................................................................105
Q. If a user does not have permission to a table, but has permission to a view created on it, will he be
able to view the data in table? ......................................................................................................105
Q. Tony works in the Sales Department and has created a table named OrderDetail for the Sales
department. You write a stored procedure which will help Mark, a sales representative, update the
OrderDetail table. However, when Mark uses the stored procedure he gets an error. You realize that
this is a security issue. What is required to enable Mark to use your stored procedure?.................106
Transactions...................................................................................................................................107
Q. Define Concurrency Control....................................................................................................107
Q. What is Pessimistic Concurrency Control? ..............................................................................107
Q. What is Optimistic Concurrency Control? ...............................................................................107
Q. Explain Isolation levels. ..........................................................................................................107
Q. What is the difference between the REPEATABLE READ and SERIALIZABLE isolation levels?
....................................................................................................................................................108
Q. What is the default isolation level in SQL server? ...................................................................108
Q. What is the most restrictive isolation level?.............................................................................108
Q. What is the least restrictive isolation level? .............................................................................108
Q. How do you determine the current isolation level?...................................................................108
Q. What are the conditions an underlying table must satisfy before a cursor can be used by a
positioned UPDATE or DELETE statement? ...............................................................................108
Q. Explain Locks. ........................................................................................................................109
Q. Explain Lock escalation. .........................................................................................................109
Q. Under what condition it is possible to have a page level lock and row lock at the same time for a
query?..........................................................................................................................................109
Q. Explain how to use transactions efficiently. .............................................................................109
Triggers..........................................................................................................................................110
Q. What you can do to delete a table without the delete trigger firing? .........................................110
Q. Other than Truncate statement, which other command can by-pass the trigger on the tables?..110
Q. What are the differences between Triggers and Stored Procedures? .........................................110
Q. Is this statement true: A trigger can reference objects outside the current database?.................110
Q. Can a trigger be created on a view? .........................................................................................110
Q. Will the WRITETEXT statement activate a trigger?................................................................110
Q. Can a table be created inside a Trigger? ..................................................................................110
Q. When should you use an INSTEAD OF trigger?......................................................................111
Q. Can the "If Update (colName)" statement be used in a delete trigger?......................................112
Stored Procedures..........................................................................................................................113
Q. Why should one not prefix user stored procedures with sp_?.................................................113
Q. What are the results of running this script? .............................................................................113
Q. Which table keeps information about Stored Procedures? ........................................................113
Q. What will be the value of @@FETCH_STATUS if a row that was a part of the cursor resultset
has been deleted from the database after the time the stored procedure that opened the cursor was
executed?.....................................................................................................................................114
Q. Why and when do stored procedure recompile? .......................................................................115
Q. How can you find out which stored procedures are recompiling? .............................................115
Q. How can you stop stored procedures from recompiling?...........................................................117
Cursors...........................................................................................................................................118
Q. How many types of cursor type are there?................................................................................118
Q. What is the difference between insensitive and scroll cursor? ..................................................118
Q. If a table does not have a unique index, can a cursor be opened on it? .....................................118
Q. Can a cursor be updated? If yes, how you can protect which columns are updated?..................118
Q. While using a cursor, how can you differentiate between a deleted row and a row that has been
inserted with NULL data values? .................................................................................................119
SQL Interview Questions 10
Q. How can you know if the row fetched from cursor is still valid in underlying table? ................119
Q. How can you find out how many rows returned in a cursor? ....................................................119
Q. What does it mean if @@CURSOR_ROW returns a negative number?...................................119
Q. How can you set the threshold at which SQL Server will generate keysets asynchronously?.....119
Q. What is the difference between Deallocate cursor and Close cursor?........................................119
Q. Define Cursor Locking............................................................................................................120
Datatypes .......................................................................................................................................121
Q. Between Cast and Convert which function would you prefer and why?....................................121
Q. What are the new data types are introduced in SQL 2000? ......................................................121
Q. Why it is recommended to avoid referencing a floating point column in the WHERE clause?..121
Q. You have to store user responses of Yes and No. What kind of data type is best suited for this
task? ............................................................................................................................................121
Q. What is the Exact Numeric data type in SQL?.........................................................................121
Q. Explain timestamp datatype?...................................................................................................122
Q. How can a user-defined datatype be created? ...........................................................................122
Q. What are the approximate numeric data types?........................................................................123
Q. You are creating an application where Users are asked their gender. In the gender combo box you
have three options: 'Male' , 'Female' and 'I choose not to disclose'. These options are stored in the
table as 1, 0 or NULL. Which datatype should you use? ...............................................................123
Q. Which data types generate inaccurate results if used with an = or <> comparison in a WHERE
clause of a SQL statement? ..........................................................................................................123
Q. Beginning with SQL Server Version 7.0, a new enhanced data type nchar was added. What type
of data is supported with this data type? .......................................................................................123
Q. What will happen if a column containing char type data is changed to the nchar data type? ....123
Q. To automatically record the time on which the data was modified in a table, which data type
should you choose for the column?...............................................................................................124
XML in SQL..................................................................................................................................125
Q. What is XDR?.........................................................................................................................125
Q. What is the difference between FOR AUTO and FOR NESTED?............................................126
Q. What is the difference between FOR XML RAW and FOR XML AUTO? ...............................128
Q.Explain FOR XML EXPLICIT Mode? ..............................................................................131
Q. Using the Customer, and Order table in Northwind database, Please write a query to produce
following XML? ..........................................................................................................................131
Q. What is XML Schema? ...........................................................................................................132
Q. What is mapping schema?.......................................................................................................132
Q. You have a table customers , which has three fields, Address, CustomerID and ContactName.
You would like to retrieve rows as follows: ..................................................................................133
Q. What is XPath? .......................................................................................................................134
Q. What keyword you will use to get schema appended to the result set of a for XML query? ....135
Q. If I execute this query..............................................................................................................136
How can I rewrite this query to get all attributes as an element?...................................................136
Clustering.......................................................................................................................................137
Q. Explain Active/Passive and Active/Active cluster configurations? ...........................................137
SQL Server TID BITS...................................................................................................................138
SQL Interview Questions 11
SQL Interview Questions 12
This book is based on a collective effort and personal experience of many individuals. These Interview
questions are collected after personally going through many interviews, reading books, reviewing
magazine, internet articles and lurking in news groups.
This book is a collection of all those questions and answers in one place, for easy reference and revision.
By no means does it replace your online manual or a good SQL book. Our goal is to provide you with
frequently asked questions in interviews, and some explanations to get you started on your quest for
deeper understanding.
Many of the examples in this book use the northwind and pubs databases that are installed with SQL
server.
The latest version of this book can be downloaded from http://www.manyquestions.com. The online
version is continuously updated and kept current. For a year, for no extra cost the new updated versions
will be e-mailed to you.
I highly encourage you to please send me your feedback. Send your questions, your comments, your
criticism, and your suggestions to [email protected].
Many thanks to the following contributors and reviewers: Pete Levenson, Bott Bruce Ted Nethery and
Rawish Khan.
SQL Interview Questions 13
Disclaimers
SQL Server Books On-Line is referenced many times throughout this book. The authors believe that SQL
Server Books On-Line is an excellent reference and there is no need to repeat what has already been
wonderfully explained. This book should be considered a supplement to SQL Server Books On-Line. In
the unlikely event that this book provides conflicting information with the SQL Server Books On-Line,
SQL Server Books On-Line should be considered the ultimate reference.
Intended Audience:
The target audience for this book will have basic SQL Server knowledge and have at least a rudimentary
understanding of the Transact SQL language. In addition, the reader should have a grasp of the
fundamentals of computing.
Target database:
SQL 2000 was used to develop and test the examples in this book. Most of the code examples are written
in ANSI-92 compliant T-SQL and will work equally well on SQL 6.5 or SQL 7.0 as they do in SQL 2000.
However, please note that SQL 2000 was the target development environment.
SQL Interview Questions 14
SQL SERVER
General Relational DBMS knowledge
For example, the EmployeeID may uniquely identify a particular employee. If so, then the EmployeeID is
a candidate key. The Social Security Number (SSN) can also be another candidate key, as it can also be
used to define a unique row in the table. One of these candidate keys may be choosen as a primary key. If
EmployeeID is choosen as a primary key, SSN will become an alternate key.
SQL Interview Questions 15
TOC
SQL Interview Questions 19
There are three possible types of relationships between tables. The type of relationship depends on how the
related columns are defined. The three types of relationships are:
One-to-Many Relationships
Many-to-Many Relationships
One-to-One Relationships
SQL Interview Questions 20
One-to-Many Relationships
A one-to-many relationship is the most common type of relationship. In this type of relationship, a row in
Table A can have many matching rows in Table B, but a row in Table B can have only one matching row
in Table A. For example, the Sales and Stores tables have a one-to-many relationship with each other:
each Store can have many sales, but each sale can have only one store. This relationship uses the Stor_ID
field for its key field.
The primary key side of a one-to-many relationship is denoted by a key symbol. The foreign key side of a
one-to-many relationship is denoted by an infinity symbol.
A many-to-one relationship between Table A and Table B is the same as a one-to-many relationship
between Table B and Table A.
Many-to-Many Relationships
In a many-to-many relationship, a row in Table A can have many matching rows in Table B, and vice
versa. In SQL Server, you create such a relationship by defining a third table, called a junction table,
whose primary key consists of the foreign keys from both Table A and Table B. For example, the Authors
table and the Titles table have a many-to-many relationship that is defined by a one-to-many relationship
from each of these tables to the Titleauthors table. The primary key of the Titleauthors table is the
combination of the au_id column (the Authors table's primary key) and the title_id column (the Titles
table's primary key).
A many-to-many relationship is the only relationship of the three types of relationships that cannot be
directly defined in SQL Server by using foreign key relationships and constraints.
One-to-One Relationships
In a one-to-one relationship, a row in Table A can have no more than one matching row in Table B, and
vice versa. A one-to-one relationship is created if both of the related columns are primary keys or have
unique constraints.
SQL Interview Questions 21
This type of relationship is not common. You might use a one-to-one relationship to divide a table with
many columns for security purposes or to increase data access performance.
Q. What is a subquery?
A. A subquery is a nested select statement where the inner select statement is evaluated first. You can use
the results of the inner select statement as the input for another select statement. For example, you can use
the results of a subquery as a search condition that uses the IN ( ) function or EXISTS operator:
USE northwind
USE pubs
SELECT * FROM authors WHERE au_id
NOT IN (SELECT au_id FROM titleauthor)
The subquery runs once for each row that the main query returns. This repeated access to the
[Order Details] table could be inefficient. Generally, correlated subqueries can be re-written as a query
using a join.
TOC
SQL Interview Questions 22
Q. How do you use a subquery to find records that exist in one table
and do not exist in another?
A. Use a subquery with the NOT IN clause or NOT EXISTS clause.
For example, Find out authors who do not have any records in the titleauthor table?
USE pubs
SELECT * FROM authors WHERE au_id
NOT IN (SELECT au_id FROM titleauthor)
Prefered method is now an ANSI outer join where outside join table value is NULL.
SELECT a.*
FROM a
LEFT OUTER JOIN b
ON a.joinColumn = b.joinColumn
WHERE b.joinColumn IS NULL
SQL Interview Questions 23
To achieve first normal form, we will eliminate the repeating column, and create a salary table.
Salary Table
Employee Table
In this manner, the salary information is not repeated in the Employee table, and/or the employee
information is not repeated in the Salary table.
SQL Interview Questions 25
In the above table, the primary key is OrderNo and ItemNo. What we see here is a violation of the second
normal form, as the Item Description and Price fields are not dependent on the whole primary key, but are
only dependent on part of the primary key. So to achieve 2NF (Second Normal Form), we create another
table called Item.
Item
LineItem
SQL Interview Questions 26
In the above example, the primary key OrderNo can uniquely determine VendorCity, but VendorCity also
depend upon VendorNo. To normalize this table to 3NF, create another table called Vendor.
Vendor
Order
SQL Interview Questions 27
We can realize the Boyce/Codd normal form by removing the column BirthYear, as this column is
dependent upon the DOB column not on the primary key Empno.
Employee Table
TOC
Q. What is denormalization?
A. Denormalization is the opposite of normalization process. It is deliberately introducing redundancy
into a table design. In some situation, redundancy helps query performance as the number of joins can be
reduced, and some calculated values can be kept in a column reducing the overhead of doing calculations
with every query. An example of denormalization is a database that stores the quartely sales figures for
each item as a separate piece of data. This approach is often used in data warehouse or in on-line
analytical processing applications.
SQL Interview Questions 28
Q. How would you design a database for an online site, which would
average a million hits a day?
A. This is an open ended question to check your understanding about the subject. The interviewer wants to
see how inquisitive you are and how well you prepare before you jump on the design board. The
interviewer certainly expects some more questions in reponse from you to see your analytical ability.
Before you answer any thing you must ask, about the application. What kind of application it is
supporting? Is it an On-line Transaction Processing (OLTP) or Decision Support System (DSS)
application? What time of day is the traffic is highest? Is the traffic evenly distributed, or does it come and
go in peaks? What is the spike threshold the site is expected to endure? What is the size of the database?
How many tables will be there? What is the hardware configuration? Is it preexisting, or is the
interviewer expecting you to design that as well? How many hard disks you will have? What is the
performance expectation? What hardware is available? Are the servers running clustering or SAN
technology?
To approach this question, first get all the facts about the application and the system, and then begin
reciting the best practices of database design.
For a good database design there are many things to be considered, but few of them are of paramount
importance, namely :
- Normalization and appropriate denormalization of the database
- Proper indexing of the tables
- Optimum use of the stored procedures, functions etc.
- Distribution of data tables, their indicies and transaction logs on different hard disks
- Proper choice of data types
After basic design with the help of a performace monitor, use the Index Tuning Wizard and Profiler to
further refine the design to a more detailed level.
Above all, make sure you are desiging the right system. It would be unfortunate to ramble on for 15
minutes about all the correct things for a SQL Server implementation only to find out that the database
will reside on a legacy server running Linux.
SQL Interview Questions 29
Q. You are testing the performance of a query. The first time you run
the query, the performance is slow. The second time you run the
query, the performance is fast. Why is this?
A. The second query is faster bacause SQL server caches the data, and keeps the query plans in memory.
Q. What you can do to remove data from the cache and query plans
from memory for testing the performance of a query repeatedly?
A. If you want to run your query and test its performance under the same circumstances each time, use
command DBCC DROPCLEANBUFFERS after each run to remove the data from the memory and DBCC
FREEPROCCACHE to remove the query plans from the memory.
A. In update clause, once an alias is defined, you must use that alias in the update clause or an error will
be generated. The correct statement is below.
UPDATE c
SET c.name = u.name
FROM customer c, customerdetail cd
WHERE c.custid = cd.custid
SQL Interview Questions 30
Q. Write the fastest query to find out how many rows exist in a table.
A. The fastest query for this purpose is:
SELECT rows
FROM sysindexes
WHERE id = OBJECT_ID (tablename)
AND indid < 2
With this query, SQL does not have to do a full table scan or an index scan to find out the number of rows
in a table. The rows column in the sysindexes table keeps the current rowcount dynamically for the
clustered index. If table has a clustered index then indid = 1. If no clustered index exists then indid = 0 for
the table.
The statement
SELECT COUNT(*) FROM TableName
requires a full table scan or full clustered index scan to determine the total number of rows. Recall that
the interviewer asked for the fastest query that would return the results. SELECT COUNT(*) will
return the correct answer, but it will not be the fastest.
Pay attention to Query cost in the diagram
SQL Interview Questions 31
Q. The COUNT() function always returns a int value type. What should
you do if you need to count rows from a query which you know will
return a value that is too large for an int value type?
A. If you think that the count function will be returning rows more than 2^31 -1, then you can use the new
function COUNT_BIG, which always returns a big integer value type.
COALESCE is equivalent to a searched CASE expression where a NOT NULL expression1 returns
expression1 and a NULL expression1 returns expression2. In searched CASE expression form, it would
look like this:
CASE
WHEN expression1 IS NOT NULL THEN expression1
ELSE expression2
END
COALESCE returns the first non-null value from the list.
SQL Interview Questions 32
Q. Write a query to get all of the employees and all of the orders that were
processed this year. Employees who have no orders should also be
displayed.
TOC
TOC
A. Because 3/4 (.75) is converted into integer, but the result is less than one, therefore it becomes 0. To
make this query work use
SELECT (3.0/4) * 100
or
TOC
TOC
Q. Which four data types cannot be used as a return type from a user-
defined function?
A. text, ntext, image, and timestamp.
TOC
TOC
SQL Interview Questions 35
SELECT *
FROM CustomerNamesInRegion(NWA)
GO
The RETURNS clause contains only the keyword table. You do not have to define the format of a return
variable because it is set by the format of the result set of the SELECT statement in the RETURN clause.
There is no function_body delimited by BEGIN and END.
The RETURN clause contains a single SELECT statement in parentheses. The result set of the SELECT
statement forms the table returned by the function. The SELECT statement used in an inline function is
subject to the same restrictions as SELECT statements used in views.
TOC
Q. How can you log an error to the server event log from a stored
procedure?
A. Use the WITH LOG option in the RAISERROR function.
RAISEERROR (This is an error, 19, NULL) WITH LOG
The WITH LOG option logs the error in the server error log and the event log. This option is required for
messages with a severity level of 19 through 25, and it can be issued only by the system administrator.
Other options can be:
NOWAIT Sends messages immediately to the client server.
SETERROR Sets @@ERROR value to msg_id or 50000 regardless of the severity level.
This example returns an @@ERROR value of 50000:
RAISERROR(Test Only,1,2) WITH SETERROR
This example returns an @@ERROR value of 101:
RAISERROR (101, 1, 2) WITH SETERROR
TOC
By using the ROLLUP operator, these groupings are created by moving right to left along the list of
columns:
pub_name au_lname title SUM (qty)
pub_name au_lname (null) SUM (qty)
pub_name (null) (null) SUM (qty)
(null) (null) (null) SUM (qty)
The (null) value represents all values for that column.
If you use the SELECT statement without the ROLLUP operator, the statement creates a single grouping.
The query returns a sum value for each unique combination of pub_name, au_lname, and title.
pub_name au_lname title SUM(qty)
Compare these examples with the groupings created by using the CUBE operator on the same query:
pub_name au_lname title SUM(qty)
pub_name au_lname (null) SUM(qty)
pub_name (null) (null) SUM(qty)
(null) (null) (null) SUM(qty)
(null) au_lname title SUM(qty)
(null) au_lname (null) SUM(qty)
pub_name (null) title SUM(qty)
(null) (null) title SUM(qty)
For cube, Columns included in the GROUP BY clause are cross-referenced to produce a superset of
groups. The aggregate function specified in the select_list is applied to these groups to produce summary
values for the additional super-aggregate rows. The number of extra groups in the results set is determined
by the number of columns included in the GROUP BY clause.
Every possible combination of the columns or expressions in the GROUP BY clause is used to produce
super-aggregates. If you have n columns or expressions, there are 2 (n) -1 possible super-aggregate
combinations. Mathematically, these combinations form an n-dimensional cube, which is how the
operator got its name.
This example demonstrates the results set from a SELECT statement that uses the CUBE operator. The
SELECT statement covers a one-to-many relationship between book titles and the quantity sold of each
book. By using the CUBE operator, the statement returns an extra row.
SELECT title, Qty = SUM(qty)
FROM sales, titles
WHERE sales.title_id = titles.title_id
SQL Interview Questions 38
GROUP BY title
WITH CUBE
This is the results set:
Title Qty
-------------------------------------------------- -----------
But Is It User Friendly? 30
Computer Phobic AND Non-Phobic Individuals: Behavi 20
You Can Combat Computer Stress! 35
(null) 85
SQL Interview Questions 39
Q. You have two tables with a one to many join based on a field
named ID. You need to find records in the parent table that are not
represented in the child table. How would you accomplish this?
A. You can use a LEFT JOIN to show records that exist in one table, but not another. For example:
SELECT parent.ID, child.ID
FROM parent
LEFT JOIN child
ON parent.ID = child.ID
WHERE child.ID is NULL
Write both the queries in SQL Query analyzer and view the query execution plan for each.
Q. You schedule a job to run every minute. What will happen if the
first job runs more than 1 min? Will the second instance of the job
start?
SQL Interview Questions 42
A. No, Only one instance of a job can run at a time. If you execute a job
manually while it is running as scheduled, SQL Server Agent refuses the
request. The timer for the second job will start only after first job ends.
Q. You have several tables, and they are joined together for querying.
The tables contain both clustered indexes and non clustered indexes.
To optimize performance, how should you distribute the tables and
their indexes onto different file groups?
A. Try to keep the non-clustered indexes on different file groups from their tables and on different disks so
that they can be scanned in parallel for data. You cannot separate clustered indexes from the underlying
table.
USE Northwind
GO
SELECT * FROM aTable
IF @@ERROR<>0 BEGIN
PRINT Table does not exist
-- create a new table here
END
Other alternative is write a separate procedure, and check @@ERROR after the execution.
USE Northwind
GO
EXEC CheckTable
IF @@ERROR <> 0 BEGIN
PRINT Table does not exist
-- create a new table
END
SQL Interview Questions 44
A. Referring Stored Procedures and views that refer to the table need to be dropped explicitly.
DROP TABLE removes a table definition and all data, indexes, triggers, constraints, and permission
specifications for that table, however, any view or stored procedure that references the dropped table must
be explicitly dropped by using the DROP VIEW or DROP PROCEDURE statement.
DROP TABLE cannot be used to drop a table being referenced by a FOREIGN KEY constraint. The
referencing FOREIGN KEY constraint or the referencing table must first be dropped.
When a table is dropped, rules or defaults on it lose their binding, and any constraints or triggers
associated with it are automatically dropped. If you re-create a table, you must rebind the appropriate rules
and defaults, re-create any triggers, and add all necessary constraints.
You cannot use the DROP TABLE statement on system tables.
If you delete all rows in a table (DELETE tablename) or use the TRUNCATE TABLE statement, the table
exists until it is dropped.
Q. Explain the DBCC PINTABLE command. When would you use it?
A. DBCC PINTABLE is best used to optimize performance from small, frequently referenced tables. The
pages for the small table are read into memory the first time they are used, and subsequent references to
the data does not require a disk read.
SQL Server does not flush pinned pages when it needs space to read in a new page. DBCC PINTABLE
does not cause the table to be read into memory. As the pages from the table are read into the buffer cache
by normal Transact-SQL statements, they are marked as pinned pages. SQL Server still logs updates to
the page and, if necessary, writes the updated page back to disk. However, SQL Server does keep a copy of
the page available in the buffer cache until the table is unpinned with the DBCC UNPINTABLE
statement.
Q. There is a table with 100 rows of data. You want to add a new column to
the table using the ALTER TABLE command. Which of the following is
true?
A.This column can allow Null values
B.This column can be restricted to Not Null
C. This column can be an identity column
A. The answer is both A and C. You can add an identity column to a table that already has existing data.
You can add a column, which allows null. If you want to add a column which does not allow null then you
must define a default value for the column.
TOC
Q. What is deadlock?
A. A deadlock occurs between two processes, when one process is waiting for an object the other process
has locked. A thread in a multi-threaded system may acquire one or more resources (locks). If another
thread (Thread B) currently owns the resource being acquired, the first thread (Thread A) may have to
wait for the owning thread to release the target resource. The waiting thread (Thread B) is said to have a
dependency on the owning thread (Thread A) for that particular resource.
If the owning thread (Thread A) wants to acquire another resource that is currently owned by the waiting
thread, the situation becomes a deadlock: both threads cannot release the resources they own until their
transactions are committed or rolled back, and their transactions cannot be committed or rolled back
because they are waiting on resources the other owns.
Deadlocking is often confused with normal blocking. When one transaction has a lock on a resource that
another transaction wants, the second transaction waits for the lock to be released. The second transaction
is blocked, not deadlocked.
Q. What is a livelock?
A. A livelock occurs when a request for an exclusive lock is repeatedly denied because a series of
overlapping shared locks keeps interfering. SQL Server detects the situation after four denials and refuses
further shared locks. A livelock also occurs when read transactions monopolize a table or page, forcing a
write transaction to wait indefinitely.
SQL Interview Questions 51
Q. Define Joins.
A. A join is a way of taking the results of one table, view, or query and correlating them with the results of
another table, view, or query. Join conditions can be specified in either the FROM or WHERE clauses;
specifying them in the FROM clause is recommended. WHERE and HAVING clauses can contain search
conditions to further filter the rows selected by the join conditions. Joins can be categorized as inner, outer
and cross joins.
Inner joins (the most common join operation, which uses some comparison operator like = or <>). These
include equi-joins and natural joins.
Inner joins use a comparison operator to match rows from two tables based on the values in columns from
each table.
When executing an INNER JOIN the keyword INNER is optional.
For example, here is an inner join retrieving the authors who live in the same city and state as a publisher:
USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors a
INNER JOIN publishers p
ON a.city = p.city
AND a.state = p.state
ORDER BY a.au_lname ASC, a.au_fname ASC
The tables or views in the FROM clause can be specified in any order with an inner join or full outer join;
however, the order of tables or views specified when using either a left or right outer join is important.
Other kind of joins are outer join and cross join, they are discussed in detail in next question.
TOC
SQL Interview Questions 53
Because an outer join includes unmatched rows, you can use it to find rows that violate foreign key
constraints.
When you create an outer join, the order in which tables appear in the SQL statement is significant. The
first table you add becomes the "left" table and the second table becomes the "right" table. When you
specify a left or right outer join, you are referring to the order in which the tables were added to the query
and to the order in which they appear in the SQL statement in the SQL pane.
There are three variations of outer join.
Left outer join All rows from the first-named table (the "left" table, which appears leftmost in the JOIN
clause) are included. Unmatched rows in the right table do not appear. In this example the left table is
titles and the right table is publishers.
SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles
LEFT OUTER JOIN publishers
ON titles.pub_id = publishers.pub_id
The above query include all titles, even those who do not have publisher id.
Right outer join All rows in the second-named table (the "right" table, which appears rightmost in the
JOIN clause) are included. A right outer join between the titles and publishers tables will include all
publishers, even those who have no titles in the titles table. The resulting SQL might look like this:
SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles
RIGHT OUTER JOIN publishers
ON titles.pub_id = publishers.pub_id
TOC
SQL Interview Questions 54
A.
RETURN
(
SELECT productID, ProductName FROM Products
ORDER BY
CASE WHEN @OrderBy = 1 THEN ProductID END,
CASE WHEN @OrderBy = 2 THEN ProductName END
)
if I execute these two statements, one with UDF, one with out it.
select top 100 Empid, dbo.GetDayOfWeek (JobDate, 1 ) from Timesheet
After profiling these queries, I see the Query with UDF did many more reads, took excessively much time,
and consumed more CPU too.
A. One solution can be to execute the stored procedure from a job, and that job in turn can be called from
Trigger.
Using SQL Server Agent, define a job MyBatchJob. This job should call the stored procedure to execute
the batch job.
From trigger, this job can be called as follows:
EXEC msdb..sp_start_job MyBatchJob
This job starts and run under SQL server Agent control. The sp_start_job procedure returns control to the
calling T-SQL code as soon as the job starts. Trigger does not wait for this job to complete, rest of the
trigger code executed as soon as job starts, which is a great improvement over waiting for the whole batch
processing to finish. SQL server Agent initiates a new connection and run the job in background. You
have to be careful about the concurrency though, now two different connections accessing the same data.
Read More on: SQL server magazine. InstantDoc ID 21975
SQL Interview Questions 59
EmpID
1
2
3
4
Indexes
Q. What is an index?
A. An index is a structure, which provides an efficient method for locating a particular row or set of rows.
It is commonly used to enforce uniqueness constraints.
TOC
Columns frequently involved in search conditions of a query (WHERE clause) that return exact
matches.
Decision Support System applications for which joins and grouping are frequently required.
Create multiple nonclustered indexes on columns involved in join and grouping operations, and a
clustered index on any foreign key columns.
TOC
SQL Interview Questions 63
Q. Your table has a large character field. There are queries that use
this field in their search clause. What should you do?
A. Creating indexes on very large fields is not advisable. SQL Server 2000 provides a new function
CHECKSUM, which computes a checksum on a row or a column, and its value is always a 4 byte integer.
You can create a computed column to be the checksum of your large character field and then build an
index on that computed column. The values returned by CHECKSUM are not guaranteed to be absolutely
unique, but there will be few duplicates. Since there is the possibility of two character string values having
the same value for the checksum, your queries will need to include the full string that you're looking for.
SELECT *
FROM titles
WHERE title = Straight Talk About Computers
AND check_title =
CHECKSUM(' Straight Talk About Computers')
TOC
Q. What is clustering?
A. Clustering is physically ordering the rows of table to match the order of the index.
TOC
TOC
SQL Interview Questions 65
There is a clustered index on ItemID. Most of the time you query this table on [Item Desc] and
[Year Manufactured], what other indexes should you create to make your queries run faster?
A. Create a composite non-clustered index on [Item Desc] and [Year Manufactured]. There is no need to
create two non-clustered indexes, as the [Year Manufactured] is not going to change much.
[Year Manufactured] will not have many distinct values; therefore, a separate index on this column is not
justified.
Most selective columns should be put leftmost in the key of nonclustered indexes. The index is built in the
order that the columns are defined for the constraint. You should adjust the order of the columns in the
constraint to make the index most useful to queries.
For this example, create an index as follows:
CREATE INDEX Item
ON ([Item Desc] , [Year Manufactured])
Note: If you query the table based only on [Year Manufactured], the composite index will not be used
since, it is not the first column in the index.
Q. Which data type columns are the best candidates for full-text
indexing?
A. Datatypes text and varchar are the best candidates for full-text indexing.
How you would rewrite the SQL Query to return the CUSTOMERID
sorted numerically?
A. Because the CUSTOMERID column is a character data type, the values in the column are being sorted
alphabetically. To sort them numerically, in the ORDER BY statement, force a cast operation on the
character value of CUSTOMERID to convert it to an integer.
CUSTOMERID CUSTOMERNAME
______________________________________
1 AAA
2 CCC
3 EEE
11 BBB
12 DDD
SQL Interview Questions 68
The UNION operator allows you to combine the results of two or more SELECT statements into a single
result set. Result sets combined using UNION must have the same structure. They must have the same
number of columns, in the same order, and the corresponding result set columns must have compatible
data types.
SQL Interview Questions 69
What happens after the above statement is executed? Will the trigger will fire and all the hire date will be
filled with the current date?
A. No, SQL server triggers fire only once per statement, not for every row affected. Only the last row is
updated with the current date. But rest of all the rows will have the original data.
OPENQUERY is the preferred way, OPENROWSET has to have connection information, password, and
user name hard coded into T-SQL, which makes it vulnerable to breaking, if ever the connection
information changes. Writing passwords in connection string text also increase the chance of security
breaches.
Q. How you can add messages to the NT event log from within a
stored procedure?
A. XP_logevent will log a user-defined message into the SQL Server log file and the Microsoft
Windows NT Event Viewer. xp_logevent can also be used to send an alert without sending a message to
the client.
SQL Interview Questions 70
Q. What are three ways you can use an identity value inside a trigger?
Why would you prefer one way over another?
A. There are three ways to get an Identity value. IDENT_CURRENT(), SCOPE_IDENTITY() and
@@IDENTITY. All three return last-generated identity values. However, the scope and session make the
result different in each of these functions.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table.
@@IDENTITY returns the last identity value generated for any table in the current session, across all
scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the
current scope.
For example, you have two tables, T1 and T2, and an INSERT trigger defined on T1. When a row is
inserted to T1, it inserts 1111 in the identity column, the trigger fires and inserts a row in T2. For
example, let say 2222 is inserted in the T2 identity column. This scenario illustrates two scopes: the insert
on T1, and the insert on T2 as a result of the trigger.
IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on
T1.
@@IDENTITY will return the last IDENTITY column value inserted across any scope in the current
session, which is the value inserted in T2, which in this case is 2222.
SCOPE_IDENTITY() will return the IDENTITY value inserted in T1, which was the last INSERT that
occurred in the same scope. It will return 1111.
IDENT_CURRENT (T1) would have returned 1111 and 2222 for IDENT_CURRENT(T2).
Choose the method that is appropriate for your needs.
Q. How can I get the name of the first day of the month using SQL Query?
A.
SELECT DATENAME (dw, CAST(DATEPART ( mm,GETDATE()) AS CHAR(2)) + '/1' +
'/' + CAST(DATEPART (YYYY,GETDATE()) AS CHAR(4)))
Q. How can I return the name of last day of the current month using SQL
Query?
A. This is a little more interesting. The easiest way to do this is with a small trick. Divide and conquer.
If the current month is December, return the DATENAME value for 12/31 concatentated with the current
year. If the month is not December, return the value for the first of the next month, then substract one to
return the last day of the previous month. This will return the last day of the current month and will
automatically take into account leap years and leap centuries as well as months that just do not have 31
days.
SELECT
CASE
WHEN DATEPART ( mm,GETDATE()) = 12 THEN
DATENAME(dw, CAST( '12/31/' + CAST((DATEPART (YYYY,GETDATE())) AS
VARCHAR(4)) AS DATETIME) - 1)
ELSE
DATENAME(dw, CAST(CAST((DATEPART ( mm,GETDATE())+1) AS VARCHAR(2)) +
'/1' + '/' +
CAST((DATEPART (YYYY,GETDATE())) AS VARCHAR(4)) AS DATETIME) - 1)
SQL Interview Questions 71
END
SQL Interview Questions 72
View
Q. List some of the rules that apply to creating and using a view.
A. When creating and using a view, these rules apply:
CREATE VIEW statements cannot be combined with other SQL statements in a single batch.
You cannot create a trigger on a view.
Data modification statements (INSERT or UPDATE) are allowed on multi-table views if the data
modification statement affects only one base table. Data modification statements cannot be used on more
than one table in a single statement.
INSERT statements are not allowed if a computed column exists within the view.
All column(s) being modified must adhere to all restrictions of the base table. This applies to column
nullability, constraints, identity columns, and columns with rules and/or defaults and base table triggers.
UPDATE statements cannot change any column in a view that is a computation, nor can they change a
view that includes aggregate functions, built-in functions, a GROUP BY clause, or DISTINCT.
You cannot use READTEXT or WRITETEXT on text or image columns in views.
SQL Interview Questions 73
Q. You added a row to a view, but the row is not shown on the view.
Explain how this can happen, and how you can remedy the situation.
A. By default, data modification statements on views are not checked to determine whether the rows
affected will be within the scope of the view. You can issue an INSERT statement on a view to add a row
to the underlying base table, but not to add it to the view. Similarly, you can issue an UPDATE statement
that changes a row so that the row no longer meets the criteria for the view. If all modifications should be
checked, use the WITH CHECK option.
For example, if you insert a value of AAA into a view that only returns values that are LIKE B%. This
would not be allowed if the WITH CHECK option was in place.
TOC
The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is
also specified.
SQL Interview Questions 74
A. Using SELECT TOP you can have a view sorted in the desired order, for e.g.
CREATE VIEW vAuthors AS
SELECT TOP 100 PERCENT au_lname, au_fname
FROM Authors ORDER BY au_lname
GO
au_lname au_fname
__________________________________________________
Bennet Abraham
Blotchet-Halls Reginald
Carson Cheryl
DeFrance Michel
del Castillo Innes
Dull Ann
Green Marjorie
Greene Morningstar
Gringlesby Burt
Hunter Sheryl
Karsen Livia
Locksley Charlene
MacFeather Stearns
McBadden Heather
O'Leary Michael
SQL Interview Questions 75
A. Derived tables are a SELECT statement that you use in a FROM clause in place of tables references.
Transact-SQL has extensions that support the specification of objects other than tables or views in the
FROM clause. These other objects return a result set, or rowset that form a virtual table. The SELECT
statement then operates as if the result set were a table.
These virtual tables can help you in executing certain queries that are not possible without having a view
or a temp table.
SELECT au_lname, au_fname , royaltyper
FROM (SELECT A.au_lname, A.au_fname, TA.royaltyper
FROM Authors A
JOIN TitleAuthor TA on
A.au_id = TA.au_id
WHERE state = 'CA')
ORDER BY au_lname
USE pubs
SELECT *
FROM INFORMATION_SCHEMA.TABLES
There are number of ANSI SQL Schema Views are defined. Check SQL Server Books On Line for a list
of views.
SQL Interview Questions 77
-- On Server1:
CREATE TABLE Sales1
([Month] INTEGER PRIMARY KEY
CHECK (Month = 1),
Sales float)
-- On Server2:
CREATE TABLE Sales2
([Month] INTEGER PRIMARY KEY
CHECK (Month = 2),
Sales float)
-- On Server3:
CREATE TABLE Sales3
([Month] INTEGER PRIMARY KEY
CHECK (Month = 3),
Sales float)
After creating the member tables, you define same partitioned view on each server all having the same
name. This allows queries referencing the distributed partitioned view name to run on any of the member
servers. The system works as if a copy of the original table is on each member server, but in fact each
server has only a member table and a distributed partitioned view. The location of the data is transparent
to the application.
CREATE VIEW Sales AS
SELECT * FROM DB1. Owner.Sales1
SQL Interview Questions 78
UNION ALL
SELECT * FROM Server2.DB1. Owner.Sales2
UNION ALL
SELECT * FROM Server3.DB1. Owner.Sales2
The most important rule is that a partitioning column exists on each member table and, through CHECK
constraints, identifies the data available in that specific table. Partitioning column key values are enforced
by CHECK constraints. The key ranges of the CHECK constraints in each table do not overlap with the
ranges of any other table. Any given value of the partitioning column must map to only one table.
Q. How you can get a list of all the table constraints in a database?
A.
SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE.
See the question on ANSI SQL Schema view for detail.
Q. If no size is defined while creating the database, what size will the
database have?
A. The size of the model database determines the initial size of a database if no size is indicated in the
CREATE DATABASE statement.
Q. How can you set the database to single user mode and restrict the
access to dbo use only?
A. In SQL Server 2000, a database cannot be in single-user mode with dbo use only. Instead, the
following alternative options are available by using the ALTER DATABASE command:
ALTER DATABASE database SET SINGLE_USER.
This command restricts access to the database to only one user at a time.
ALTER DATABASE database SET RESTRICTED_USER.
This command restricts access to the database to only members of the db_owner, dbcreator, or sysadmin
roles.
ALTER DATABASE database SET MULTI_USER.
This command returns access to the database to its normal operating state.
TOC
SQL Interview Questions 81
A. One alternative is
DECLARE
@TableName varchar (50)
OPEN TableCur
CLOSE TableCur
DEALLOCATE TableCur
SQL Interview Questions 82
Q.How you can list all the indexes for a table in a database?
A.
DECLARE
@TABLENAME varchar(255)
select @TABLENAME ='Application'
END
from sysindexes JOIN sysobjects on
sysindexes.id = sysobjects.id
where sysobjects.name = @TABLENAME
and sysobjects.xtype = 'U'
and indid < 255
An easier method to listing the indexes for a table is to use the SQL Enterprise Manager and right click
on the table name and then choose indexes.
TOC
SQL Server does not shrink a database to a size any smaller than the size that was specified in the
CREATE DATABASE statement.
Q. How can you find out if the current user is a member of the
specified Microsoft Windows NT group or Microsoft SQL Server
role?
A. The Is_Member function.
IS_MEMBER ({group | role})
Q. Your SQL Server is running out of disk space. You notice that
there are several large files with LDF extensions. What are these
files?
A. LDF files are the database log files.
Q. You accidentally delete the MSDB database. What effect does this
have on your existing SQL databases, and how do you recover?
A. Your ability to use SQL Server Agent to schedule alerts, jobs, and recording operators has been
eliminated. To recover from this situation, restore the MSDB database from a backup.
get locking problems. Which tool is best suited to help you diagnose
the problem?
A. SQL Server Profiler. Use SQL Server Profiler with a trace to observe Lock conflicts.
Set IDENTITY_INSERT Tickets ON allows explicit values to be inserted into the identity column of a
table.
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table
already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another
table, Microsoft SQL Server returns an error message that states SET IDENTITY_INSERT is already
ON and reports which table it is set ON for.
If the value inserted is larger than the current identity value for the table, SQL Server automatically uses
the new inserted value as the current identity value.
The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.
SQL Interview Questions 87
only table. A lower value leaves more empty space on the data pages, which reduces the need to split data
pages as indexes grow but requires more storage space. This setting is more appropriate when there will
be changes to the data in the table. A value of 0 behaves like 100 but leaves some extra space in the upper
level of the data tree.
SQL Interview Questions 89
Out-of-date or missing statistics are indicated as warnings (table name in red text) when the execution
plan of a query is graphically displayed using SQL Query Analyzer.
SQL Interview Questions 90
Q. Every night you run a full backup. After every 3 three hours you
make a differential backup. Every hour you make an incremental
backup. In a worst-case scenario, how much work you can lose?
A. In the worst case you will loose, one hour of work, as every hour you take incremental backup.
Q. Explain a Checkpoint?
A. A Checkpoint is essentially a bookmark for activity in the database transaction log. A database has a
transaction log that records data modifications made in the database. The log records every transaction.
SQL Server stores enough information in the log to either redo (roll forward) or undo (roll back) the data
modifications that make up a transaction
As writing data on disk is a slow process, SQL Server caches modifications in buffers for a period of time
to optimize disk writes. A page in this cached buffer is known as a dirty page. Writing this dirty buffer
page to disk is called flushing the page. In SQL Server, first log images are written to disk before the
corresponding data modification.
A commit operation writes all log records for a transaction to the log file. A commit operation does not
have to force all the modified data pages to disk as long as all the log records are flushed to disk. A system
recovery can roll the transaction forward or backward using only the log records.
Periodically, SQL Server ensures that all dirty log and data pages are flushed. This is called a checkpoint.
If the database is using either the full or bulk-logged recovery model, an automatic checkpoint is
generated whenever the number of log records reaches the number SQL Server estimates it can process
during the time specified in the recovery interval option.
Automatic checkpoints truncate the unused portion of the transaction log if the database is using the
simple recovery model. The log is not truncated by automatic checkpoints if the database is using the full
or bulk-logged recovery models.
Or
SELECT name FROM sysobjects WHERE type = 'U'
The first query will tell you all table name plus system tables and views too.
For additional information see Information Schema Views in SQL Server Books On-Line.
Add a task in the package to run this script file, and result should be sent to you in mail. This question is
more an exercise in how to create and manage DTS package, rather than mirroring a real life scenario.
A. First write SQL to find out the table names with obsolete statistics. SysIndexes tables has two fields
rows and rowmodctr, which keeps the number of total rows in a table and the number of modified
rows. If more than 20% rows are modified you should update statistics of the tables. To find out the name
of tables you can run the following query:
This statement will give us the list of all the tables and their index names, which has more than 20%
modified rows.
TABLENAME INDEXNAME
Dtproperties pk_dtproperties
timeSheetAuditing _WA_Sys_posted_7231DAC4
timeSheetAuditing _WA_Sys_DeptNo_7231DAC4
timeSheetAuditing _WA_Sys_JobNo_7231DAC4
timeSheetAuditing _WA_Sys_EmpID_7231DAC4
timeSheetAuditing _WA_Sys_UserID_7231DAC4
SELECT 'UPDATE STATISTICS ' + o.name + ' ' + i.name + ' ' AS comman
FROM sysindexes i JOIN Sysobjects o ON o.id = i.id
WHERE i.id IN (SELECT id FROM sysobjects WHERE Type = 'U' )
SQL Interview Questions 94
Now once we have the proper query, we will make a DTS package:
Open SQL Enterprise manager
Click on Data Transformation Services
Click on Local Packages
Right click and choose new package
Drag and drop Microsoft OLE DB Provider for SQL Server connection, and choose the appropriate
database in the connection property.
SQL Interview Questions 95
After this drag and drop the destination File. Destination file you will find on Top left window (it has a
file icon, with an arrow going towards left). After dropping it on the right pane, Right click on the icon to
get to property window of destination file name, type an existing file name.
After this click on Transform data task, and then click on Microsoft OLE DB icon and then click on
destination connection.
SQL Interview Questions 96
SQL Interview Questions 97
Once you will click on the destination connection, you will find an Arrow is formed between Source to
Destination. Now click on the arrow and choose property.
SQL Interview Questions 98
In the source tab, choose SQL Query and type the query as described above.
Click on the Destination Tab, and choose the txt file name if it is not already selected.
SQL Interview Questions 99
Now create another SQL task to run the scripts from the text file.
Drag and drop the SQL task from the left bar to design screen.
To get the automatic message from the package, drag and drop the Mail icon from the task pane to design.
Choose the property and fill up all the required email address and subject line etc.
In the procedure Tab, click on the New button and choose the source step and then choose precedence.
Click ok and you will see the final package as follows :
SQL Interview Questions 101
Q. How can you transfer data from a text file to a database table? Or
how can you export data from a table to a comma delimited (CSV)
file? Or how can you import data from MS Access to a table in a
database? Or how can you export data from a table to an Excel file?
A. To transfer data from a text file to a database table or vice versa use DTS.
Open SQL server enterprise manager, right click on a database, and choose All Tasks and then Import
task. Follow the instruction on the DTS wizard.
You should practice couple of scenarios before you go for an interview as it often happens that you may
asked to perform these tasks.
Q. When does the auto update index statistics feature in SQL server
turn itself on?
A. If the number of rows in a table are greater than 6, but less than or equal to 500, then statistics are
automatically updated when there have been 500 modifications made.
If the number of rows in the table is greater than 500, then updates are automatically made when (500
plus 20 percent of the number of rows in the table) has been modified.
For addtioinal information refer to http://support.microsoft.com/directory/article.asp?ID=KB;EN-
US;Q195565&.
A.First is Select into/bulk copy option should be activated on the database. You can reach to this option
by right clicking on the database and choosing property option.
Second, the destination table should be free of indexes.
SQL Interview Questions 103
Replication
Q. What is database replication? What are the different types of
replication you can set up in SQL Server?
A. Replication is the process of copying/moving data between databases on the same or different servers.
SQL Server supports the following types of replication scenarios:
Snapshot replication
Transactional replication (with immediate updating subscribers, with queued updating subscribers)
Merge replication
All these different kind of replications are very well defined in Books on line, please Refer to BOL for
further detail.
Q. Your company has 50 branches all over the country. All the
branches, including the Head Office have SQL Server as the
database. Every night all 50 branches upload certain information to
the Head Office. Which Replication topology is best suited for the
above scenario?
A. Central Subscriber is the best topology for this scenario. In a Central Subscriber scenario, a number of
Publishers replicate information into a common destination table at a Subscriber. The destination table is
partitioned horizontally and contains a location-specific column as part of the primary key. Each
Publisher replicates rows containing location-specific data.
This replication configuration may be useful, for example, for rolling up inventory data from a number of
servers at local warehouses into a central Subscriber at corporate headquarters. It could also be used to roll
up information from autonomous business divisions within a company, or to consolidate order processing
from dispersed locations.
Security
Q. A user is a member of the Public role and the Sales role. The Public
role has select permission on all the tables. The Sales role does not
have select permission on some of the tables. Will the user be able to
select from all tables?
A. No, the user will not be able to select information from tables. Permissions assigned to a role supercede
those assigned to the role Public. This is true whether permissions assigned to the role are more restrictive
or less restrictive than those assigned to the role Public.
TOC
Q. Tony works in the Sales Department and has created a table named
OrderDetail for the Sales department. You write a stored procedure
which will help Mark, a sales representative, update the OrderDetail
table. However, when Mark uses the stored procedure he gets an
error. You realize that this is a security issue. What is required to
enable Mark to use your stored procedure?
A. You must assign EXECUTE permission to Mark on your stored procedure, and Tony must assign
UPDATE permission on the Table. For tables and views, the owner can grant INSERT, UPDATE,
DELETE, SELECT, and REFERENCES permissions, or ALL permissions. A user must have INSERT,
UPDATE, DELETE, or SELECT permissions on a table before they can use it in any INSERT, UPDATE,
DELETE, or SELECT statements.
The owner of a stored procedure can grant EXECUTE permissions for the stored procedure. If the owner
of a base table wants to prevent users from accessing the table directly, they can grant permissions on
views or stored procedures referencing the table, but not grant any permissions on the table itself.
The REFERENCES permission lets the owner of another table use columns in your table as the target of a
REFERENCES FOREIGN KEY constraint from their table.
You can eliminate this problem by having dbo own all objects in a database. In that case, the only thing
you would need to have done was to grant Mark execute permissions to the stored procedure.
SQL Interview Questions 107
Transactions
Q. Define Concurrency Control.
A. Concurrency Control is the practice of defining controls so that multiple users can use and update data
in a database simultaneously. In a multi-user system, we require some kind of mechanism to protect one-
person change adversely affecting the others change. There are two main types of concurrency control,
Optimistic Concurrency Control, and Pessimistic Concurrency Control.
The default isolation level is READ COMMITTED. When the isolation level is specified, the locking
behavior for all SELECT statements in the SQL Server session operates at that isolation level and remains
in effect until the session terminates, or until the isolation level is set to another level.
USE pubs
GO
BEGIN TRANSACTION
SELECT au_lname FROM authors
Q. Explain Locks.
A. A lock is an object created by SQL Server to indicate that a user is currently using a resource. The lock
does not allow other users to perform operations on the resource that would adversely affect the first user
who owns the lock. Locks are managed internally by system software, are acquired, and released based on
actions taken by the user.
Microsoft SQL Server 2000 uses locks to implement concurrency control among multiple users in a
database at the same time. Transactions and locks are managed on a per connection basis.
SQL Server locks are applied at various levels of granularity in the database. Locks can be acquired on
rows, pages, keys, ranges of keys, indexes, tables, or databases. SQL Server dynamically determines the
appropriate level at which to place locks for each Transact-SQL statement.
The level at which locks are applied does not have to be specified by users and needs no configuration by
administrators.
Triggers
Q. What you can do to delete a table without the delete trigger firing?
A. TRUNCATE TABLE statement is not caught by a DELETE trigger. Although a TRUNCATE TABLE
statement is, in effect, like a DELETE without a WHERE clause (it removes all rows), it is not logged and
thus cannot execute a trigger. Since permission for the TRUNCATE TABLE statement defaults to the
table owner and is not transferable, only the table owner should be concerned about inadvertently
circumventing a DELETE trigger with a TRUNCATE TABLE statement.
INSTEAD OF triggers allow views that would normally not surpport updates to allow updates. A view
made up of multiple base tables must use an INSTEAD OF trigger to support inserts, updates and deletes
that reference data in the tables. Another advantage of INSTEAD OF triggers is that they allow you to
process data before updating the table. Lets explore this with an example
SET NOCOUNT ON
GO
USE tempdb
GO
GO
GO
CREATE TRIGGER ViewCustomer_INSERT ON ViewCustomer INSTEAD OF INSERT
AS
FROM inserted
INSERT CreditLimit SELECT SCOPE_IDENTITY() , creditlimit FROM
inserted
GO
TOC
SQL Interview Questions 113
Stored Procedures
Q. Why should one not prefix user stored procedures with sp_?
A. If the stored procedure does not exist in the master database, do not prefix your stored procedure with
sp_, as it causes a performance penalty. SQL server gives name resolution preference to the master
database for procedure that have sp_ suffix. Once the SQL fails to find the stored procedure in the
master database, it assumes that this procedure requires a recompilation as it is not in the cache, and
acquires an exclusive lock on the stored procedure for a short time. This lock causes the performance hit.
The exclusive lock is required because only one copy of a stored procedure is generally in the cache at any
time. This requires serialization of some parts of the compilation process, and this synchronization
process is accomplished in part through the use of compile locks.
Read more on http://support.microsoft.com/support/kb/articles/q263/8/89.asp
SQL Server Magazine InstantDoc #23011
EXEC Test1
GO
A. 1
2
All references to the name within the stored procedure are resolved against the temporary table created in
the procedure, not the version that existed before the procedure. Nested stored procedures can also create
temporary tables with the same name as a temporary table created by the stored procedure that called it.
All references to the table name in the nested stored procedure are resolved to the table created in the
nested procedure.
The following SET options are ON by default in SQL Server, and changing the state of these options in
your stored procedure will cause the stored procedure to recompile:
SET ANSI_DEFAULTS
SET ANSI_NULLS
SET ANSI_PADDING
SET ANSI_WARNINGS
SET CONCAT_NULL_YIELDS_NULL
Q. How can you find out which stored procedures are recompiling?
A. Use the SQL Profiler.
To determine if you have a problem with a specific stored procedure:
Start Profiler.
Start a new trace.
On the Events Tab, remove all default events and add SP:Recompile, SP:Starting, and SP:Completed
under Stored Procedure events. If you want to determine the statement that causes the recompile also add
SP:StmtStarting and SP:StmtCompleted to the selection.
Click on the filter tab, and add filter to reduce the clutter.
SQL Interview Questions 116
To determine the statement causing the recompile look at the statement immediately before and after the
recompile if you included SP:StmtStarting and SP:StmtCompleted in your trace.
SQL Interview Questions 117
Cursors
Q. How many types of cursor type are there?
A. ODBC, ADO, and DB-Library define four cursor types supported by Microsoft SQL Server2000.
The DECLARE CURSOR statement has been extended; thus you can specify the four cursor types for
Transact-SQL cursors.
The four API server cursor types supported by SQL Server are:
Static cursors
Dynamic cursors
Forward-only cursors
Keyset-driven cursors
Static cursors detect few or no changes but consume relatively few resources while scrolling, although
they store the entire cursor in tempdb. Dynamic cursors detect all changes but consume more resources
while scrolling, although they make the lightest use of tempdb. Keyset-driven cursors lie in between,
detecting most changes but at less expense than dynamic cursors.
Although the database API cursor models consider a forward-only cursor to be a distinct type of cursor,
SQL Server does not. SQL Server considers both forward only and scroll as options that can be applied to
static, keyset-driven, and dynamic cursors.
If OF column_list is supplied, only the columns listed will allow modifications. If no list is supplied, all
columns can be updated unless the cursor has been defined as READ ONLY.
SQL Interview Questions 119
Q. How can you know if the row fetched from cursor is still valid in
underlying table?
A. A global variable, @@FETCH_STATUS, will be updated at every execution of FETCH. At a
successful fetch, @@FETCH_STATUS will be set to 0. If no data was fetched because the requested
cursor position exceeded the results set, 1 will be returned. If the row returned is no longer a member of
the results set (if the row was deleted from the base table after the cursor was opened),
@@FETCH_STATUS will return 2.
Always use this to determine the validity of the data returned from a cursor fetch prior to attempting any
operation against that data.
Q. How can you find out how many rows returned in a cursor?
A. Use the @@CURSOR_ROWS function to determine how many rows have been returned.
Q. How can you set the threshold at which SQL Server will generate
keysets asynchronously?
A. Use the cursor threshold configuration option.
Datatypes
Q. Between Cast and Convert which function would you prefer and
why?
A. Both the functions are used to convert one data type to another. CAST is based on the SQL-92 standard
and is preferred over CONVERT. You may prefer to use convert, in situations where you want to convert
a datetime value to a specific style.
Q. What are the new data types are introduced in SQL 2000?
A. SQL Server 2000 introduces three new data types. bigint is an 8-byte integer type. sql_variant is a type
that allows the storage of data values of different data types. table is a type that allows applications to
store results temporarily for later use. It is supported for variables, and as the return type for user-defined
functions.
TOC
Q. You have to store user responses of Yes and No. What kind of
data type is best suited for this task?
A. The bit datatype is best suited to store Yes/No, True/False, and 1/0 values.
TOC
Q. Beginning with SQL Server Version 7.0, a new enhanced data type
nchar was added. What type of data is supported with this data type?
A. Unicode Data. Unicode data is stored using the nchar, nvarchar, and ntext data types in SQL Server.
Use these data types for columns that store characters from more than one character set.
XML in SQL
Q. What is XDR?
A. In Microsoft SQL Server 2000, the XML-Data Reduced (XDR) language is used to create the
schemas. The XDR is flexible and overcomes some of the limitations of the Document Type Definition
(DTD), which also describes the document structure. Unlike DTDs, XDR schemas describe the structure
of the document using the same syntax as the XML document. Additionally, in a DTD, all the data
contents are character data. XDR language schemas allow you to specify the data type of an element or an
attribute.
SQL Interview Questions 126
When you query views by using AUTO mode on the server-side, the view name is returned as the element
name in the resulting XML. For example, assume that the following view is created on the Employees
table in the Northwind database:
The following template specifies a query against the EmpView view and also specifies server-side XML
formatting:
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
</ROOT>
When you execute the template, the following XML is returned. (Only partial results are shown.) Note
that the element names are the names of the views against which the query is executed.
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<EmpView EID="1" FName="Nancy" LName="Smith" />
<EmpView EID="2" FName="Andrew" LName="Fuller" />
...
</ROOT>
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:query client-side-xml="1">
SELECT * FROM EmpView FOR XML NESTED
</sql:query>
</ROOT>
Executing this template produces the following XML. Note that the element name is the base table name
in this case.
SQL Interview Questions 127
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:query client-side-xml="0">
SELECT FirstName as fname,
LastName as lname
FROM Employees E
FOR XML AUTO
</sql:query>
</ROOT>
When you use the NESTED mode of the client-side FOR XML, the table names
are returned as element names in the resulting XML. (Table aliases that are
specified in the query are not used.) For example, consider this template:
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:query client-side-xml="1">
SELECT FirstName as fname,
LastName as lname
FROM Employees E
FOR XML NESTED
</sql:query>
</ROOT>
Q. What is the difference between FOR XML RAW and FOR XML
AUTO?
A. RAW: Takes the query result and transforms each row in the result set into an XML element with a
generic identifier <row /> as the element tag.
For e.g.
SELECT AppID, CommonName FROM Application for XML RAW
AUTO: Returns query results in a simple, nested XML tree. Each table in the FROM clause is represented
as an XML element. The columns listed in the SELECT clause are mapped to the appropriate element
attributes.
<Orders OrderID="10952"/>
</Customers>
The hierarchy in the result set is based on the order of tables identified by the columns specified in the
SELECT clause; therefore, the order in which column names are specified in the SELECT clause is
significant.
The tables are identified and nested in the order in which the column names are listed in the SELECT
clause. The first, leftmost table identified forms the top element in the resulting XML document. The
second leftmost table (identified by columns in the SELECT statement) forms a subelement within the top
element, and so on.
If query specifies the ELEMENT option, then an element-centric document is returned.
<Customers>
<CustomerID>ALFKI</CustomerID>
<ContactName>Maria Anders</ContactName>
<Orders><OrderID>10643</OrderID></Orders>
<Orders><OrderID>10692</OrderID></Orders>
<Orders><OrderID>10702</OrderID></Orders>
<Orders><OrderID>10835</OrderID></Orders>
<Orders><OrderID>10952</OrderID></Orders>
<Orders><OrderID>11011</OrderID></Orders>
</Customers>
When a column in the SELECT clause cannot be associated with any of the tables identified in the FROM
clause (say a computed column), the column is added in the XML document in the deepest nesting level in
place when it is encountered in the list. If such a column appears as the first column in the SELECT
clause, the column is added to the top element.
<Customers>
<CustomerID>ALFKI</CustomerID>
<ContactName>Maria Anders</ContactName>
<Orders>
<OrderID>10643</OrderID>
<ComputedColumn>69</ComputedColumn>
</Orders>
<Orders>
<OrderID>10692</OrderID>
<ComputedColumn>69</ComputedColumn>
</Orders>
</Customers>
SQL Interview Questions 130
SQL Interview Questions 131
<Results>
<Data>
<row id="ALFKI" name="Maria Anders" orderid="10643"/>
<row id="ALFKI" name="Maria Anders" orderid="10692"/>
<row id="ALFKI" name="Maria Anders" orderid="10702"/>
<row id="ALFKI" name="Maria Anders" orderid="10835"/>
<row id="ALFKI" name="Maria Anders" orderid="10952"/>
</Data>
</Results>
A.
SELECT '<Results><Data>'
SELECT 1 AS Tag,
NULL AS Parent,
dbo.Customers.CustomerID AS [row!1!id],
dbo.Customers.ContactName AS [row!1!name],
dbo.Orders.OrderID AS [row!1!orderid]
FROM dbo.Customers, dbo.Orders
WHERE dbo.Customers.CustomerID = dbo.Orders.CustomerID
FOR XML EXPLICIT
SELECT '</Data></Results>'
SQL Interview Questions 132
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-
schema">
...
</xsd:schema>
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<Customer CustomerID="ALFKI" ContactName="Maria Anders">
<Address>Obere Str. 57</Address>
</Customer>
</ROOT>
To do get the rows return in the above format you wrote a XDR Schema as follows:
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
You did not get the desired result. What is wrong with the above XDR Schema?
A. The content attribute is not specified on the <Address> subelement. Without the content=textOnly
attribute, the <Address> element does not map to the address column in the Customers table because, by
default, elements map to a table and not to a field. So the correct schema will be:
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
As an alternative, instead of specifying content=textOnly attribute, you can specify sql:field annotation to
map the <Address> subelement to the Address column:
SQL Interview Questions 134
Q. What is XPath?
A. XPath (XML Path Language) is a graph navigation language. XPath is used to select a set of nodes
from an XML document. Each XPath operator selects a node-set based on a node-set selected by a
previous XPath operator.
XPath language is defined by the W3C as a standard navigation language. The XPath language
specification, XML Path Language (XPath) version 1.0 W3C Proposed Recommendation 8 October 1999,
can be found at the W3C Web site at http://www.w3.org/TR/1999/PR-xpath-19991008.html. A subset of
this specification is implemented in SQL Server 2000.
SQL Interview Questions 135
Q. What keyword you will use to get schema appended to the result
set of a for XML query?
A.
<row CustomerID="ALFKI"/>
<row CustomerID="ANATR"/>
To get the schema appended to the result set, write the query as follows:
A. If ELEMENT key word is used, the query will return an element centric result set.
<Customers>
<CustomerID>ALFKI</CustomerID>
<ContactName>Maria Anders</ContactName>
<Orders><OrderID>10643</OrderID></Orders>
<Orders><OrderID>10692</OrderID></Orders>
<Orders><OrderID>10702</OrderID></Orders>
<Orders><OrderID>10835</OrderID></Orders>
<Orders><OrderID>10952</OrderID></Orders>
<Orders><OrderID>11011</OrderID></Orders>
</Customers>
SQL Interview Questions 137
Clustering
Q. Explain Active/Passive and Active/Active cluster configurations?
A. Active/Passive cluster configuration is a combination of two nodes, one called a primary node and
another called the secondary node. The primary node suporst the clients and the secondary node acts are
a failover backup node. An Active/Active cluster configuration is two simultaneous mirrored
active/passive configurations.
TOC
SQL Interview Questions 138
For outer joins, you should consider the *= operator obsolete and always use OUTER JOIN syntax. the *=
operator might be dropped entirely in future releases of SQL Server.
Always qualify the owner of an object when you reference it, it is always a good idea. You will
see a slight performance gain if you fully qualify the stored procedure name.
Clustered tables keep their data rows in order based on the clustered index key.
Avoid using auto-grow on transaction logs because it is easy for a transaction log to grow
uncontrolled from a bad application.
On a PC or a laptop autogrow should be restrict to leave 100- 50 MB of the hard disk space.
A file can be member of only one filegroup.
Log files are not considered to be part of any file group because they are managed separately.
A column that contains a limited number of unique values is a good candidate for a Clustered
Index.
No members are assigned to the Application role. The role is activated when the user runs the
application. The user inherits the permission assigned to the application role.
Neither system processes nor processes running extended stored procedures can be killed.
Members of the fixed server role sysadmin and those who access an SQL server via the sa login
account are mapped to the user account dbo in all databases. The dbo account is the owner of any
objects created by these users.
Members of the fixed database role db_owner can perform any tasks for the database in which
they have been assigned to this role. However, any objects they create will be owned by their user
account, not by the user account dbo. Members of the role db_owner can designate dbo as the
owner of objects they create. This is a good practice because users can then refer to the objects
without including the name of the owner in the object name.
The default database for a new login ID created in SQL Enterprise Manager will be the master
database if no default database is designated. The master database tracks information about SQL
Server in general and about each user database. It should not be the default database for users, so
it is a good practice to specify a default database for each user or group.
When no explicit mappings are defined between an SQL server and a linked server, the security
credentials of the current login are used. Thus, the current users Login ID and password will be
passed to the linked server.
To access any SQL services on the linked server, the user must have a valid Login ID on that
server or belong to a Windows NT group for which a Login ID has been defined.
The Login account SQLAgentCmdExec is used to provide the security context for jobs that are
owned by accounts that are not members of the sysadmin role.
The smallest value for the FILEGROWTH parameter is one extent. One extent contains eight
pages and each page is 8 KB in size.
SQL Interview Questions 139
A database extent consists of eight 8 KB pages. Different objects can now share an extent or an
object can have its own extent. A table and index both have a minimum of two pages.
The extended stored procedure xp_trace_setqueryhistory is used to enable the query history.
Those who belong to the db_ddladmin role can manage database objects.
The Transact-SQL statement sp_changegroup can be used to add a database User ID to a
database group. Since a User ID may belong only to one group other than public,
sp_changegroup also removes the user from any group other than public to which he or she is
assigned.
The Transact-SQL statement sp_addgroup allows a new group to be defined but cannot be used to
add Database User IDs to the group.
The Transact-SQL statement sp_addlogin can be used to create a Login ID and its associated
password as well as define the default database and default language for the Login ID. It cannot
be used to define the members of a database group.
The Transact-SQL statement sp_adduser can be used to assign a database User ID to a Login ID
and to indicate to which group the User ID should be assigned. This is used to define a new
database User ID, not to reassign an existing User ID to another group.
SQL 7.0 onward uses roles instead of groups, but the stored procedures for group management
are provided for backward compatibility.
Only the current database owner or a member of the sysadmin role can transfer ownership of a
database to another user. The new owner must have a valid login account and must NOT have a
user account for the database.
A database may be taken offline with the stored procedure sp_dboption. The Options tab of the
Database Properties dialog box in SQL Enterprise Manager does not provide this capability. This
option is often used with databases that are on removable media.
The database options dbo use only, select into / bulk copy and trunc. log on chkpt may all
be set on the Options tab of the Database Properties dialog box in SQL Enterprise Manager.
Setting a database to dbo use only allows active users to continue using the database, but no
new users other than anyone aliased as the DBO are allowed.
Setting the select into / bulk copy option turns off logging. This should be set when doing a fast
bulk copy (bcp).
Setting the trunc. log on chkpt option causes all committed transactions to be removed from the
transaction log file whenever a checkpoint is done. This prevents the log from filling up, but also
eliminates the capability of doing an incremental backup.
Members of the fixed server role securityadmin can manage login accounts for an SQL server.
Members of the fixed server role serveradmin can manage server configuration parameters.
Members of the fixed database role db_accessadmin can manage users and groups for a database.
The built-in account sa is a login account, not a role.
The default password for the sa login account is blank (no password). Always change this to a
secure password when SQL authentication is enabled.
Two databases may contain tables with the same name. A single database may also contain
tables with the same name as long as the owner is different.
SQL Interview Questions 140
With Windows ONLY NT Authentication, the users Windows NT username is used as that
individuals login ID for SQL. If the users login ID has been defined as an sa account, the user
will have sa privileges on the server. The sa login itself is not used if Windows NT
Authentication ONLY is configured, so it is not necessary to assign a password to the sa account.
A password could be assigned to the sa login ID, but it will be ignored with Windows NT
Authentication.
TOC
SQL Interview Questions 141
N S
non-clustered, 60 scroll, 115
Non-Clustered Index, 61 Second Normal Form, 25
normalize, 23 Security, 102
SERIALIZABLE, 104
O sp_changeobjectowner, 80
Static cursors, 115
one-to-many, 19
statistics, 64, 87
One-to-Many Relationships, 20
one-to-one, 19
T
One-to-One Relationships, 20
Optimistic Concurrency Control, 104 Third Normal Form, 26
outer join, 53 timestamp, 119
Transactions, 104
P Triggers, 107
page level lock, 106
U
parameterized views, 35
Pessimistic Concurrency Control, 104 unique, 17
PINTABLE, 49 user-defined function, 34
primary, 16
primary key, 16 V
view, 102
R
View, 71
RAID, 47
RAID5, 83 W
RAISEERROR, 35
What is a subquery, 21
READ COMMITTED, 104
WRITETEXT, 107
READ UNCOMMITTED, 104
referential integrity, 18
X
REPEATABLE READ, 104
Replication, 100 XDR, 122
Rollup, 36 XML Schema, 128
row lock, 106 XPath, 130
ROWGUIDCOL, 48