Open In App

SQL – SELECT FIRST

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article we are going to study the SELECT FIRST commands in SQL, as we all know that there is already a command for selecting all the rows from a table by “SELECT * FROM Table” but if we want to select only the top row, here SELECT FIRST comes into play. This FIRST query can be used for login related purpose in any website, or if we want to make a billing system we can implement this that increment the Bill number from the top row number.

The first () function is used to return the first row of any table.

Syntax : SELECT FIRST(columnName) FROM tableName

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


Last Updated : 14 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads