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

  • Home
  • SEARCH
  • 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 7857927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:11:48+00:00 2026-06-02T21:11:48+00:00

I have a table Emp like this for example. ———————- eName | eId ———————-

  • 0

I have a table Emp like this for example.

----------------------
eName      | eId
----------------------
Anusha       1
Sunny        2

Say i am looking for an entry whose id is 3.I want to write a query which finds the row and displays it.But if it doesnt find it it is expected to display a default row (temp,999)

select case
when (total != 0) then (select eName from Emp where eId = 3)
when (total == 0) then "temp"
end as eName,
case
when (total != 0) then (select eId from Emp where eId = 3)
when (total == 0) then 999
end as eId
from Emp,(select count(*) as total from Emp where eId = 3);

Using this query that i wrote it gives me two rows as a result.

temp  999
temp  999

I assume it is because of
(select count(*) as total from Emp where eId = 3) this query in the from list of the query.

I tried using the distinct clause and it gives me just a single row. But i am a little doubtful if i am messing the query and only trying to probably employ a hack to do it.Please suggest if there is a better way to do this or if i am wrong.

  • 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-02T21:11:49+00:00Added an answer on June 2, 2026 at 9:11 pm

    I’ll get to how to do this right, but first let me give you a long answer to maybe help you with your understanding of SQL. What’s happening to use is this:

    Your select clause does not affect the number of records you get. So to understand what’s happenning, let’s simpify the query a little. Let’s change it to,

    select * from emp, (select count(*) as total from emp where eid=3)
    

    I’m not sure what you think the comma after “emp” does here, but SQL see this as an old-style join on two tables: emp and the temporary table created by the select count(*), etc. There is no WHERE clause, so this is a cross join, but the second table only has one record anyway, so that part doesn’t matter. But the fact that there is no WHERE clause means that you will get every record in emp, joined to the count. So the output of this query is:

    ename   eid  count(*)
    Anusha  1    0
    Sunny   2    0
    

    If you had 100 records, you would get 100 results.

    Frankly there is no really clean way to do what you want in SQL. It’s the sort of thing that’s cleaner to do in code: do a plain “select … where eid=3”, and if you get no records, fill in the default at run-time.

    But assuming that you need to do it in SQL for some reason, I think the simplest way would be:

    select eid, ename from emp where eid=3
    union
    select 999 as eid, 'temp' as ename
    where not exists (select 1 from emp where eid=3)
    

    In some versions of SQL you need to give a dummy table name on the second select, like Oracle requires you to say “from dual”.

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

Sidebar

Related Questions

Hi i have a table named EMP,which looks like this.... EmpName |EmpBranchName |ApproverId| Anil
i have something like this var emp = db.Employees.toList(); in my employees table i
I have a cursor that queries a table like this CURSOR Cur IS SELECT
I have the EMP datatable it contains 500 records like this: UserAceNumber UserID emp001
I am having one table say emp, in that i dont have any values.
I have a table emp with following structure and data: name dept salary -----
I have the typical emp table from which i want to fetch the hierarchical
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have 2 tables with structure as Emp Table id name 001 Smith 002
I have a two column table as follows: ID Emp ID 1 1 1

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.