Open In App
Related Articles

SQL – SELECT FIRST

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.

Last Updated : 14 May, 2021
Like Article
Save Article
Similar Reads
Related Tutorials