SQL SELECT FIRST

SQL SELECT FIRST retrieves the first/top row from the table and displays it in the results set.

SELECT FIRST in SQL

The SELECT FIRST clause in SQL is used to fetch the first record from a column.

This FIRST query can be used for Employee management systems, inventory management, and billing systems.

Note: The SELECT FIRST clause is not universally supported across all SQL platforms. Some variations of the FIRST clause in other databases are LIMIT, TOP, or FETCH FIRST.

Syntax

The syntax to use the SELECT FIRST clause in SQL is:

SELECT FIRST (columnName) FROM tableName;

SQL SELECT FIRST Example

Let’s look at an example of the SELECT FIRST query in SQL.

This query can only be used in MS Access.

In this example, we will select the first name for the table “stationary”.

SELECT FIRST (Name) AS First_name FROM Stationary;

How to Use SQL SELECT FIRST Clause

Follow the given steps to use the SELECT FIRST clause in SQL.

So we will start by creating a database to perform the operations.

Step 1: Create a database.

CREATE DATABASE GFG

Step 2: Use this database

USE GFG

Step 3: Create a table

/****** (1,1) indicates that increment 1 every time insert is performed ******/

CREATE TABLE first
(ID INT PRIMARY KEY IDENTITY (1,1),
Name VARCHAR (20) NOT NULL,
Age INT NOT NULL,
Dept VARCHAR (20) NOT NULL)

Step 4: Check the created table schema  

Schema of table

Step 4: Insert the values in Table

/****** Insertion queries ******/

INSERT INTO [dbo].[first]
([Name]
,[Age]
,[Dept])
VALUES
('Devesh', 20, 'CSE')
GO

INSERT INTO [dbo].[first]
([Name]
,[Age]
,[Dept])
VALUES
('Aditya', 19, 'BT')
GO

INSERT INTO [dbo].[first]
([Name]
,[Age]
,[Dept])
VALUES
('Megha', 20, 'CSE')
GO

Step 5: Use first() function in table( first() is used in MS ACCESS ).

SELECT TOP 1 Name FROM first

Important Points About SQL SELECT FIRST

  • The SELECT FIRST command in SQL is used to retrieve only the top row from a table.
  • SELECT FIRST can be beneficial for various purposes like login-related functions on websites or implementing billing systems.
  • The SELECT FIRST clause is not universally supported across all SQL systems. It is primarily used in MS Access.
  • It can used with other clauses like ORDER BY.