Andrew Aleynikov
Andrew Aleynikov

Reputation: 3

Apex. How to output NULL values too?

I had a question with a single sql query related to an interactive table in apex

Here, look, I prescribe a request in which I print all the existing artists in the database, but I only have entries where all the fields have values, and those in which at least one NULL are not displayed

select artist.name as "Artist", country.name as "Country" , city.name as "City of foundation", label.name as "Label of records"  
from artist, country, city, label
where artist.country = country_id
and artist.city = city_id
and city.country = country_id
and artist.label = label_id

How to fix it?

https://i.sstatic.net/ZRYzm.png

Upvotes: 0

Views: 307

Answers (1)

Littlefoot
Littlefoot

Reputation: 142720

As you didn't provide test case (a screenshot isn't quite enough - at least, not to me), I'll try to show what might be going on using Scott's schema.

There are 4 departments: note department 40, and the fact that nobody works in it:

SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL> select * from emp where deptno = 40;

no rows selected

If you want to display all 4 departments and employees who work in them, you'd join EMP and DEPT table. Outer join lets you display department 40 (which, as we saw, has no employees):

SQL> select d.deptno, d.dname, e.ename
  2  from dept d left join emp e on e.deptno = d.deptno      --> outer join is here
  3  order by d.deptno;

    DEPTNO DNAME          ENAME
---------- -------------- ----------
        10 ACCOUNTING     CLARK
        10 ACCOUNTING     MILLER
        10 ACCOUNTING     KING
        20 RESEARCH       JONES
        20 RESEARCH       SMITH
        20 RESEARCH       SCOTT
        20 RESEARCH       FORD
        20 RESEARCH       ADAMS
        30 SALES          WARD
        30 SALES          TURNER
        30 SALES          ALLEN
        30 SALES          JAMES
        30 SALES          MARTIN
        30 SALES          BLAKE
        40 OPERATIONS                       --> this is what you're looking for

15 rows selected.

SQL>

You'd get the same result using the old Oracle's (+) outer join operator. You'd rather switch to modern joins and avoid that operator, though.

SQL> select d.deptno, d.dname, e.ename
  2  from dept d, emp e
  3  where d.deptno = e.deptno (+)        --> the old outer join operator
  4  order by d.deptno;

    DEPTNO DNAME          ENAME
---------- -------------- ----------
        10 ACCOUNTING     CLARK
        10 ACCOUNTING     MILLER
        10 ACCOUNTING     KING
        20 RESEARCH       JONES
        20 RESEARCH       SMITH
        20 RESEARCH       SCOTT
        20 RESEARCH       FORD
        20 RESEARCH       ADAMS
        30 SALES          WARD
        30 SALES          TURNER
        30 SALES          ALLEN
        30 SALES          JAMES
        30 SALES          MARTIN
        30 SALES          BLAKE
        40 OPERATIONS

15 rows selected.

SQL>

Upvotes: 2

Related Questions