Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

sql query

This is a discussion on sql query within the SQL forums, part of the Databases category; 1. Display employee name and salary as 'Above 1500’ if salary >1500 (using Decode) ‘Below 1500’ if salary ‘Exactly 1500’ If salary=1500 2. Write a query to to find out ...

Go Back   Geeks Talk > Databases > SQL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 02-11-2009
Junior Member
 
Join Date: Feb 2009
Location: india
Posts: 24
Thanks: 12
Thanked 1 Time in 1 Post
hariprasad234 is on a distinguished road
sql query

1. Display employee name and salary as

'Above 1500’ if salary >1500 (using Decode)
‘Below 1500’ if salary<1500
‘Exactly 1500’ If salary=1500


2. Write a query to to find out number of days in that month for a given date ?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-11-2009
Junior Member
 
Join Date: Feb 2009
Location: Chennai, India
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
chandrupsekar is on a distinguished road
Re: sql query

select to_char(lastday(to_date('02-01-2009'), sysdate from dual
Reply With Quote
  #3 (permalink)  
Old 02-11-2009
Junior Member
 
Join Date: Feb 2009
Location: Chennai, India
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
chandrupsekar is on a distinguished road
Cool Re: sql query

select to_char(lastday(to_date('02-01-2009'), sysdate from dual.

Correct me if I am wrong
Reply With Quote
  #4 (permalink)  
Old 02-11-2009
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,853
Thanks: 9
Thanked 168 Times in 142 Posts
debasisdas has a spectacular aura aboutdebasisdas has a spectacular aura about
Re: sql query

better to use case, that is more effective

Code:
SELECT EMPNO,ENAME,
 CASE 
 WHEN SAL>1500 THEN 'ABOVE'
 WHEN SAL<1500 THEN 'BELOW'
 WHEN SAL=1500 THEN 'EXACT'
 END AS REMARK
 FROM EMP;
Reply With Quote
The Following User Says Thank You to debasisdas For This Useful Post:
  #5 (permalink)  
Old 02-12-2009
Contributing Member
 
Join Date: Dec 2008
Location: bangalore
Posts: 76
Thanks: 0
Thanked 18 Times in 18 Posts
ecearund is on a distinguished road
Re: sql query

Hi,
it is some what complex to get your solution using Decode Function..
For your requirement better use case statement..it is advisable..
Anyhow check it out this..

select ename,
sum(decode(greatest(SALARY,29000),least(SALARY,30000),1,0))
from employee
group by ename;

It displays list of ename who r having salary in the range of 29000-30000.
Reply With Quote
  #6 (permalink)  
Old 02-12-2009
Contributing Member
 
Join Date: Dec 2008
Location: bangalore
Posts: 76
Thanks: 0
Thanked 18 Times in 18 Posts
ecearund is on a distinguished road
Re: sql query

For ur second Query,,
Try this..

SELECT TO_CHAR(LAST_DAY('01-jan-2008'),'dd')
from dual;
Reply With Quote
The Following User Says Thank You to ecearund For This Useful Post:
  #7 (permalink)  
Old 02-13-2009
Junior Member
 
Join Date: Feb 2009
Location: india
Posts: 24
Thanks: 12
Thanked 1 Time in 1 Post
hariprasad234 is on a distinguished road
Re: sql query

Quote:
Originally Posted by ecearund View Post
For ur second Query,,
Try this..

SELECT TO_CHAR(LAST_DAY('01-jan-2008'),'dd')
from dual;
thanks alot
Reply With Quote
  #8 (permalink)  
Old 02-13-2009
Junior Member
 
Join Date: Feb 2009
Location: india
Posts: 24
Thanks: 12
Thanked 1 Time in 1 Post
hariprasad234 is on a distinguished road
Re: sql query

Quote:
Originally Posted by chandrupsekar View Post
select to_char(lastday(to_date('02-01-2009'), sysdate from dual.

Correct me if I am wrong



SELECT TO_CHAR(LAST_DAY('01-jan-2008'),'dd')
from dual;

sorry, and the above query is working

any way thanks for giving support
your second query is corect
Reply With Quote
  #9 (permalink)  
Old 02-20-2009
Junior Member
 
Join Date: Feb 2009
Location: kolkata
Posts: 8
Thanks: 1
Thanked 1 Time in 1 Post
blue_hill is on a distinguished road
Re: sql query

Hi,

If you need to use the decode function then first you have to create a formula that will evaluate to a single number for each one of your ranges


The following code may give you some idea,though I am not sure of the code because I havent run it before posting it to you.

SELECT emp_name,
decode(trunc (( salary + 1500) / 1500), 1, "BELOW",
2, "EXACT",
"ABOVE") as SALARY
FROM employees;


For the second question,I think the solution provided by the other members is sufficient.
Reply With Quote
  #10 (permalink)  
Old 2 Weeks Ago
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 514
Thanks: 29
Thanked 63 Times in 61 Posts
susarlasireesha will become famous soon enough
Re: sql query

Quote:
Originally Posted by hariprasad234 View Post
1. Display employee name and salary as

'Above 1500’ if salary >1500 (using Decode)
‘Below 1500’ if salary<1500
‘Exactly 1500’ If salary=1500


2. Write a query to to find out number of days in that month for a given date ?

Select ename,
DECODE (SUBSTR (sal - 1500, 1, 1),
'-', 'Below',
'0', 'Exact',
'Above'
)
From scott.emp
__________________
Sireesha
Reply With Quote
  #11 (permalink)  
Old 1 Week Ago
Junior Member
 
Join Date: Oct 2009
Location: varanasi
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
akg_swaroop is on a distinguished road
Smile start

hi freinds,i m very glad to be here...its reali helping me out in solving my different problems...
Reply With Quote
Reply

  Geeks Talk > Databases > SQL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Update query using sub query ravikumar.drk99 SQL 5 09-01-2008 04:40 AM
How to get this o/p from the sql query bhaski SQL 4 06-21-2008 02:27 AM
What is an Ad Hoc Query Lokesh M Data Warehousing 0 01-31-2008 06:57 AM
DB2 query kanchhana DB2 2 12-28-2007 01:12 AM
SQL Query vidyasris SQL 2 09-24-2007 08:24 AM


All times are GMT -4. The time now is 09:59 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved