'How to use LIMIT and IN together to have a default row in SQL?

I am exploring SQL with W3School page and I have this requirements where I need to limit the query to a certain number but also having a default row included with that limit.

enter image description here

Here I want a default row where the customer name is Alfreds, then grab the remaining 29 rows to complete the query regardless of what their name is.

enter image description here

I tried to look on other SO question but they are too complicated to understand and using different syntax.

sql


Solution 1:[1]

What you are looking for is a specific order clause. Try this

SELECT * FROM Customers order by (case when CustomerName in ('Alfreds Futterkiste') then 0 else CustomerId end) limit 30 ;

Solution 2:[2]

If you're going to have a default row in SQL you should really have that row in the table with a known primary key, and then UNION it onto your query:

--default row, that is always included as long as the table has a PK 1
SELECT *
FROM Customers
WHERE CustomerId = 1

UNION ALL
 
--other rows, a variable number of
SELECT *
FROM Customers
WHERE CustomerId <> 1 AND ...

LIMIT 30

The limit presented in this way applies to the result of the Union

If you ever want to do something where you're unioning together limited sets in other combinations you might want to look at eg a form like

(... LIMIT 2)
UNION ALL
(... LIMIT 28)

Solution 3:[3]

Use UNION to combine the two queries.

SELECT *
FROM Customers
WHERE CustomerName != 'Alfredo Futterkiste'
LIMIT 9
UNION 
SELECT *
FROM Customers
WHERE CustomerName = 'Alfreo Futterkiste'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asgar
Solution 2
Solution 3