SubSELECTs Syntax Variants : Sub query « Select Clause « SQL / MySQL






SubSELECTs Syntax Variants

       
mysql>
The following query returns all book titles whose publisher's name begins with "O."
Then, a list of publID values for the key word IN is returned with a separate SELECT query.
mysql>
mysql>
mysql> CREATE TABLE titles (
    ->   titleID int(11),
    ->   title varchar(100),
    ->   subtitle varchar(100),
    ->   edition tinyint(4),
    ->   publID int(11),
    ->   catID int(11),
    ->   langID int(11),
    ->   year int(11),
    ->   isbn varchar(20),
    ->   comment varchar(255),
    ->   ts timestamp,
    ->   authors varchar(255),
    ->   PRIMARY KEY  (titleID)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql>
mysql>
mysql> INSERT INTO titles VALUES (1,'Linux','Installation',5,1,57,2,2000,NULL,NULL,'2005-02-28 13:34:21','Michael'),
    ->                           (2,'Excel',NULL,NULL,2,3,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','David'),
    ->                           (3,'XML',NULL,NULL,1,2,NULL,1997,NULL,NULL,'2005-02-28 13:34:22','Edwards'),
    ->                           (4,'PHP',NULL,NULL,3,6,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','Tom'),
    ->                           (5,'MySQL','',0,3,34,NULL,2000,'','','2005-02-28 13:34:22','Paul'),
    ->                           (6,'Java',NULL,NULL,4,34,NULL,1999,NULL,NULL,'2005-02-28 13:34:22','Tim');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> CREATE TABLE publishers (
    ->   publID int(11) NOT NULL auto_increment,
    ->   publName varchar(60) collate latin1_german1_ci NOT NULL default '',
    ->   ts timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
    ->   PRIMARY KEY  (publID),
    ->   KEY publName (publName)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql>
mysql> INSERT INTO publishers VALUES (1,'A','2004-12-02 18:36:58'),
    ->                                 (2,'Apress','2004-12-02 18:36:58'),
    ->                                 (3,'New Riders','2004-12-02 18:36:58'),
    ->                                 (4,'O\'Reilly & Associates','2004-12-02 18:36:58'),
    ->                                 (5,'Hanser','2004-12-02 18:36:58');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> SELECT title FROM titles WHERE publID IN
    -> (SELECT publID FROM publishers WHERE publName LIKE 'O%');
+-------+
| title |
+-------+
| Java  |
+-------+
1 row in set (0.00 sec)

mysql>
mysql> drop table titles;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table publishers;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Subqueries As Calculated Columns: Simple Subqueries
2.Subqueries in the WHERE Clause 2
3.Subqueries That Return Multiple Results
4.Sub queries
5.Sub query with string concatenate
6.Sub query with not equal and order
7.Sub query with calculation
8.Update command with sub query
9.Delete command with sub query
10.Uses a subquery to return an AuthID value
11.Use a not equal (<>) comparison operator in the WHERE clause to introduce the subquery
12.Subquery uses an aggregate function to arrive at a value that the outer statement can use
13.Nested subquery
14.Four-level nested subquery with alias
15.Nested subquery and where clause
16.Minus value from subquery
17.Subquery and equals operator(ERROR 1242 (21000): Subquery returns more than 1 row)
18.Row values and subquery
19.Nested three level subquery
20.Greater than the result of subquery
21.Equals to the result of subquery
22.Row values comparison for subquery
23.Select from the result of subquery
24.Value calculation of subquery
25.Subquery as a table
26.Alias subquery
27.Calculation in subquery
28.Using function with result from subquery
29.Create constant value in subquery
30.Calculation in subquery and use the result from outside
31.Equals operator with subquery
32.Less than and subquery(ERROR 1242 (21000): Subquery returns more than 1 row)
33.Less than data type value from subquery
34.<=> and subquery
35.Less than two values in subquery
36.Greater than two values in subquery
37.Get substring for a value from subquery
38.Mix constant value and subquery
39.Using the value from subquery with in operator
40.Pair value within in operator and subquery
41.In a list of value from subquery
42.In operator and subquery
43.Nested subquery and in operator
44.Not in and subquery
45.Between...and opertor and subquery
46.Count value in subquery
47.Add up count value from subquery
48.From a subquery
49.Aggregate function in subquery
50.Having clause with subquery
51.Count and subquery in Having clause
52.Subquery with having clause
53.Order by value from subquery
54.Subquery with limit clause
55.Insert statement with subquery
56.Use a derived table.
57.Searching for Titles Without Authors
58.Compare to aggregate function
59.Data calculation inside in operator
60.COMPARISON OPERATORS WITH SUBQUERIES
61.Select from three subqueries
62.Select from two subqueries
63.Row value comparison with select statement
64.Compare two data type value together
65.Nested subqueries
66.Two USING clauses, rather than ON clauses
67.Finding people who didn't send mail to themselves