MySQL select first row

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","[email protected]"),
 ("Furan T","[email protected]"),
 ("Veronica","[email protected]"),
 ("Atharv","[email protected]"), 
 ("Erick","[email protected]"),
  ("Veronica","[email protected]"),
 ("Rasmus","[email protected]"),
 ("Aditi Sharma","[email protected]"),
 ("George","[email protected]"),
 ("Veronica","[email protected]"),
 ("Simon","[email protected]");

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!! 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top