Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:42+00:00 2026-06-17T16:59:42+00:00

I need to write a query which would always return something, even if nothing

  • 0

I need to write a query which would always return something, even if nothing satisfies conditions, something like this

SELECT * WHERE date > NOW() FROM table ORDER by date
   IF none is returned THEN
      SELECT FIRST FROM table ORDER by date

So only numbers more then 10 will be returned, if none is returned, return any number
Any ideas how to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:59:43+00:00Added an answer on June 17, 2026 at 4:59 pm

    Here is one way, using union all:

    select *
    from table
    where number > 10
    union all
    (select *
     where number > 0 and
           not exists (select * from table where number > 10)
     limit 1
    )
    

    If you are using a reasonable version of SQL, you could do something like:

    select t.*
    from (select t.*, max(number) over () as maxnumber,
                 row_number() over (order by number desc) as seqnum
          from table t
         ) t
    where (maxnumber > 10 and number > 10) or seqnum = 1
    

    You need window functions for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL LIKE vs LOCATE I need to write a Mysql query which
I need to write a SQL query which will get me those rows from
I need to write the following query so the att field is always a
How would one write a LINQ query which takes a hierarchical source data and
I need to write a simple query in mySQL that would use MIN() in
I am a newbee to SQL and need to write a query which seems
I need to write a select query for a network diagram. There are two
I have a query which returns a result set which looks like this: A
I need to write a query where I need to find the ' character.
I need to write q query where I check if the table exists, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.