all groups > sql server dts > january 2007 >
You're in the

sql server dts

group:

Subqueries in SQL Server


Subqueries in SQL Server sangu_rao NO[at]SPAM yahoo.co.in
1/11/2007 9:03:27 PM
sql server dts:
Hi,
Is there any similar statement in SQL Server for the below Oracle query

SELECT ENAME FROM (SELECT ENAME FROM EMP WHERE DEPTNO = 10)
WHERE SUBSTR(ENAME,1,4) = 'SAHU';

The above one is a Oracle query, i want to know is there any equivalent
query in SQL server.
I don't want to write the query as

SELECT ENAME FROM EMP WHERE DEPTNO = 10 AND SUBSTR(ENAME,1,4) = 'SAHU';

Please provide your comments.

Thanks
Rao
Re: Subqueries in SQL Server Dejan Sarka
1/12/2007 8:07:49 AM
[quoted text, click to view]

Yes, these are called derived tables in SQL Server. Actually, if you change
the SUBSTR function to SUBSTRING, i think your query should work in SQL
Server.
In addition, SQL Server 2005 has also subqueries in the WITH prolog of the
SELECT statement called Common Table Expressions. They can be more
eefficient that derived tables, if you have to refer to the same subquery
more than once.

--
Dejan Sarka
http://www.solidqualitylearning.com/blogs/

Re: Subqueries in SQL Server Peter W. DeBetta
1/12/2007 9:55:24 AM
SELECT ENAME
FROM EMP
WHERE DEPTNO = 10
AND SUBSTRING(ENAME,1,4) = 'SAHU';

Why wouldn't you write the query like this? For all intents and purposes, it
is the same as you nested query (in both ORacle and SQL Server), but easier
to read, IMHO.

--
Peter DeBetta, MVP - SQL Server
http://sqlblog.com
--
[quoted text, click to view]

AddThis Social Bookmark Button