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 951483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:44:37+00:00 2026-05-15T23:44:37+00:00

That title is brutal, but I don’t know how else to put it. I

  • 0

That title is brutal, but I don’t know how else to put it.

I have a key value table tied to a user_id that stores user preferences throughout the site. If a user hasn’t gone in and updated any of their settings, any time I ask for a key (say “FAVORITE_COLOR” it’s going to be null, so I need to pull the default setting that I have tied to the default user, user_id = 0.

I was thinking along the lines of UNION’ing the user_id = 0 results to the bottom of the query, but that’d just lead to duplicates. I’m not sure if it’s even possible, but what’d I’d love to be able to say something like:

SELECT val FROM k_v WHERE user_id = 123 AND k = 'FAVORITE_COLOR'
UNION IF val IS NULL
SELECT val FROM k_v WHERE user_id = 0 AND k = 'FAVORITE_COLOR';

Any good way to do this?

Edit: Thanks for all the help on this. If your use case is only grabbing a single value which is what this question is, then NVL from dual is exactly what you want, but if you’re planning on returning multiple pairs in the same query, take a look at some of the other answers as they may actually make more sense for implementation.

I wound up going with ZeissS’s suggestion below as it’s simple, still one query, works with multiple k,v pairs and I don’t have a problem doing the possible duplicate filtering client side.

  • 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-05-15T23:44:38+00:00Added an answer on May 15, 2026 at 11:44 pm

    Use this,

    select
      nvl(
        (SELECT val FROM k_v WHERE user_id = 123 AND k = 'FAVORITE_COLOR'),
        (SELECT val FROM k_v WHERE user_id = 0 AND k = 'FAVORITE_COLOR')
      ) val 
    from dual
    ;
    

    From the Oracle docs,

    NVL(expr1, expr2):
    NVL lets you replace null (returned as a blank) with a string in the results of a query. If
    expr1 is null, then NVL returns expr2. If expr1 is not null, then NVL returns expr1.

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

Sidebar

Related Questions

Hopefully that title makes sense... Let's say I have an employee table: ID |
I know that title sounds crazy but here is my situation. After a certain
Folks, I know I didn't phrase that title very well, but here's the scenario.
Sorry, that title probably doesn't make much sense, but what I want to know
I know that the title might sound a little contradictory, but what I'm asking
OK, that title is a little unclear, but I can't think of a better
(I realize that my title is poor. If after reading the question you have
I know that the title doesn't really say what I'm actually looking for, as
I have a text title that reads This User The Title Of The Post
I have in a sidebar: <div id=yyy> [shortcode]</div> It outputs a title that when

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.