Gomaa Intelligence.Data Hub
عربي

Foundations › SQL › SQL Basics

🔎 SQL Basics

ORDER BY, LIMIT and DISTINCT

Lesson 7 / 14· 7 min

ORDER BY sorts the result, LIMIT caps the rows and DISTINCT removes duplicates.

▶ Try it yourself

The 5 most expensive products. DESC is descending, ASC ascending (the default). Open in SQL Lab

▶ Try it yourself

Sorting by several columns: category first, then price. Open in SQL Lab

▶ Try it yourself

The distinct cities with no repeats. Open in SQL Lab

▶ Try it yourself

OFFSET skips rows and is used for paging. Open in SQL Lab

For advanced readers NULLs and performance

NULL ordering differs: SQLite and MySQL treat NULL as smallest; PostgreSQL and Oracle put NULLs last by default and let you set NULLS FIRST/LAST. Large OFFSET is slow because rows are read then discarded; for deep paging use keyset paging (WHERE id > last id).

Exercise 1 — Show the name and price of the 3 most expensive products.

Exercise 2 — Show the distinct customer cities in alphabetical order.

Exercise 3 — Show id and order_date of the latest 5 orders by date, ties broken by id descending.

Summary
  • ORDER BY sorts; DESC is descending.
  • LIMIT and OFFSET cap rows and page.
  • DISTINCT removes duplicates.

Want an expert to do it for you?

Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.

Get your data analyzed by an expert ↗