SQLUSA

Microsoft SQL Server 2005 Best Practices

Microsoft SQL Server 2000 Best Practices

How to construct a correlated derived table query?

 

The following sample illustrates the derived table in the select list which is also a correlated subquery:

USE pubs

SELECT EmployeeID=emp_id, FirstName=fname,
LastName=lname, JobID=job_id,
(SELECT COUNT(*) FROM employee e2
WHERE e2.lname <= e1.lname AND e2.job_id = 7) AS SeqNo
FROM employee e1
WHERE job_id = 7
ORDER BY LastName

 

The Best SQL Server Training in the World
The future is just a click away! Your Future!
SQLUSA.com Home Page