SQL Technical INterview PDF
SQL Technical INterview PDF
+91-8553350742
Write a SQL query to display the average price of the items for each
company, showing only the company code.
Output :
Write a SQL query to find all the details of 1970 winners by the ordered to
subject and winner name; but the list contain the subject Economics and
Chemistry at last.
Output :
Which TCP/IP port does SQL Server run on? How can it be changed?
SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties.
1. A clustered index is a special type of index that reorders the way records in the table are
physically stored. Therefore table can have only one clustered index. The leaf nodes of a
clustered index contain the data pages.
2. A non clustered index is a special type of index in which the logical order of the index does not
match the physical stored order of the rows on disk. The leaf node of a non clustered index does
not consist of the data pages. Instead, the leaf nodes contain index rows.
In OLTP - online transaction processing systems relational database design use the discipline of
data modeling and generally follow the Codd rules of data normalization in order to ensure
absolute data integrity. Using these rules complex information is broken down into its most
simple structures (a table) where all of the individual atomic level elements relate to each other
and satisfy the normalization rules.
Both primary key and unique key enforces uniqueness of the column on which they are defined.
But by default primary key creates a clustered index on the column, where are unique creates a
nonclustered index by default. Another major difference is that, primary key doesn't allow
NULLs, but unique key allows one NULL only.
Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming
too large, you can filter them based on the information you want, so that only a subset of the
event data is collected. Monitoring too many events adds overhead to the server and the
monitoring process and can cause the trace file or trace table to grow very large, especially when
the monitoring process takes place over a long period of time.
What are the authentication modes in SQL Server? How can it be changed?
Windows mode and Mixed Mode - SQL and Windows. To change authentication mode in SQL
Server click Start, Programs, Microsoft SQL Server and click SQL Enterprise Manager to run
SQL Enterprise Manager from the Microsoft SQL Server program group. Select the server then
from the Tools menu select SQL Server Configuration Properties, and choose the Security page.
13. Which command using Query Analyzer will give you the version of SQL server and
operating system?
SQL Server agent plays an important role in the day-to-day tasks of a database administrator
(DBA). It is often overlooked as one of the main tools for SQL Server management. Its purpose is
to ease the implementation of tasks for the DBA, with its full- function scheduling engine, which
allows you to schedule your own jobs and scripts.
15. Can a stored procedure call itself or recursive stored procedure? How much level SP
nesting is possible?
Yes. Because Transact-SQL supports recursion, you can write stored procedures that call
themselves. Recursion can be defined as a method of problem solving wherein the solution is
arrived at by repetitively applying it to subsets of the problem. A common application of
recursive logic is to perform numeric computations that lend themselves to repetitive evaluation
by the same processing steps. Stored procedures are nested when one stored procedure calls
another or executes managed code by referencing a CLR routine, type, or aggregate. You can nest
stored procedures and managed code references up to 32 levels.
Log shipping is the process of automating the backup of database and transaction log files on a
production SQL server, and then restoring them onto a standby server. Enterprise Editions only
supports log shipping. In log shipping the transactional log file from one server is automatically
updated into the backup database on the other server. If one server fails, the other server will have
the same db and can be used this as the Disaster Recovery plan. The key feature of log shipping is
that it will automatically backup transaction logs throughout the day and automatically restore
them on the standby server at defined interval.
17. Name 3 ways to get an accurate count of the number of records in a table?
18. What does it mean to have QUOTED_IDENTIFIER ON? What are the implications of
having it OFF?
Disk Space: Indexes are stored on the disk, and the amount of space required will depend on the size
of the table, and the number and types of columns used in the index. Disk space is generally cheap
enough to trade for application performance, particularly when a database serves a large number of
users.
Insert, Update and Delete statements could be slow: Another downside to using an index is the
performance implication on data modification statements. Any time a query modifies the data in a
table (INSERT, UPDATE, or DELETE), the database needs to update all of the indexes where data
has changed. Indexing can help the database during data modification statements by allowing the
database to quickly locate the records to modify, however, providing too many indexes to update can
actually hurt the performance of data modifications. This leads to a delicate balancing act when tuning
the database for performance.
How many Clustered and Non Clustered Indexes can you have per table?
Clustered Index - Only one Clustered Index per table. A clustered index contains all of the data for a
table in the index, sorted by the index key. Phone Book is an example for Clustered Index.
Non Clustered Index - You can have multiple Non Clustered Indexes per table. Index at the back of
a book is an example for Non Clustered Index.
Which Index is faster, Clustered or Non Clustered Index?
Clustered Index is slightly faster than Non Clustered Index. This is because, when a Non Clustered
Index is used there is an extra look up from the Non Clustered Index to the table, to fetch the actual
rows.
When is it usually better to create a unique nonclustered index on the primary key column?
Sometimes it is better to use a unique nonclustered index on the primary key column, and place the
clustered index on a column used by more queries. For example, if the majority of searches are for the
price of a product instead of the primary key of a product, the clustered index could be more effective
if used on the price field.
1. Stored Procedure support deffered name resolution where as functions do not support deffered
name resolution.
2. User Defined Function can be used in a select statement where as you cannot use a stored
procedure in a select statement.
3. UDF's cannot return Image, Text where as a StoredProcedure can return any datatype.
4. In general User Defined Functions are used for computations where as Stored Procedures are
used for performing business logic.
6. User Defined Functions accept lesser number of input parameters than Stored Procedures.
UDF can have upto 1023 input parameters where as a Stored Procedure can have upto 21000 input
parameters.
7. Temporary Tables can not be used in a UDF where as a StoredProcedure can use Temporary
Tables.
8. UDF can not Execute Dynamic SQL where as a Stored Procedure can execute Dynamic SQL.
3. The ORDER BY clause is invalid in views unless TOP or FOR XML is also specified.
4. Views cannot be based on temporary tables.
You might have heard about self join, but self join is not a different type of join. A self join means
joining a table with itself. We can have an inner self join or outer self join. Read this sql server
interview question, to understand self join in a greater detail.
The following 2 SQL Server Interview questions were asked when I attended an interview for SQL
Server Developer role.
Can you list a few useful string manipulation functions in SQL Server?
LEN(), SUBSTRING(), CHARINDEX(), LEFT(), RIGHT() etc.
Then he asked me, Can you give me one example of where you have used these functions in your
experience?
The following is one simple real time example, where we can use LEN(),
CHARINDEX() and SUBSTRING() functions. Let us assume we have table as shown below.
I want you to write a query to find out total number of emails, by domain. The result of the query
should be as shown below.
We can use LEN(), CHARINDEX() and SUBSTRING() functions to produce the desired results.
Please refer to the query below.
Select SUBSTRING(Email,CHARINDEX('@',Email)+1,(LEN(Email) -
CHARINDEX('@',Email))) as EmailDomain, Count(*) as Total
From TableName
Group By SUBSTRING(Email,CHARINDEX('@',Email)+1,(LEN(Email) -
CHARINDEX('@',Email)))
Order by Count(*) Desc
There could be even better ways of producing the same result. If you feel you have a better way of
producing the same output, please share using the form below.