TOP command in SQL

In SQL, the TOP statement is used to retrieve N records or X percent records from a table.

Note: All databases do not support the TOP clause. For example, MySQL supports the LIMIT clause to retrieve a limited number of records, while Oracle uses the ROWNUM command to perform the same operation.

In this article, we will show you in detail how to use TOP statements in SQL with syntax and specific examples to make it easier to visualize and capture functions better.

TOP command syntax in SQL

The basic syntax of TOP command with SELECT statement will be as follows:

 SELECT TOP so|phantram tencot 
FROM ten_bang
WHERE [dieu_kien]

Example of TOP in SQL

Suppose the NHANVIEN table has the following records:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

The following query is an example on the SQL server, it will retrieve the first 3 records from the NHANVIEN table .

 SQL> SELECT TOP 3 * FROM NHANVIEN; 

The above example returns the result:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | +----+----------+-----+-----------+----------+

For MySQL server, this is an equivalent example:

 SQL> SELECT * FROM NHANVIEN 
LIMIT 3;

The results return the same:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | +----+----------+-----+-----------+----------+

If you are using Oracle Server, then this is an equivalent example:

 SQL> SELECT * FROM NHANVIEN 
WHERE ROWNUM <= 3;

The results return the same:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | +----+----------+-----+-----------+----------+

In the next section, we will learn about the ORDER BY statement, please keep track.

Previous article: LIKE command in SQL

Next lesson: ORDER BY statement in SQL

5 ★ | 1 Vote

May be interested

  • How to use the which command in LinuxHow to use the which command in Linux
    the which command in linux determines the executable binary, which will execute when you issue a command to the shell. if you have different versions of the same program on your computer, you can use which to find out which shell will use.
  • The command reg add in WindowsThe command reg add in Windows
    the reg add command adds new subkeys or entries to the registry.
  • Exit command in WindowsExit command in Windows
    the exit command exits the cmd.exe program (command interpreter) or the current batch script. to learn how to use this command, please see the example below.
  • Rem command in WindowsRem command in Windows
    the rem command helps write comments (comments) in a batch file or config.sys.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • How to use Xcopy command in Command PromptHow to use Xcopy command in Command Prompt
    knowing how to use xcopy command in command prompt, you will easily use commands in cmd professionally. in particular, the xcopy command is extremely useful for those who copy and copy data on the computer.
  • 20+ essential Linux security commands20+ essential Linux security commands
    here are some of the most important security commands for everyday work on linux systems. if you're interested in security issues on your linux system, don't ignore these helpful security commands.
  • The certutil command in WindowsThe certutil command in Windows
    when the certutil command is run by a ca without additional parameters, it displays the current ca configuration. when the certutil command is run on an unassigned ca, the default command to run is certutil -ump.
  • Defrag command in WindowsDefrag command in Windows
    the defrag command locates and merges fragmented files on local drives to improve system performance. local administrator, or equivalent, is the minimum necessary requirement to run this command.
  • Eventcreate command in WindowsEventcreate command in Windows
    the eventcreate command allows the admin to create custom events in the specified event log. for an example of how to use this command, please see the example below.