Use LIMIT on two tables with an Inner Join

Question:

The following SQL code works fine. It has the purpose of displaying all the products ordered by each user and sorting them by products in a descending manner.

The problems occur when I try to limit the display of customers and their related products, so when i use LIMIT 2. I tried to insert LIMIT 2 immediately after the ORDER BY line, but there is a problem: I only display one customer with its 2 purchases, so in total I only have 2 lines printed.

cursor.execute('''SELECT Client.client_name, Purchases.product 
                  FROM Client
                  INNER JOIN Purchases 
                  ON Client.client_name = Purchases.client_name
                  ORDER BY client_name;''')

I would like to print 2 lines for each customer. Where and how should I enter LIMIT 2? Can you show me please? (trying not to write a too complicated sql, because i understand little of Sql and i would like to try to keep the code as simple as possible)

In the case of the example below, I would like to print James, George, Sophie and get this output:

client_name | product   |
------------+-----------+
James      | Hard Disk |
James      | Mouse     |
George     | Book      |
George     | Keyboard  |  
Sophie     | Mouse     |
Sophie     | Coffee    |

The two main tables I’m using are these:

CLIENT


client_name |  born  |
-----------+---------+
James      |   1988  |
George     |   1988  |
Sophie     |   1988  |

PURCHASES

client_name | product   |
------------+-----------+
James      | Hard Disk |
James      | Mouse     |
George     | Book      |
George     | Keyboard  |  
Sophie     | Mouse     |
Sophie     | Coffee    |
Harry      | Cd        |
Harry      | Book      |
Lily       | Mouse     |
Lily       | Desk      |
Asked By: Rob92

||

Answers:

You can use the row_number window function to count assign a number to each product per client, and then have a condition on it:

SELECT   client_name, product
FROM     (SELECT     Client.client_name, 
                     Purchases.product,
                     ROW_NUMBER() OVER (PARTITION BY Client.client_name 
                                        ORDER BY Purchases.product DESC) AS rn
          FROM       Client
          INNER JOIN Purchases  ON Client.client_name = Purchases.client_name) t
WHERE    rn <= 2
ORDER BY client_name ASC
Answered By: Mureinik