This article will be looking into how to SELECT the first record of a MySQL table or a query. We will be going through a few examples to demonstrate the concept.

Let us get started by making the sample data to be used across the examples. Create a table named sales_team_emails, followed by inserting some rows into it.

#create table sales_team_emails
CREATE TABLE sales_team_emails (
    sales_person_id INT AUTO_INCREMENT,
    sales_person_name VARCHAR(255),
    sales_person_email VARCHAR(255),
    PRIMARY KEY (sales_person_id)
);
#insert rows to sales_team_emails
 INSERT INTO sales_team_emails (sales_person_name,sales_person_email) 
 VALUES("Aditi","aditi@managementbliss.com"),
 ("Furan T","Furan.Abey@me.com"),
 ("Veronica","Veronika.Loud@me.com"),
 ("Atharv","Atharv@managementbliss.com"), 
 ("Erick","Erick@mine.com"),
  ("Veronica","Veronika.Sun@me.com"),
 ("Rasmus","Rasmus.Thomas@mine.com"),
 ("Aditi Sharma","aditi@managementbliss.com"),
 ("George","Furan.Abey@me.com"),
 ("Veronica","Veronika.Freebird@me.com"),
 ("Simon","Simon@bond.com");

To view the snapshot of the table sales_team_emails, execute:

SELECT * FROM sales_team_emails;
image_1: sales_team_emails

Example1: Get the first record of the table sales_team_emails

We will be using the LIMIT clause. LIMIT clause is used when we want to restrict the result set to a certain number of rows. To get the first record of the table sales_team_emails, observe the below select statement.

SELECT * FROM sales_team_emails LIMIT 1;

Action Output Message:-

1 row(s) returned.

Output:-

image_2

Similarly, we can also get the first record of any other query.

Example2: Get the first record of sale_person_id and sales_person_email from the table sales_team_emails where sales_person_name is Veronica.

SELECT 
    sales_person_id, sales_person_email
FROM
    sales_team_emails
WHERE
    sales_person_name = 'Veronica'
LIMIT 1;

Action Output Message:-

1 row(s) returned.

Output:-

image_3

If we run the query without the LIMIT clause, we will get more than one result.

SELECT 
    sales_person_id, sales_person_email
FROM
    sales_team_emails
WHERE
    sales_person_name = 'Veronica';

Output:-

image_4

READ MORE

We hope this article helped you with getting the first record of a table or a query in MySQL. Good Luck!!