Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - using an alias in a where clause in a subquery

So this isn't actually my code, but just an example of what I'm trying to do. Ideally I'd be able to use INNER JOINS and foreign key relations to get data, but I can't in my real-life situation - this is just a simple example.

SELECT [EmployeeID],
       [DepartmentID],
       (SELECT Title FROM Depts WHERE ID = [DepartmentID]) AS Department, 
       (SELECT Name FROM DeptHeads WHERE DeptName = Department) AS DepartmentLead
FROM   Employees E

I'm getting data from one table (Employees).

I'm using one of the columns from that table (DepartmentID) in a where clause in a subquery, and creating an alias from that (Department)

I'm then trying to do the same thing as above, except using that alias in the where clause.

I get an error saying:

Invalid column name "Department"

Is there a better way for me to do this, or a way around this?

like image 819
Steven Avatar asked Aug 09 '13 16:08

Steven


People also ask

Can you use an alias in a WHERE clause?

The WHERE clause can contain non-correlated aliases and correlated aliases.

Can you use alias in WHERE clause SQL Server?

In PROC SQL, a column alias can be used in a WHERE clause, ON clause, GROUP BY clause, HAVING clause, or ORDER BY clause. In the ANSI SQL standard and ISO SQL standard, the value that is associated with a column alias does not need to be available until the ORDER BY clause is executed.

Can you use alias in subquery?

When using a self-join, it is important to use a logical SQL alias for each table. (Aliases are also useful for subqueries.

Can a subquery be used in a WHERE clause?

A subquery can be nested inside the WHERE or HAVING clause of an outer SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery.


3 Answers

You can't use aliases you just defined. You can:

SELECT * FROM (

    SELECT [EmployeeID],
               [DepartmentID],
               (SELECT Title FROM Depts WHERE ID = [DepartmentID]) AS Department, 
               (SELECT Name FROM DeptHeads WHERE DeptName = Department) AS DepartmentLead
    FROM   Employees E

) Base

WHERE Base.Department = ...
like image 96
xanatos Avatar answered Oct 17 '22 19:10

xanatos


;WITH MyCTE AS
(
    SELECT [EmployeeID],
       [DepartmentID],
       (SELECT Title FROM Depts WHERE ID = [DepartmentID]) AS Department, 
       (SELECT Name FROM DeptHeads WHERE DeptName = Department) AS DepartmentLead
    FROM   Employees E
)
SELECT *
FROM   MyCTE
WHERE  Department = 'IT'
like image 33
Giannis Paraskevopoulos Avatar answered Oct 17 '22 20:10

Giannis Paraskevopoulos


Method 1:

SELECT [EmployeeID],
[DepartmentID],
[Department],
(SELECT Name FROM DeptHeads WHERE DeptName = Department) AS DepartmentLead
FROM
(SELECT [EmployeeID],
       [DepartmentID],
       (SELECT Title FROM Depts WHERE ID = [DepartmentID]) AS Department, 
       FROM   Employees E ) E2

Using [Department] alias in where clause

SELECT [EmployeeID],
[DepartmentID],
[Department],
(SELECT Name FROM DeptHeads WHERE DeptName = Department) AS DepartmentLead
FROM
(SELECT [EmployeeID],
       [DepartmentID],
       (SELECT Title FROM Depts WHERE ID = [DepartmentID]) AS Department, 
       FROM   Employees E ) E2
WHERE E2.Department = 'XYZ'

Method 2:

SELECT E.[EmployeeID],
       E.[DepartmentID],
       D.Title AS Department, 
       DH.Name AS DepartmentLead
FROM   Employees E
LEFT JOIN Depts D ON E.[DepartmentID] = D.ID
LEFT JOIN DeptHeads DH ON D.Title = DH.DeptName
like image 21
Anup Agrawal Avatar answered Oct 17 '22 18:10

Anup Agrawal