srvy srvy - 1 year ago 66
MySQL Question

What is the use of "SELECT <somevalue>" in SQL

Mysql allows just do a "select 1" and will return column/value of "1". Also if I provide some random string.

mysql> select "foo" ;
| foo |
| foo |
1 row in set (0.01 sec)


Is there any use-case for supporting these commands?

Answer Source

There are definitely use cases where this feature comes in handy. Support you had a table of grades and students, which looked something like this:

Student | Grade
Tom     |  75
Tim     |  85
Paul    |  50
Mark    |  65
Beth    |  70
John    |  80
Dan     |  55

If you were tasked with creating a report which showed all students, and whether they passed or fail, you could use the select constant feature. Let's say that a failing grade was anything below 60. But you have one problem, namely that the label Pass or Fail does not actually appear in your table. You can write the following query:

SELECT Student,
       CASE WHEN Grade < 60 THEN 'Fail' ELSE 'Pass' END AS Result
From students

This would give the following output:

Student | Result
Tom     |  Pass
Tim     |  Pass       -- these constants are not part of your original data
Paul    |  Fail       -- set, but rather were introduced in the SELECT statement
Mark    |  Pass
Beth    |  Pass
John    |  Pass
Dan     |  Fail
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download