Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT TOP statement not working

Tags:

mysql

I am trying to view the first 2 records of a table name Customers which have two columns name Name(varchar) and Salary(text) in MySQL server 6.0
Command which I am using is:

SELECT TOP 2 * FROM customers;

But its not working. Can anyone help me.

like image 627
Kapil Garg Avatar asked Jan 28 '16 08:01

Kapil Garg


People also ask

What is select top statement in SQL?

The SELECT TOP statement in SQL shows the limited number of records or rows from the database table. The TOP clause in the statement specifies how many rows are returned. It shows the top N number of rows from the tables in the output. This clause is used when there are thousands of records stored in the database tables.

What is the use of top clause in SQL?

The TOP clause in the statement specifies how many rows are returned. It shows the top N number of rows from the tables in the output. This clause is used when there are thousands of records stored in the database tables.

How do I use top with a subselect statement?

If you need to use TOP to insert, delete, or modify rows in a meaningful chronological order, use TOP with an ORDER BY clause specified in a subselect statement. See the following Examples section in this article.

How do I get the top 10 rows in SQL?

For other SQL databases, try the SELECT LIMIT statement. The syntax for the SELECT TOP statement in SQL is: It will return the top number of rows in the result set based on top_value. For example, TOP (10) would return the top 10 rows from the full result set.


2 Answers

I recommend you to write statement like this using LIMIT

SELECT * FROM customers [WHERE conditions] [ORDER BY expression [ASC|DESC]] LIMIT 2 ;

Instead of

SELECT TOP 2 * FROM customers;
like image 62
Shree Krishna Avatar answered Sep 16 '22 16:09

Shree Krishna


It's very simple. Just try this

SELECT * FROM customers LIMIT 2; 

You can check the manual also.

like image 27
Jyothi Babu Araja Avatar answered Sep 20 '22 16:09

Jyothi Babu Araja