Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE TABLE Orders (ID INT); INSERT INTO Orders VALUES (1), (2), (3), (4), (5); CREATE TABLE OrdersProducts (ID INT IDENTITY(1, 1), OrderID INT, Code INT); INSERT INTO OrdersProducts (OrderID, Code ) VALUES (1, 100), (2, 200), (3, 300), (4, 400), (5, 500), (1, 110), (2, 220), (3, 330), (4, 440), (5, 550); WITH Ranked AS ( SELECT OP.*, DENSE_RANK() OVER (ORDER BY O.ID DESC) R FROM Orders O JOIN OrdersProducts OP ON O.Id = OP.OrderID ) SELECT * FROM Ranked WHERE R <= 3;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear