SELECT top

SELECT TOP klauzasi, qaytarilishi kerak bo'lgan yozuvlar sonini belgilash uchun ishlatiladi.

SU

SQL TOP, LIMIT, FETCH FIRST yoki ROWNUM Klauzalari

SQL SELECT TOP Klauzasi SELECT TOP klauzasi, qaytarilishi kerak bo'lgan yozuvlar sonini belgilash uchun ishlatiladi. Bu katta ma'lumotlar bazalari bilan ishlaganda, masalan, minglab yozuvlari bo'lgan jadvallar bilan ishlashda foydalidir. Katta miqdordagi yozuvlarni qaytarish, dastur samaradorligiga ta'sir ko'rsatishi mumkin.

Misol: Faqat "Customers" jadvalidan birinchi 3 yozuvni tanlash:

SELECT TOP 3 * FROM Customers;

Eslatma: Har bir ma'lumotlar bazasi tizimi SELECT TOP klauzasini qo'llab-quvvatlamaydi. MySQL LIMIT klauzasini, Oracle esa FETCH FIRST n ROWS ONLY va ROWNUM klauzalarini ishlatadi.

Sintaksislar

  • SQL Server / MS Access Sintaksisi:
    SELECT TOP number|percent column_name(s)
    FROM table_name
    WHERE condition;
  • MySQL Sintaksisi:
    SELECT column_name(s)
    FROM table_name
    WHERE condition
    LIMIT number;
  • Oracle 12 Sintaksisi:
    SELECT column_name(s)
    FROM table_name
    ORDER BY column_name(s)
    FETCH FIRST number ROWS ONLY;
  • Eski Oracle Sintaksisi:
    SELECT column_name(s)
    FROM table_name
    WHERE ROWNUM <= number;

Demo Ma'lumotlar Bazasi

Customers jadvalidan misollar:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

LIMIT

MySQL uchun ekvivalent misol:

SELECT * FROM Customers
LIMIT 3;

FETCH FIRST

Oracle uchun ekvivalent misol:

SELECT * FROM Customers
FETCH FIRST 3 ROWS ONLY;

SQL TOP PERCENT Misoli

"Customers" jadvalidan birinchi 50% yozuvlarini tanlash (SQL Server/MS Access):

SELECT TOP 50 PERCENT * FROM Customers;

Oracle uchun ekvivalent misol:

SELECT * FROM Customers
FETCH FIRST 50 PERCENT ROWS ONLY;

WHERE Klauzasini Qo'shish

"Customers" jadvalidan mamlakati "Germany" bo'lgan birinchi uchta yozuvni tanlash:

  • SQL Server/MS Access:
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
  • MySQL:
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
  • Oracle:
SELECT * FROM Customers
WHERE Country='Germany'
FETCH FIRST 3 ROWS ONLY;

ORDER BY Kalit So'zini Qo'shish

Natijani tartibga solib, tartiblangan natijadan birinchi 3 yozuvni qaytarish.

  • SQL Server va MS Access:
SELECT TOP 3 * FROM Customers
ORDER BY CustomerName DESC;
  • MySQL:
SELECT * FROM Customers
ORDER BY CustomerName DESC
LIMIT 3;
  • Oracle:
SELECT * FROM Customers
ORDER BY CustomerName DESC
FETCH FIRST 3 ROWS ONLY;

Ushbu sahifada

Xato haqida xabar berish