Skip to content

Tag: sql-order-by

How to get the first row per group?

I have a query like this: The result looks like this: Now I want to get the first row for each category_id. So it must be the biggest num and its business_id. So the expected result would be: How can I do that? Answer if your MySQL version support ROW_NUMBER + window function, you can try to use ROW_NUMBER to

MySQL Query Returns Different Results

I’m having a strange problem with the following query: Sometimes I get zero, one, two or three results. I should always get three results. What could be causing this? Answer As pointed out by Solarflare, GROUP_CONCAT() could produce your team string in a random order, e.g. ‘Van Williams & Dere…

SQL query for entries of last N jobs

I have the following table: etc, so basically jobs keep adding entries concurrently I am looking for an efficient query to return all entries of the latest (by time) N jobs So find the first (by time) entry for each job, then using that get the latest N jobs with all their entries So for the above table and N…