mysql
  1. mysql-first

MySQL Aggregate Function: FIRST()

The FIRST() function is a MySQL Aggregate Function that enables you to return the first value in an ordered set of values. In this tutorial, we'll discuss the syntax and usage of the FIRST() function.

Syntax

The syntax for the FIRST() function is:

FIRST(value)

Example

Let's say we have a table called members that contains the following data:

id name age city
1 John 25 New York
2 Amy 32 Chicago
3 Michael 29 Houston
4 Jessica 21 Miami
5 Samantha 36 Seattle

We can use the FIRST() function to return the name of the first member in the table:

SELECT FIRST(name) FROM members;

The output will be:

John

Explanation

In the example above, we used the FIRST() function to return the name of the first member in the table. Since we didn't specify an order, the first member returned is the one with the lowest id. In this case, the first member with the lowest id is John.

Use

The FIRST() function is useful when you want to return the first value in a set. It can be used in conjunction with the ORDER BY clause to retrieve the first value in an ordered set, or without the ORDER BY clause to retrieve the first value in an unsorted set.

Important Points

  • The FIRST() function is a MySQL Aggregate Function that is used to return the first value in a set of values.
  • The FIRST() function can be used with or without the ORDER BY clause.
  • If the ORDER BY clause is not specified, the first value returned is the first value in the table.

Summary

In this tutorial, we discussed the syntax and use of the FIRST() function in MySQL. We also provided an example of how to use the function to return the first element in a table. With the knowledge gained from this tutorial, you should be able to use the FIRST() function to retrieve the first element in a MySQL table.

Published on: