I need to know how to return a default row if no rows exist in a table. What would be the best way to do this? I'm only returning a single column from this particular table to get its value.
Edit: This would be SQL Server.
Which of the following clause is used to limit the number of rows retrieved from a SELECT query? Answer: B. The WHERE clause is used to restrict the number of rows returned from a SELECT query.
Use the relational operators != or <> to exclude rows in a WHERE clause.
One approach for Oracle:
SELECT val FROM myTable UNION ALL SELECT 'DEFAULT' FROM dual WHERE NOT EXISTS (SELECT * FROM myTable)
Or alternatively in Oracle:
SELECT NVL(MIN(val), 'DEFAULT') FROM myTable
Or alternatively in SqlServer:
SELECT ISNULL(MIN(val), 'DEFAULT') FROM myTable
These use the fact that MIN()
returns NULL
when there are no rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With