Subqueries
IN subquery with keywords
Only the sub-query returns the results column contains a value comparison operator applies. If a sub-query returns a list of the result set value, then the comparison operator must be replaced with the IN operator.
IN operator can detect a specific value of the result set exists, and if the detection is successful query executed outside.
Example 1:
View table in infos score field contents there's a match info table information
SELECT * from info where score in ( SELECT score FROM infos);
search result:
Example 2:
View table in infos and info table with celebrity information
SELECT * from info where name in ( SELECT name FROM infos);
search result:
Subqueries with comparison operators
Subqueries can use comparison operators. These include comparison operators =,! =,>,> =, <, <=, Etc. Comparison operators when a subquery used very widely.
example:
SELECT * from info where age >= (SELECT age FROM infos where id =4);
search result:
Subqueries with EXISTS keyword
When using the EXISTS keyword, the inner query does not return records query. But return a true or false value. If the inner query query to the record satisfies the criteria, it returns a true value (true), otherwise, it returns a false value (false). When the return value is true, the outer query query; when the return is false, the outer query statement does not any query or a query record.
Example 1:
SELECT * from info where exists (SELECT * from infos where id =2);
search result:
Example 2:
SELECT * from info where exists (SELECT * from infos where id =27);
search result:
Subqueries with ANY keyword
ANY keyword indicates that either one condition is met. When using ANY keyword, as long as any inner query results returned in a statement, you can be performed by the outer query conditions.
example:
SELECT * from info where age > ANY(SELECT age from infos);
search result:
Subqueries with ALL keywords
ALL keyword indicates that all conditions are met. When you use the keyword ALL, only if all of the results returned by the inner query, the outer query can execute.
example:
SELECT * from info where age < all(SELECT age from infos);
search result:
Combined Queries
The combined results are the results of several SELECT statements merged together. The combined results of the query using UNION and UNION ALL keywords.
Example 1:
SELECT score from info UNION SELECT score from infos;
search result:
Example 2:
SELECT score from info UNION SELECT name from infos;
search result: