Select Top Percent Records MSSQL, MySQL


Returning TOP records

Microsoft SQL Server
SELECT TOP 10 column FROM table
My SQL
- SELECT column FROM table LIMIT 10

Returning TOP PERCENT Records

Microsoft SQL Server
- SELECT TOP 50 PERCENT * FROM table
MySQL
SELECT @percentage := ROUND(COUNT(*) * 50/100) FROM table;
  PREPARE STMT FROM 'SELECT * FROM table LIMIT ?';
  EXECUTE STMT USING @percentage;

Share this

Related Posts

Previous
Next Post »