Subqueries With ANY, IN, or SOME: ANY True True ANY s1 t1 s1 s1 t2 IN Any IN Any Some ANY
Subqueries With ANY, IN, or SOME: ANY True True ANY s1 t1 s1 s1 t2 IN Any IN Any Some ANY
The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison
is TRUE for ANY of the values in the column that the subquery returns.” For example:
SELECT s1 FROM t1 WHERE s1 > ANY (SELECT s1 FROM t2);
the word IN is an alias for = ANY. IN can take an expression list, but = ANY cannot
select * from rental where customer_id > any (select customer_id from customer where
customer_id>500);
The word ALL, which must follow a comparison operator, means “return TRUE if the comparison
is TRUE for ALL of the values in the column that the subquery returns.” For example:
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
SUB-QUERY ERRORS
This error occurs for statements where the subquery must return at most one row but returns
multiple rows. Consider the following example:
If SELECT column1 FROM t2 returns just one row, the previous query will work. If the subquery
returns more than one row, error 1242 will occur. In that case, the query should be rewritten as:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
This error occurs in cases such as the following, which attempts to modify a table and select from
the same table in the subquery:
Row Subqueries
A row subquery is a subquery variant that returns a single row and can thus return more than one
column value.
SELECT * FROM t1
WHERE (col1,col2) = (SELECT col3, col4 FROM t2 WHERE id = 10);
the following two statements are semantically equivalent (and are handled in the same way by
the optimizer):
The following query answers the request, “find all rows in table t1 that also exist in table t2”:
SELECT column1,column2,column3
FROM t1
WHERE (column1,column2,column3) IN
(SELECT column1,column2,column3 FROM t2);
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using
column names, column aliases, or column positions. Column positions are integers and begin
with 1
SELECT college, region, seed FROM tournament
ORDER BY 2, 3;
The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:
SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) > 10;
MySQL supports the following JOIN syntax for the table_references part
in MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace
each other). In standard SQL, they are not equivalent. INNER JOIN is used with
query FROM clause. For example, a subquery in a SELECT statement FROM clause is a derived
table:
SELECT ... FROM (subquery) [AS] tbl_name ...
Indexes can be used for views processed using the merge algorithm. However, a view
that is processed with the temptable algorithm is unable to take advantage of indexes
on its underlying tables (although indexes can be used during generation of the
temporary tables).
You can use DROP TABLE or ALTER TABLE to drop or alter a table that is used in a view
Aliases for column names in the SELECT statement are checked against the maximum
column length of 64 characters (not the maximum alias length of 256 characters).
ORDER BY is permitted in a view definition, but it is ignored if you select from a view using a
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
This statement creates and schedules a new event. The event will not run unless the Event
Scheduler is enabled.
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
| {VISIBLE | INVISIBLE}
index_type:
USING {BTREE | HASH}
algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
The best way to improve the performance of SELECT operations is to create indexes on one
or more of the columns that are tested in the query. The index entries act like pointers to
the table rows, allowing the query to quickly determine which rows match a condition in
the WHERE clause, and retrieve the other column values for those rows. All MySQL data types
can be indexed.
Although it can be tempting to create an indexes for every possible column used in a query,
unnecessary indexes waste space and waste time for MySQL to determine which indexes to
use. Indexes also add to the cost of inserts, updates, and deletes because each index must
be updated. You must find the right balance to achieve fast queries using the optimal set of
indexes.
Indexes are used to find rows with specific column values quickly. Without an index, MySQL
must begin with the first row and then read through the entire table to find the relevant rows.
The larger the table, the more this costs. If the table has an index for the columns in question,
MySQL can quickly determine the position to seek to in the middle of the data file without
having to look at all the data. This is much faster than reading every row sequentially.
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
| {VISIBLE | INVISIBLE}
index_type:
USING {BTREE | HASH}
algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}