sql >> Databasteknik >  >> RDS >> Oracle

Oracle SQL Hitta de 5 lägsta lönerna

I Oracle 12c :

-- more than 5 rows being returned, if multiple rows 
-- match the value of the 5th row
SELECT e.ID_No, e.Name
  FROM Employees e
 ORDER BY e.Salary  
 FETCH FIRST 5 ROWS WITH TIES;

-- only 5 rows being returned, even if multiple rows 
-- match the value of the 5th row
SELECT e.ID_No, e.Name
  FROM Employees e
 ORDER BY e.Salary  
 FETCH FIRST 5 ROWS ONLY; 

-- NEXT clause may be replaced with FIRST  
SELECT e.ID_No, e.Name
  FROM Employees e
 ORDER BY e.Salary 
 FETCH NEXT 5 ROWS ONLY; 

Före Oracle 12c :

SELECT e.ID_No, e.Name
  FROM ( SELECT ID_No, Name, row_number() over (order by salary) seq FROM Employees ) e
 WHERE e.seq <= 5
 ORDER BY e.seq; 

frågor kan användas för Top-N-frågor



  1. Postgres 9.5 ON CONFLICT DO SELECT

  2. SQL-fel:ORA-14006:ogiltigt partitionsnamn

  3. Hur påverkar transaktionsnivåisolering prestanda i autocommit-läge i MySQL?

  4. MySQL kontra PDO