It is currently Fri Jul 30, 2010 1:55 pm


All times are UTC [ DST ]


Ask on Codemiles community and get answers Free and Fast :

SQLServer codes,SQLServer tricks ,SQLServer faq ,SQLServer answers .and more

Our guest share with us your code snippets , your programming problems , your open source projects ,read articles and post yours .







Post new topic Reply to topic  [ 1 post ] 
  Print view Previous topic | Next topic 
Author Message
 Post subject: Step-4- in SQl-server2000
PostPosted: Sat Apr 07, 2007 7:16 pm 
Offline
Newbie
User avatar

Joined: Thu Apr 05, 2007 9:57 pm
Posts: 17
Has thanked: 0 time
Have thanks: 0 time

DML Statements(Subqueries)

A) Using Subquery in WHERE Clause

Code:
SELECT <column list>
FROM <table names>
WHERE expression operator
               ( SELECT <column list>
                                             FROM <table names>)

Example 1:

Get the names of the customers who have ordered at least one order

Code:
SELECT  Customers.ContactName
FROM Customers
WHERE Customers.CustomerID IN
   (
    SELECT Orders.CustomerID
    FROM Orders
   );


Example 2:

Get the supplier names of the suppliers who supply category number 3


Code:
SELECT Suppliers.ContactName
FROM Suppliers
WHERE  Suppliers.SupplierID IN
  (
   SELECT Products.SupplierID
   FROM Products
   WHERE Products.CategoryID=3
  );


Example 3:

Get the books titles priced higher than the lowest priced book that has a type 'trad_cook'


Code:
SELECT Title
FROM Titles
WHERE Price >
(
   SELECT MIN(Price)
   FROM Titles
   WHERE Type = 'trad_cook'
);




B) Using Subquery in FROM Clause ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط·آ¥أ¢â‚¬إ“Represents a tableط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â€ڑآ¬ط¥â€™

Code:
SELECT <column list>
FROM <table names>,        ( SELECT <column list>
                                        FROM <table names>)

[WHERE <condition>]


Example:

Get the names of all customers and employees living in USA , but with customers not working as Sales Agents

Code:
SELECT C. ContactName "Cust Name", E.FirstName + ' ' + E.LastName "Emp Name"
FROM Customers C ,
           (
                            SELECT FirstName,LastName
                            FROM Employees
                                   WHERE Country = 'USA'
                                 ) E
WHERE C.Country = 'USA'
AND C.ContactTitle <> 'Sales Agent';



C) Using Subquery in SELECT Clause ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط·آ¥أ¢â‚¬إ“Represents column(s)ط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â€ڑآ¬ط¥â€™

Code:
SELECT <column list> , ( SELECT <column list>
                                     FROM <table names>)

FROM <table names>      
[WHERE <condition>]


Example:

Get the name , job (title) and number of orderes requested by each customer

Code:
SELECT ContactName ,ContactTitle , (SELECT COUNT(*)
                                                               FROM Orders
                                                               WHERE Customers.CustomerId = Orders.CustomerId 
                           ) AS "Number of Orders"      
FROM Customers;



Note:

When using subquery in the where clause , take care when using operators ;
If the subquery returns more than one value you canط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â‚¬â€چط¢آ¢t use operator such as = , but you may use the IN operator.



TOP
 Profile Send private message  
 
| More
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


 Similar topics
 Topic title   Forum   Author   Replies 
 unit step and impulse function L.T  General Discussion  loomy  0
 (MS Press) Microsoft Visual CSharp 2008 - Step By Step  offers  Casper  0
 Cisco - Press - Configuring CallManager and Unity A Step-by-  offers  Casper  0
 TCP IP First Step  offers  Casper  0
 Cisco Press - Routing First-Step 2004  offers  Casper  0

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  





Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2009
mileX v1.0 designed by codemiles team