I use PostgreSQL WITH https://www.postgresql.org/docs/current/static/queries-with.html. And I can run pretty complicated query, but can I use already sorted data? For instance:
SELECT id, f_name, l_name, email FROM users
WHERE f_name = 'BabyBoy'
WITH i_need_do_thomething AS (
# and than use that filtered data
)
SELECT * FROM i_need_do_thomething
Thanks
UPDATE
All problems that I use this query in Ruby on Rails. And I use association like:
@laptop = Laptop.find(1)
@laptop.user.do_custom_sql(HERE_WILL_BE_RAW_SQL)
where @laptop.user is equal to
SELECT id, f_name, l_name, email FROM users
WHERE f_name = 'BabyBoy'