Is the following a valid SQL query? Why or why not?
SELECT TOP 5 YEAR(BillingDate) AS BillingYear, COUNT(*) AS NumberOfInvoices FROM Invoices WHERE CustomerId = 42 GROUP BY YEAR(BillingDate) HAVING COUNT(*) > 1 ORDER BY BillingYear;
As part of your answer, list the phrases of this statement in the order that SQL Server logically processes them.
One might think that this SQL statement is invalid since it uses the alias
BillingYear in the
ORDER BY phrase. Even though BillingYear cannot be used in the
GROUP BY phrases, it can be used in the
ORDER BY phrase.
Listing the phrases in the logical order in which they are processed helps make clear why this is so:
FROM Invoices WHERE CustomerId = 42 GROUP BY YEAR(BillingDate) HAVING COUNT(*) > 1 SELECT YEAR(BillingDate) AS BillingYear, COUNT(*) AS NumberOfInvoices ORDER BY BillingYear TOP 5
Although the reordered statement is not valid T-SQL syntax, it is critical for candidates to be able to reorder the statement in their heads so they can work out any problems they are having when composing complex queries. Most T-SQL programmers never need to explicitly memorize this processing order. There is a logic to it that becomes very familiar with experience, so this question is designed to gauge that experience in a candidate.
Considering the database schema displayed in the diagram below, write a SQL query which lists all invoices billed to the customer who has the highest number of invoices. Note that there may be more than one customer tied for most invoices, in which case the invoices for all of them should be listed.
The point here is to see if the candidate can break down a complex query into simpler parts. The first step is to find out who the customer with the highest number of invoices is, taking into account there may be a tie:
SELECT TOP 1 WITH TIES CustomerId FROM Invoices GROUP BY CustomerId ORDER BY COUNT(*) DESC;
Now that we know who the “winner(s)” are, we can query for that customer(s) orders. In order to query the orders of a list of customers we know by ID, we can use the IN operator:
SELECT * FROM Invoices WHERE CustomerId IN (…);
So now inserting the first query as a subquery into the second one, we get:
SELECT * FROM Invoices WHERE CustomerId IN ( SELECT TOP 1 WITH TIES CustomerId FROM Invoices GROUP BY CustomerId ORDER BY COUNT(*) DESC );
Extremely complicated queries can be broken down into subqueries in this fashion, then used as building blocks to produce the final query.
How does SQL Server clear up orphaned connections?
It doesn’t. SQL Server never terminates a connection unless explicitly told to do so (i.e., by a user, by a KILL command, or by the operating system informing it that the network connection has been disconnected).
The length of time it takes for the operating system to kill a network connection can vary greatly. 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. If you’re using TCP/IP sockets, those sessions don’t time out at all by default.
More information on troubleshooting orphaned connections in SQL Server is available here.
Hiring a SQL Server developer? Toptal connects you with the world’s top SQL Server talent.Meet Our Developers
How can SQL Server be configured to listen on other Net Libraries? How can you tell what Net Libraries are being used?
For SQL 7.0 and above, the “Server Network Utility” installed in the SQL Server program group tells you what Net Libraries are installed and allows you to configure new ones.
For SQL 6.5 and earlier, run SQL setup and choose Configure Server and then Network Support to access this functionality.
For all versions of SQL Server, you can check the SQL errorlog for “listening on” lines that tell you what Net Libraries are being used.