Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select going wrong

I need to generate this output from this db table

— Expected output:
— Neil

— Example case create statement:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CREATE TABLE poll (
  id INTEGER NOT NULL PRIMARY KEY,
  name VARCHAR(30) NOT NULL,
  answer CHAR(1)
);

INSERT INTO poll(id, name, answer) VALUES(1, 'Neil', NULL);
INSERT INTO poll(id, name, answer) VALUES(2, 'Nina', 'Y');
INSERT INTO poll(id, name, answer) VALUES(3, 'Walt', 'N');

I tried to obtain the results using this sql query but does not work

select * from poll where (name like 'N%') AND (answer <> 'N' or answer <> 'Y');

what did I do wrong?

Thx for the support!

>Solution :

Comparing NULL against any string literal will always return false. So, you should also include a null check in your logic:

SELECT *
FROM poll
WHERE name LIKE 'N%' AND
      (answer NOT IN ('N', 'Y') OR answer IS NULL);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading