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 |