SQL – SELECT FIRST
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
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
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