M
M
Mikhail2018-02-24 14:37:49
MySQL
Mikhail, 2018-02-24 14:37:49

How to write a subquery?

Hello. Found the following problem on Habré:
There are three tables: CUSTOMERS (ID, NAME, MANAGER_ID); MANAGERS (ID, NAME); ORDERS (ID, DATE, AMOUNT, CUSTOMER_ID). Write a query that will display the names of Customers and their SalesManagers who have made purchases totaling more than 10,000 since 01/01/2013.
As I understand it, here it is necessary to do through subqueries. I made a query to select all purchase ids:

SELECT id FROM Orders WHERE Amount > 10000 AND Date > 01.01.2013

But I don’t understand how I can correctly insert these id into the selection now:
SELECT Customers.Name, Managers.Name FROM Customers, Managers
WHERE (??)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
latush, 2018-02-24
@latush

SELECT Customers.Name, Managers.Name FROM Customers, Managers, Orders
WHERE Orders.id IN (SELECT id FROM Orders WHERE Amount > 10000 AND Date > 01.01.2013)

In general, read about JOIN
PS. And, no, some kind of nonsense turns out. Better read about JOIN)
something like this
SELECT c.Name, m.Name
FROM Orders o 
JOIN Customers c 
ON c.id=o.CUSTOMER_ID
JOIN Managers m
OM m.id = c.MANAGER_ID
WHERE o.Amount>10000 and o.Date > 01.01.2013

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question