8 Essential SQL Server Interview Questions *

最好的SQL Server开发人员和工程师可以回答的全面来源的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top SQL Server Developer Now
Toptal logois an exclusive network of the top freelance software developers, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

从an中获取名字“John”中字母“o”的位置的SQL Server命令是什么 Employee table

View answer

Select CHARINDEX(' 0 ',FIRST_NAME,0) from employee where FIRST_NAME ='John'

CHARINDEX is the SQL Server Equivalent of INSTR in Oracle.

2.

Is the following a valid SQL query? Why or why not?

选择前5年(BillingDate)作为BillingYear, COUNT(*)作为numberoffinvoices
FROM Invoices
WHERE CustomerId = 42
GROUP BY YEAR(BillingDate)
HAVING COUNT(*) > 1
ORDER BY BillingYear;

As part of your answer, 按照SQL Server逻辑处理的顺序列出该语句的短语.

View answer

有人可能会认为这个SQL语句无效,因为它使用了别名 BillingYear in the ORDER BY phrase. Even though BillingYear cannot be used in the WHERE or GROUP BY phrases, it can be used in the ORDER BY phrase.

按照处理的逻辑顺序列出短语有助于清楚地解释为什么会这样:

FROM Invoices
WHERE CustomerId = 42
GROUP BY YEAR(BillingDate)
HAVING COUNT(*) > 1
选择YEAR(BillingDate)作为BillingYear, COUNT(*)作为numberoffinvoices
ORDER BY BillingYear
TOP 5

Although the reordered statement is not valid T-SQL syntax, 对于候选人来说,能够在头脑中重新排序语句是至关重要的,这样他们就可以在编写复杂查询时解决遇到的任何问题. 大多数T-SQL程序员从不需要显式地记住这个处理顺序. There is a logic to it that becomes very familiar with experience, 所以这个问题是用来衡量候选人的经验的.

3.

How does SQL Server clear up orphaned connections?

View answer

It doesn’t. SQL Server永远不会终止连接,除非被明确告知这样做.e., by a user, by a KILL command, 或者由操作系统通知它网络连接已断开).

操作系统终止网络连接所需的时间长度可能差别很大. It largely depends on the net-lib and network protocol used.

Typically, named pipe connections over NetBEUI will time out quickly. Named pipes over IP will time out almost as quickly as well. 如果您使用的是TCP/IP套接字,那么这些会话在默认情况下根本不会超时.

有关排除SQL Server中孤立连接的更多信息,请参阅 here.

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance SQL Server Developer Jobs

Apply as a Freelancer
4.

How can SQL Server be configured to listen on other Net Libraries? How can you tell what Net Libraries are being used?

View answer

For SQL 7.0 and above, 安装在SQL Server程序组中的“服务器网络实用程序”告诉您安装了哪些网络库,并允许您配置新库.

For SQL 6.5 and earlier, 运行SQL setup并选择Configure Server,然后选择Network Support来访问此功能.

For all versions of SQL Server, 您可以查看SQL错误日志中的“监听”行,这些行告诉您正在使用哪些Net库.

5.

Can records be deleted from a View in SQL Server?

View answer

It depends.

There are two types of Views in SQL Server. One is a “simple” view that contains data from one table only, 另一种是包含来自多个表的数据的“复杂”视图.

在简单视图中可以对记录执行删除操作,但在复杂视图中不能执行删除操作.

6.

在SQL Server的SSIS合并连接和查找组件之间的区别是什么?

View answer

Merge Join和Lookup组件的使用取决于两个表之间的数据关系类型, i.e. a one-to-one relationship or a one-to-many relationship.

Merge Join用于连接来自两个或多个数据源的数据,并将合并的数据插入到一个非规范化的目标数据源中. Merge Join组件接受两个输入源,合并后的数据被传输到单个目标数据输出.

Lookup组件用于SSIS中,其中一个输入记录在目标源中有一个匹配的记录. Both the source and target satisfy the one-to-one relation.

7.

When should a developer use the NOLOCK hint? 使用此提示时可能发生什么问题以及如何解决这些问题?

View answer

NOLOCK 通过在SQL读取/处理行时不锁定行来提高性能. 应该在访问大型表和包含很少更改数据的表时使用它.

如果创建的锁太多,可以将行级锁升级为块级锁,然后再升级为表级锁. 表锁(尤其是核心表上的表锁)将暂停该数据库实例上的大多数处理,直到SQL操作完成.

NOLOCK 允许查询访问已提交和未提交的数据,并可能返回脏数据. 如果系统没有编码来处理这种情况,脏数据可能会导致数据完整性问题.

Data integrity issues can be avoided by adding a TIMESTAMP column and referencing it in UPDATE and DELETE statements. 这可以防止在查询读取脏数据时发生数据更改. 这需要更多的代码来确定没有发生更新或删除,然后采取适当的操作来恢复. Comparing all original values of every column in a row on every UPDATE or DELETE may be performed instead of verifying the TIMESTAMP column has not changed.

8.

Considering the database schema displayed in the diagram below, 编写一个SQL查询,列出所有发票,发给发票数量最多的客户. 请注意,大多数发票可能绑定了多个客户, in which case the invoices for all of them should be listed.

View answer

这里的重点是看候选人是否能够将一个复杂的查询分解成更简单的部分. 第一步是找出发票数量最多的客户是谁, taking into account there may be a tie:

SELECT TOP 1 WITH TIES CustomerId
FROM Invoices
GROUP BY CustomerId
ORDER BY COUNT(*) DESC;

现在我们知道了谁是“赢家”,我们可以查询该客户的订单. 为了通过ID查询客户列表的订单,我们可以使用In操作符:

SELECT *
FROM Invoices
WHERE CustomerId IN (…);

现在将第一个查询作为子查询插入到第二个查询中,我们得到:

SELECT * FROM Invoices
WHERE CustomerId IN (
 SELECT TOP 1 WITH TIES CustomerId
 FROM Invoices
 GROUP BY CustomerId
 ORDER BY COUNT(*) DESC
);

极其复杂的查询可以以这种方式分解为子查询, then used as building blocks to produce the final query.

There is more to interviewing than tricky technical questions, so these are intended merely as a guide. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top SQL Server Developer Now

Our Exclusive Network of SQL Server Developers

Looking to land a job as a SQL Server Developer?

Let Toptal find the right job for you.

Apply as a SQL Server Developer

Job Opportunities From Our Network

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for SQL Server Developers?

Looking for SQL Server Developers? Check out Toptal’s SQL Server developers.

Michael Kokorin

Freelance SQL Server Developer

GeorgiaToptal Member Since April 18, 2014

Michael是可伸缩和容错流程自动化系统的顶级架构师和开发人员. He has implemented numerous distributed and scalable systems. Michael擅长构建多平台、高性能的系统. Also, he has deep knowledge of .NET和Angular,以及许多其他广泛使用的技术和库.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more