SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 subqueries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

anhamade
Starting Member

7 Posts

Posted - 03/01/2014 :  23:35:35  Show Profile  Reply with Quote
i have to change this problem to subquery. this is what have so far and my question is how do i include the customers subquery.


what i have so far

USE northwind
SELECT max(OrderID) as OrderID
FROM Orders
where EmployeeID in
(select EmployeeID
from Employees
where LastName = 'Peacock'
)


Question problem

USE northwind
SELECT max(OrderID) as OrderID
FROM Orders join Employees
on Orders.EmployeeID = Employees.EmployeeID
join Customers
on Orders.CustomerID = Customers.CustomerID
WHERE Employees.LastName = 'Peacock'
and
Customers.Phone = '(5) 555-3932'

khtan
In (Som, Ni, Yak)

Singapore
17681 Posts

Posted - 03/02/2014 :  00:56:07  Show Profile  Reply with Quote

SELECT max(OrderID) as OrderID -- change this line to sub query
FROM  Orders Employees
where EmployeeID in
(select EmployeeID
from Employees
where LastName = 'Peacock' 



KH
Time is always against us

Go to Top of Page

anhamade
Starting Member

7 Posts

Posted - 03/02/2014 :  01:31:25  Show Profile  Reply with Quote
but i have to keep from as orders. this is the full question.

Query #3: Please run the following query: (you should get “10535”)

USE Cis11102_Northwind
SELECT max(OrderID) as OrderID
FROM Orders join Employees
on Orders.EmployeeID = Employees.EmployeeID
join Customers
on Orders.CustomerID = Customers.CustomerID
WHERE Employees.LastName = 'Peacock'
and
Customers.Phone = '(5) 555-3932'

Using ONLY the “Orders” table in the “FROM” clause, create a new query, using sub queries, that will
return the same result.


quote:
Originally posted by khtan


SELECT max(OrderID) as OrderID -- change this line to sub query
FROM  Orders Employees
where EmployeeID in
(select EmployeeID
from Employees
where LastName = 'Peacock' 



KH
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17681 Posts

Posted - 03/02/2014 :  03:05:32  Show Profile  Reply with Quote
oh i see. That your original query should be fine

just add another condition to the WHERE for the Customer sub query

WHERE EmployeeID in ( ... )
AND   CustomerID in ( ... )


KH
Time is always against us


Edited by - khtan on 03/02/2014 03:07:53
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/02/2014 :  09:18:32  Show Profile  Reply with Quote
this is another way using subquery

USE [Cis11102_Northwind]
GO 
SELECT max(OrderID) as OrderID 
FROM Orders join Employees 
on Orders.EmployeeID = Employees.EmployeeID 
WHERE EXISTS (SELECT 1 
             FROM Customers 
             WHERE Orders.CustomerID = CustomerID 
             AND Phone = '(5) 555-3932' 
            )
AND Employees.LastName = 'Peacock' 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000