Can EXISTS BE USED IN subquery?
Show 13.2.11.6 Subqueries with EXISTS or NOT EXISTS If a subquery returns any rows at all,
Traditionally, an For the preceding example, if
The last example is a double-nested In MySQL 8.0.19 and later, you can also use
The results are the same as when using Use the exists keyword with a subquery to test for the existence of some result from the subquery: {where | having} [not] exists (subquery) That is, the where clause of the outer query tests for the existence of the rows returned by the subquery. The subquery does not actually produce any data, but returns a value of TRUE or FALSE. For example, this query finds the names of all the publishers who publish business books: select pub_name from publishers where exists (select * from titles where pub_id = publishers.pub_id and type = "business") pub_name ---------------------------------------- New Age Books Algodata Infosystems (2 rows affected) To conceptualize the resolution of this query, consider each publisher’s name in turn. Does this value cause the subquery to return at least one row? In other words, does it cause the existence test to evaluate to TRUE? In the results of the preceding query, the second publisher’s name is Algodata Infosystems, which has an identification number of 1389. Are there any rows in the titles table in which pub_id is 1389 and type is business? If so, “Algodata Infosystems” should be one of the values selected. The same process is repeated for each of the other publisher’s names. A subquery that is introduced with exists is different from other subqueries, in these ways:
The exists keyword is very important, because there is often no alternative non-subquery formulation. In practice, a subquery introduced by exists is always a correlated subquery (see “Using correlated subqueries”). Although you cannot express some queries formulated with exists in any other way, you can express all queries that use in or a comparison operator modified by any or all with exists. Some examples of statements using exists and their equivalent alternatives follow. Here are two ways to find authors that live in the same city as a publisher: select au_lname, au_fname from authors where city = any (select city from publishers) select au_lname, au_fname from authors where exists (select * from publishers where authors.city = publishers.city) au_lname au_fname -------------- -------------- Carson Cheryl Bennet Abraham (2 rows affected) Here are two queries that find titles of books published by any publisher located in a city that begins with the letter “B”: select title from titles where exists (select * from publishers where pub_id = titles.pub_id and city like "B%") select title from titles where pub_id in (select pub_id from publishers where city like "B%") title --------------------------------------------------- You Can Combat Computer Stress! Is Anger the Enemy? Life Without Fear Prolonged Data Deprivation: Four Case Studies Emotional Security: A New Algorithm The Busy Executive’s Database Guide Cooking with Computers: Surreptitious Balance Sheets Straight Talk About Computers But Is It User Friendly? Secrets of Silicon Valley Net Etiquette (11 rows affected) How does a subquery with exists work?Use EXISTS to identify the existence of a relationship without regard for the quantity. For example, EXISTS returns true if the subquery returns any rows, and [NOT] EXISTS returns true if the subquery returns no rows. The EXISTS condition is considered to be met if the subquery returns at least one row.
What is the correct syntax for exists expression in subquery?Syntax: SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name(s) FROM table_name WHERE condition);
Which clauses can use a subquery?You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM clause. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator.
What to use instead of exists?continue.. endure.. happen.. occur.. prevail.. remain.. |