Note
- Using an asterisk (
*) in theSELECTclause is considered a bad programming practice. Always explicitly list all attributes you need. - A table has no guaranteed order of rows unless you specify an
ORDER BYclause. Think of it like a mathematical set with duplicates allowed. For example, if you use theTOPfilter without anORDER BYclause, the result is non-deterministic, and you may get different rows each time you run the query. Note that even if you useORDER BY, if there are ties in theORDER BYcolumns, the result is still non-deterministic. - T-SQL uses three-valued predicate logic, meaning that predicates can evaluate to
TRUE,FALSE, orUNKNOWN. When you compare anything toNULL, the result isUNKNOWN. - The following query is not valid.
SELECT orderid, YEAR(orderdate) AS orderyear, orderyear + 1 AS nextyear FROM Sales.Orders;- You cannot refer to a column alias in the same
SELECTclause where it was defined. - SQL evaluates all expressions in the same logical phase simultaneously, so the column alias
orderyearis not yet available whennextyearis being evaluated.
- You cannot refer to a column alias in the same
- When you use
SELECT DISTINCT, you are restricted in theORDER BYlist only to elements that appear in theSELECTlist.
Quiz
What is the order in which the query clauses are logically processed?
- FROM
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
What is the requirement for the GROUP BY clause?
- All expressions you specify in logical phases subsequent to the
GROUP BYphase are required to return a scalar value for each group.
What does sargable query mean?
- A query is
sargableif the database engine can efficiently utilize indexes. If a query isnon-sargable, the database is forced to read every single row in the table (a “full table scan”), which can lead to poor performance.
Why should you avoid using functions or applying manipulations on the columns in the WHERE clause?
- This is a classic example of a
non-sargablequery. When you apply a function to a column in theWHEREclause, the database engine cannot use any indexes on that column, and it has to perform a full table scan to evaluate the condition for each row. This can cause significant performance issues. - For example, the following query is not
sargable:SELECT transactionid, transactiondate FROM Finance.Transactions WHERE YEAR(transactiondate) = 2025 AND MONTH(transactiondate) = 5; - Instead, you can rewrite it to this
sargablequery:SELECT transactionid, transactiondate FROM Finance.Transactions WHERE transactiondate >= '2025-05-01' AND transactiondate < '2025-06-01';
SQL uses different criteria for evaluating expressions in terms of the three-valued logic. Explain.
- SQL only accepts
TRUEfor query filters and only rejectsFALSEforCHECKconstraints. - For example, the following query will return all rows where
isActiveisTRUE, but it will not return rows whereisActiveisNULL:SELECT * FROM Users WHERE isActive = TRUE; - Another example is, if you have
WHERE Id NOT IN (1, 2, NULL), you will always get zero rows because the condition evaluates toUNKNOWNfor all rows.
Why should you always use IS NULL for checking NULL values?
- When you compare anything to
NULLusing the equality operator (=), the result is alwaysUNKNOWN, even if you are comparingNULLtoNULL. Therefore, you should always use theIS NULLoperator to check forNULLvalues.
Does T-SQL consider two NULLs equal?
- T-SQL does not consider two
NULLvalues as equal for equality comparison purposes. However, for grouping and sorting purposes, T-SQL treatsNULLvalues as equal. This means that when you useGROUP BYorORDER BY, allNULLvalues will be grouped together and sorted together.
Explain the CASE expression.
- A
CASEexpression is a scalar expression. It is allowed whenever a scalar expression is allowed. - There are two types of
CASEexpressions: the simple and the searched. - The simple
CASEexpression compares an expression to a set of simple expressions to determine the result. The following is an example of a simpleCASEexpression:SELECT FirstName, LastName, DepartmentID, CASE DepartmentID WHEN 1 THEN 'Human Resources' WHEN 2 THEN 'Information Technology' WHEN 3 THEN 'Finance' ELSE 'Unknown Department' END AS DepartmentName FROM Employees; - The searched
CASEexpression evaluates a set of Boolean expressions to determine the result. The following is an example of a searchedCASEexpression:SELECT ProductName, Price, CASE WHEN Price = 0 OR Price IS NULL THEN 'N/A' WHEN Price < 100 THEN 'Under $100' WHEN Price >= 100 AND Price < 500 THEN 'Between $100 and $500' ELSE 'Over $500' END AS PriceCategory FROM Products; - Notice that for the simple
CASEexpression, theCASEkeyword is followed by an expression (DepartmentID), and eachWHENclause compares that expression to simple expressions. For the searchedCASEexpression, theCASEkeyword is not followed by an expression, and eachWHENclause contains a Boolean expression.
References
- T-SQL Fundamentals 4th Edition by Itzik Ben-Gan