PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5
In its simplest form, a subquery is a scalar subquery that returns a single value. A scalar subquery is a simple operand, and you can use it almost anywhere a single column value or literal is legal, and you can expect it to have those characteristics that all operands have: a data type, a length, an indication that it can beNULL, and so on. For example:
CREATE TABLE t1 (s1 INT, s2 CHAR(5) NOT NULL);INSERT INTO t1 VALUES(100, 'abcde');SELECT (SELECT s2 FROM t1); The subquery in thisSELECT returns a single value ('abcde') that has a data type ofCHAR, a length of 5, a character set and collation equal to the defaults in effect atCREATE TABLE time, and an indication that the value in the column can beNULL. Nullability of the value selected by a scalar subquery is not copied because if the subquery result is empty, the result isNULL. For the subquery just shown, ift1 were empty, the result would beNULL even thoughs2 isNOT NULL.
There are a few contexts in which a scalar subquery cannot be used. If a statement permits only a literal value, you cannot use a subquery. For example,LIMIT requires literal integer arguments, andLOAD DATA requires a literal string file name. You cannot use subqueries to supply these values.
When you see examples in the following sections that contain the rather spartan construct(SELECT column1 FROM t1), imagine that your own code contains much more diverse and complex constructions.
Suppose that we make two tables:
CREATE TABLE t1 (s1 INT);INSERT INTO t1 VALUES (1);CREATE TABLE t2 (s1 INT);INSERT INTO t2 VALUES (2); Then perform aSELECT:
SELECT (SELECT s1 FROM t2) FROM t1; The result is2 because there is a row int2 containing a columns1 that has a value of2.
A scalar subquery can be part of an expression, but remember the parentheses, even if the subquery is an operand that provides an argument for a function. For example:
SELECT UPPER((SELECT s1 FROM t1)) FROM t2;PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5