I have a table called Item which has an ID, a Name and a Price.
Is it possible using SQL select statements to get every possible, but distinct combination of items below a specific price?
For example, assume this table:
ID Name Price
-- ---- -----
1 A 1
2 B 2
3 C 3
4 D 4
5 E 5
The query, taken the limit is for example 10, shall only return A, B, C, D, but not A, B, D, C additionally.
Is something like this even possible? Please excuse this probably stupid question, but I’m learning SQL for a year now, but our teacher hasn’t even explained what SQL means. My entire knowledge is from books, so I’m not sure if this is a suitable question or not.
It’s a good question, but as far as I know it’s not possible with a set-based approach. It might be possible with some sort of CTE-based recursion, but if so, I can’t think of how. It could be done with cursors, but not directly with “SQL select statements” (which I interpret to mean a set-based approach).
If you wanted to find all combinations of exactly two numbers that were less then x, you could cross join the table to itself (eliminating rows where the ID was the same) and sum the two prices, excluding sums that were less than x.