Given following script:
CREATE TABLE chartest (
  charval CHAR(10) NOT NULL,
  varcharval VARCHAR(30) NOT NULL
);
INSERT INTO chartest VALUES ('aaa','aaa');
INSERT INTO chartest VALUES ('aaaaaa      ','aaa'); -- 6 'a' letters and 6 spaces in first case
INSERT INTO chartest VALUES ('aaaaaaaaaaaa','aaa'); -- 12 'a' letters as first argument
SELECT COUNT(*) FROM chartest; -- query #1
DELETE FROM chartest WHERE charval='aaaaaa';
SELECT COUNT(*) FROM chartest; -- query #2
SELECT COUNT(*) FROM chartest WHERE charval=varcharval; -- query #3
What will each of three specified SELECT queries return?
Explanation
First INSERT query will be executed without any errors. charval field will be complemented to 10 symbols with spaces.
Second INSERT query will run smoothly as well - but 2extra spaces will be truncated in that case.
Third INSERT query will not succeed though. Non-space symbols cannot be truncated during insertion into char and varchar fields.
Query #1 will return 2

Following DELETE query will delete one record. Trailing spaces are ignored during char-values comparison. Therefore, charval='aaaaaa' condition will be evaluated to True for ('aaaaaa ','aaa') record.
Query #2 will return "1"

Comparison in the third query will evaluate to True for the same reason. ('aaa','aaa') record meets its condition.
Query #3 will return "1"

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
SQL quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes