Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
   *, AS name, 
       SELECT ps.price 
       FROM product_special ps 
       WHERE =
         AND < NOW() 
       ORDER BY ps.priority ASC, LIMIT 1
   ) AS special_price,
       FROM product_special ps 
       WHERE =
         AND < NOW() 
       ORDER BY ps.priority ASC, LIMIT 1
   ) AS date
FROM product p LEFT JOIN product_special ps ON ( =

As you can see I'm repeating the same subquery just to get another column out. I'm wondering is there a better way of doing this?

id is the primary key in both tables. I've no problem making product_special.priority unique if that can help.

share|improve this question

Assuming combination, product_special.priority is unique

 SELECT p.*, special_price,special_date
 FROM product p
     SELECT, ps.price as special_price, ps.`date` as special_date
     FROM product_special ps
       SELECT id, MIN(priority) as min_priority 
       FROM product_special
       GROUP BY id
     ) ps2 
     ON ( =
 )a ON (
share|improve this answer

I think this is equivalent:

SELECT, p.image, p.price,

       ps.price AS special_price,

FROM product p 
  LEFT JOIN product_special ps 
    ON =
    AND < NOW()
share|improve this answer

unless you're intending to return the fields as special_price.price and why not alias the names inside the subquery? e.g.

SELECT p.*, AS  name, p.image, p.price, ps.*
FROM product p
      psi.price as special_price, as my_date 
    FROM product_special psi
    WHERE = AND < NOW()
    ORDER BY psi.priority ASC, LIMIT 1
   ) AS ps ON =

Does your query language have a FIRST() aggregate function? Not sure if you could make the PK of product_special a composite between id and priority (both ASC sort) and change the ORDER clause to GROUP BY id, psi.priority

you MIGHT be able to remove the ORDER BY clause entirely and use HAVING MIN(psi.priority)

share|improve this answer

Try the following SQL command:

FROM ProductSpecial ps
WHERE p.Id = ps.Id ORDER BY ps.priority )as pss
share|improve this answer
may you please add more information to your answer – Ahmad Abuhasna Apr 21 at 10:03
The code in question uses LIMIT and is not tagged with a DBMS (so it could be MySQL or Postgres or SQLite or possibly some other dbms). The code in the answer uses OUTER APPLY and TOP so it it will work in SQL Server only (and Sybase) which do not have LIMIT. – ypercubeᵀᴹ Apr 21 at 10:18
This one is applicable for sql server only for other databases we can use inner query within the select statement. – SANTOSH APPANA May 6 at 11:17

Note that the "cross apply" mechanism from SQL Server would solve this, but it isn't available in PostgreSQL. Basically, it was their solution for how to pass parameters (which tend to be references to columns external to the current table expression) to functions called as table expressions in the FROM clause. But it turned out to be useful for all kinds of situations where you want to avoid another level of subquery nesting or moving things from the FROM clause to the SELECT clause. PostgreSQL made it possible to do this by making kind of an exception -- you can pass parameters like that if the expression is a simple function call but not strictly speaking an embedded SELECT. So

left join highestPriorityProductSpecial( on true

is ok, but not

left join (select * from product_special ps where = order by priority desc limit 1) on true

even though the definition of the function is precisely that.

So, that is in fact a handy solution (in 9.1 at least): make a function to extract your highest priority row by doing the limit inside the function.

But functions have the drawback that the query plan will not show what is going on inside them and I believe it will always choose a nested loop join, even when that might not be best.

share|improve this answer
cross apply is available in Postgres starting with 9.3 (released in 2013) but they chose to adhere to the SQL standard and use the standard lateral operator. In your second query replace left join with left join lateral – a_horse_with_no_name Apr 21 at 10:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.