DF070: Not aliased table sources are used in the query.

The topic describes the DF070 T-SQL code analysis rule.

Category

BEST PRACTICE

Message

Not aliased table sources are used in the query.

Description

Always use aliases for table sources in queries to improve readability and avoid ambiguity, especially in complex queries or joins.

Additional information

Aliasing table sources in T-SQL means assigning a short name (alias) to a table or view referenced in a query. This practice helps make complex queries easier to read and maintain, especially when working with multiple tables or when the same table appears more than once (as in self-joins or subqueries).

Noncompliant code example

-- Long table names make it difficult to read column names, 
-- and without table names it is unclear where the column comes from.

SELECT
  Person.FirstName
, Person.MiddleName
, Person.LastName
, EmailAddress.EmailAddress
, Person.NameStyle
, PersonPhone.PhoneNumber
, Name
, Title
FROM Person.EmailAddress
  INNER JOIN Person.Person
    ON EmailAddress.BusinessEntityID = Person.BusinessEntityID
  INNER JOIN Person.PersonPhone
    ON PersonPhone.BusinessEntityID = Person.BusinessEntityID
  INNER JOIN Person.PhoneNumberType
    ON PersonPhone.PhoneNumberTypeID = PhoneNumberType.PhoneNumberTypeID
ORDER BY
  Person.FirstName
, Person.MiddleName
GO

Compliant solution

SELECT
  p.FirstName
, p.MiddleName
, p.LastName
, ea.EmailAddress
, p.NameStyle
, pp.PhoneNumber
, pnt.Name
, p.Title
FROM Person.EmailAddress ea
  INNER JOIN Person.Person p
    ON ea.BusinessEntityID = p.BusinessEntityID
  INNER JOIN Person.PersonPhone pp
    ON p.BusinessEntityID = pph.BusinessEntityID
  INNER JOIN Person.PhoneNumberType pnt
    ON pp.PhoneNumberTypeID = pnt.PhoneNumberTypeID
ORDER BY
  p.FirstName
, p.MiddleName
GO