Pop quiz: funny syntax

The following questions are of little importance, yet I find them entertaining. I stumbled upon these while developing QueryScript.

Can you guess the results of the following statements?

Pop quiz 1

SET @x := 7;
SELECT ++@x;

What is the computation result? What will be the value of @x?

Pop quiz 2

SET @ := 4;
SELECT @ + @'' + @``

What is the computation result?

Pop quiz 3

SET @a := 2;
SELECT @a = @'a' = @`a`

Do we get TRUE or FALSE? When?

Hints

Consider the following queries as hints to the above questions:

SELECT +++++-@x;
SELECT @ = @'', @ = @``
SELECT (@a = @'a') = @`a`

Answers

  • Pop quiz 1

++@x is interpreted as +(+(@x)), which is in turn evaluated as 0+(0+(@x)). No relation to C‘s ++ operator. @x is unchanged.

  • Pop quiz 2

@x is the same as @’x’ and as @`x`. What’s funny is that one is allowed to create the empty-named user defined variable @”. Makes for a weird looking syntax, but nothing special about it. Computation result is 12.

  • Pop quiz 3

We’ve already established that @a, @’a’ and @`a` are the same, I just used this notation for adding pepper to the quiz. The real question is whether 2 = 2 = 2 holds true.  It does not. There is no 3-way comparison. All comparisons are in pairs, which is why the expression evaluates as (2 = 2) = 2, leading to 1 = 2, “1” being the TRUE value of (2 = 2). The only value of @a for which the expression holds true is 1.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Rick James Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
Rick James
Guest

Another quiz. (People do try this in WHERE clauses.)
SELECT 123 < col < 456;

That probably evaluates to
SELECT (123 < col) 1 (and sometimes >0).