Jossi94
Jossi94

Reputation: 21

SQL. Don't show row if column has no value

List authors who have 'e' as the 4th letter in first name or last name, and have a pseudonym.

select * from authors where last_name like '___e%' or first_name like '___e%' and pseudonym is not null and pseudonym <> ' ';

The output still show the row where pseudonym are null (picture)

Upvotes: 0

Views: 2874

Answers (2)

Ted at ORCL.Pro
Ted at ORCL.Pro

Reputation: 1612

select *
from authors
where (last_name like '___e%' or first_name like '___e%') and
      trim(pseudonym) is not null;

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269693

You need parentheses:

select *
from authors
where (last_name like '___e%' or first_name like '___e%') and
      pseudonym is not null and
      pseudonym <> ' ';

If you are learning SQL, then use parentheses whenever your conditions have more than one logical operator (such as (AND) and (OR)).

The comparison is not null is redundant. I would recommend that you leave it in anyway, just to be explicit about the conditions.

Upvotes: 1

Related Questions