SQL Interview: 78 SELECT *

SQL Interview: 78 SELECT *

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

You use a tool that does static code analysis of your T-SQL code.

It identifies the following predicate as an issue:

WHERE EXISTS 
(
    SELECT * 
    FROM dbo.Products AS p 
    WHERE p.ProductID = c.ProductID
)

Is this an issue?

How could you change the code to satisfy the code analyzer?

Answer:

That is not an issue. For SQL Server, the SELECT * in an EXISTS clause does not do anything with all the columns. There is a potential for a negligible difference in parsing time, but it’s really not relevant.

To avoid the issue with the code analyzer, you could change the code as follows:

WHERE EXISTS 
(
    SELECT 1 
    FROM dbo.Products AS p 
    WHERE p.ProductID = c.ProductID
)

2025-09-13