Home

7: Sorting Records

The Basics of Sorting

-- Ascending SELECT * FROM products ORDER BY price; -- Descending SELECT * FROM products ORDER BY price DESC;

Two Variations on Sorting

We can also sort by alphabetical order.

-- Sort in alphabetical order SELECT * FROM products ORDER BY name;

You can also order by multiple different properties in order of declaration.

SELECT * FROM products ORDER BY price, weight;

Offset and Limit

  • OFFSET skips the first N records.
  • LIMIT only give first N records of a result.

SELECT * FROM products ORDER BY price, weight OFFSET 2 LIMIT 3;

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/postgresql/SQL-And-PostgreSQL-The-Complete-Developers-Guide/7-Sorting-Records

Sections


Related