Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get a limit, without using the <code>limit</code> keyword, you can impose a count.</p> <p>For example, given the table definition</p> <pre><code>create table cars (id int, carBrand char(10), carModel char(10)); </code></pre> <p>this will give you all the Car Models for the top 2 Car Brands</p> <pre><code>select cars.carBrand, cars.carModel from cars where ((select count(*) from (select distinct carBrand from cars) as carBrands where carBrands.carBrand &lt; cars.carBrand) &lt; 2) order by cars.carBrand, cars.carModel; </code></pre> <p>This creates an inline table just listing the <code>carBrands</code> and then joins this back to cars to get the list of all cars that are in the top 2 brands. The <code>count(*) .... &lt; 2</code> enforces the limit. Consider <code>'Ford'</code>, for example, in your above data. In '<code>Ford'</code>'s case, there are 3 brands that are <code>&lt; 'Ford'</code> alphabetically, so the <code>count(*)</code> above = 3. Since 3 is not less than 2, no <code>'Ford'</code> cars appear in the output.</p> <p>The output on your test data would be:</p> <pre><code>CARBRAND CARMODEL Alfa Romeo Guilietta Alfa Romeo Mito Audi A3 Audi R8 Audi TT </code></pre> <p>Now, you didn't say how you wanted to pick the 2 brands -- you just listed Ford and Fiat in your example -- I don't know how you happened to pick those. If you want something other than alphabetical criteria for ordering, that's doable, but harder.</p> <p>SQL Fiddle and results for all this: <a href="http://sqlfiddle.com/#!2/33a8f/3" rel="nofollow">http://sqlfiddle.com/#!2/33a8f/3</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload