Skip to content
Advertisement

Query returned values in SQL

I would like to write an SQL query which uses results from previous one.

For example – I have a table Orders with fields: order_id, date and value. I need to get all dates from column date where value is larger than 5:

SELECT date 
FROM Orders 
WHERE value > 5;

Then I need to return all values for dates, which are +2 days from the returned ones. Is it possible to write short query without using LOOP statement?

Here is an example table:

enter image description here

I am expecting to get the result:

enter image description here

Advertisement

Answer

his will give you what you need.

select date, value  
from Orders where date in
(
  SELECT date + INTERVAL 5 DAY as date
  FROM Orders 
  WHERE value > 100;
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement