Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 subqueries

Author  Topic 

anhamade
Starting Member

7 Posts

Posted - 2014-03-01 : 23:35:35
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)

17689 Posts

Posted - 2014-03-02 : 00:56:07
[code]
SELECT max(OrderID) as OrderID -- change this line to sub query
FROM Orders Employees
where EmployeeID in
(select EmployeeID
from Employees

where LastName = 'Peacock'
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

anhamade
Starting Member

7 Posts

Posted - 2014-03-02 : 01:31:25
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
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-02 : 03:05:32
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-03-02 : 09:18:32
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
   

- Advertisement -