Skip to content
Advertisement

SQL SELECT showing records only if the number of records is greater than N

I have a table defined like this (MySQL 5.1):

Sample dataset:

I need to show records matching a certain criteria (lets suppose lastname = 'Santucci' ) only if the number of records is greater than a certain defined limit (lets say 2). I tried in various way without success the most promising form was:

It returns only the first record.

I would prefer to use something like this form because HAVING clause will enable the use of a parameter.

— LATE UPDATE —

I have to be more specific on the solution: I’m looking for something that do not deal with the inner SELECT and more specifically its WHERE clause because, as I pointed out, the one provided is pretty hypotetical (i.e. it can be quite different from this and much more complex). Of course I appreciate any other hint.

Advertisement

Answer

I am guessing that your result is

1 Marcello Santucci

but you want something like this:

In this case, you can use this query, similar to what @Popeye suggested:

or this one, based on the usage of the ‘in’ operator

You can add ‘WHERE’ clauses to limit the result to ‘Santucci’, but I assume that a more generic answer is of interest to you.

I have also prepared a small fiddle that you can play with http://sqlfiddle.com/#!9/b1a727/16

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement