Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL LIMIT with WHERE clause

Tags:

sql

postgresql

Is it possible to use LIMIT x with the WHERE clause? If so, how?

I'm trying to do this:

select * from myVIew LIMIT 10 where type=3;

But i get the following error:

ERROR:  syntax error at or near "where"
LINE 2: where type=3;
        ^
********** Error **********
ERROR: syntax error at or near "where"
SQL state: 42601
Character: 44
like image 711
capdragon Avatar asked Sep 23 '11 17:09

capdragon


People also ask

Does LIMIT come before WHERE in SQL?

So the key thing to notice is the specific order and arrangement of the SQL statement: just as FROM comes after the SELECT clause, LIMIT comes after both.

How do you write a LIMIT in SQL query?

The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve. For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard.

How do I get last 10 rows in SQL?

SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query. mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records.

What does LIMIT 1 1 do in SQL?

SELECT column_list FROM table_name ORDER BY expression LIMIT n-1, 1; In this syntax, the LIMIT n-1, 1 clause returns 1 row that starts at the row n. For example, the following query returns the employee information who has the second-highest income: SELECT emp_name, city, income FROM employees.


2 Answers

select * from myVIew  where type=3 LIMIT 10;

Limit should be after where clause.

Syntax :

SELECT column_name(s)
FROM table_name
[WHERE]
LIMIT number;
like image 148
Vishwanath Dalvi Avatar answered Oct 21 '22 03:10

Vishwanath Dalvi


Yes, have you tried this?

select * from myVIew  where type=3 LIMIT 10;

Look here for further reference. LIMIT is after WHERE and ORDER BY clauses, which makes total sense if you stop and think about it: first you have to define your base result set (filters and orders), then you limit/page it.

like image 43
Adriano Carneiro Avatar answered Oct 21 '22 04:10

Adriano Carneiro