15

I need to display the keyboard players from a list of bands, and I've been able to using the following SQL:

SELECT BAND.NAME AS Band_Name, KBPLAYER.NAME AS Keyboard_Player
FROM BAND
FULL OUTER JOIN (
    SELECT M.NAME, MO.BID
    FROM MEMBEROF MO, MEMBER M
    WHERE MO.INSTRUMENT='keyboards'
    AND M.MID=MO.MID
    ) KBPLAYER
ON BAND.BID=KBPLAYER.BID
ORDER BY BAND.NAME, KBPLAYER.NAME

The above query displays the names of all the band and the keyboard player (if any) in that band, but I also want to display 'No KeyBoard Players' for those bands that don't have a keyboard player. How can I achieve this? Please let me know if you need me to furnish with details of the table structure.

Update: Please note that I'm not able to use any of the SQL3 procedures (COALESCE, CASE, IF..ELSE). It needs to conform strictly to SQL3 standard.

8
  • I've just updated my tags. Commented Feb 5, 2014 at 6:39
  • Just check NVL, it might help you Commented Feb 5, 2014 at 6:42
  • Is it giving NULL for you if there is no keyboard player? Commented Feb 5, 2014 at 6:48
  • It is not showing NULL; just an empty field, indicating it is null. Commented Feb 5, 2014 at 6:50
  • So you want to replace empty field to some text? Commented Feb 5, 2014 at 6:51

6 Answers 6

31

Use the coalesce function. This function returns the first of it's arguments that are not null. Eg:

COALESCE(KBPLAYER.NAME,'No KeyBoard Players') AS Keyboard_Player
Sign up to request clarification or add additional context in comments.

4 Comments

That works - except part of my assignment's requirement is that I'm not able to use COALESCE. Is there an alternative that I can look into?
Are you allowed to use a case statement? And please update your question with any constraints like functions you are not allowed to use
Updated. Please refer to above.
Thanks for introducing to me COALESCE, this will come useful in another one of my assignments.
3

I've decided to do it differently since I wasn't going anywhere with the above SQL. I'll appreciate if anyone has suggestions to make for the above SQL with the set constraints.

SELECT 
  band.name AS Band_Name, 'NULL' AS Keyboard_Player
FROM 
  memberof
INNER JOIN 
 member
ON 
  memberof.mid = member.mid 
FULL JOIN 
  band
ON 
  memberof.bid = band.bid 
AND 
  instrument = 'keyboards'

WHERE  
  member.name IS NULL 

UNION

SELECT 
  band.name AS Band_Name, member.name AS Keyboard_Player
FROM 
  memberof
INNER JOIN 
 member
ON 
  memberof.mid = member.mid 
FULL JOIN 
  band
ON 
  memberof.bid = band.bid 
WHERE
  instrument = 'keyboards'

Comments

1

As per your comment to replace empty string to some text, the simple solution is to use case statement like below.

SELECT BAND.NAME AS Band_Name, 
CASE WHEN KBPLAYER.NAME = '' THEN 'No Player' ELSE KBPLAYER.NAME END AS Keyboard_Player
FROM BAND
FULL OUTER JOIN (
    SELECT M.NAME, MO.BID
    FROM MEMBEROF MO, MEMBER M
    WHERE MO.INSTRUMENT='keyboards'
    AND M.MID=MO.MID
    ) KBPLAYER
ON BAND.BID=KBPLAYER.BID
ORDER BY BAND.NAME, KBPLAYER.NAME

1 Comment

Thanks Naveen - but I've just updated my question with specific constraints, as advised by Harmic.
0

Try this....

    SELECT BAND.NAME AS Band_Name, 
ISNULL(NULLIF(KBPLAYER.NAME,''),'No KeyBoard Players') AS Keyboard_Player
    FROM BAND
    FULL OUTER JOIN (
        SELECT M.NAME, MO.BID
        FROM MEMBEROF MO, MEMBER M
        WHERE MO.INSTRUMENT='keyboards'
        AND M.MID=MO.MID
        ) KBPLAYER
    ON BAND.BID=KBPLAYER.BID
    ORDER BY BAND.NAME, KBPLAYER.NAME

4 Comments

I'm getting ERROR: function isnull(text, unknown) does not exist'.
ISNULL is checking if the value of column is NULL, but in this case it has empty string. So Is null is not going to work.
Because of that, I've added the NULLIF see above query
Oh yeah.. I overlooked into it, its fine. :)
0

The above solutions will not handle an empty string. This one will.

COALESCE(NULLIF(KBPLAYER.NAME,''),'No KeyBoard Players') AS Keyboard_Player

Comments

-1

If NVL is allowed, below should work.

NVL(KBPLAYER.NAME, 'No KeyBoard Players')

2 Comments

I'm getting 'ERROR: function nvl(character varying, unknown) does not exist'.
The question is tagged postgresql - NVL is non-standard and not supported by postgresql. coalesce is the equivalent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.