Two USING clauses, rather than ON clauses : Sub query « Select Clause « SQL / MySQL






Two USING clauses, rather than ON clauses

       
mysql>
mysql> CREATE TABLE Books
    -> (
    ->     BookID SMALLINT NOT NULL PRIMARY KEY,
    ->     BookTitle VARCHAR(60) NOT NULL,
    ->     Copyright YEAR NOT NULL
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO Books VALUES
    -> (12786, 'Notebook', 1934),
    -> (13331, 'C++', 1919),
    -> (14356, 'Opera', 1966),
    -> (15729, 'Sql Server', 1932),
    -> (16284, 'C', 1996),
    -> (17695, 'Pascal', 1980),
    -> (19264, 'Postcards', 1992),
    -> (19354, 'Oracle', 1993);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE Authors
    -> (
    ->     AuthID SMALLINT NOT NULL PRIMARY KEY,
    ->     AuthFN VARCHAR(20),
    ->     AuthMN VARCHAR(20),
    ->     AuthLN VARCHAR(20)
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO Authors VALUES
    -> (1006, 'Hunter', 'S.', 'Thompson'),
    -> (1007, 'Joyce', 'Carol', 'Oates'),
    -> (1008, 'Black', NULL, 'Elk'),
    -> (1009, 'Rainer', 'Maria', 'Rilke'),
    -> (1010, 'John', 'Kennedy', 'Toole'),
    -> (1011, 'John', 'G.', 'Neihardt'),
    -> (1012, 'Annie', NULL, 'Proulx'),
    -> (1013, 'Alan', NULL, 'Watts'),
    -> (1014, 'Nelson', NULL, 'Algren');
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE AuthorBook
    -> (
    ->     BookID SMALLINT NOT NULL,
    ->     AuthID SMALLINT NOT NULL,
    ->     PRIMARY KEY (AuthID, BookID)
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO AuthorBook VALUES
    -> (1006, 14356),
    -> (1008, 15729),
    -> (1009, 12786),
    -> (1010, 17695),
    -> (1011, 15729),
    -> (1012, 19264),
    -> (1012, 19354),
    -> (1014, 16284);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> SELECT BookTitle, Copyright, CONCAT_WS(' ', AuthFN, AuthMN, AuthLN) AS Author
    -> FROM Books JOIN AuthorBook USING (BookID)
    -> JOIN Authors USING (AuthID)
    -> WHERE Copyright<1980
    -> ORDER BY BookTitle;
Empty set (0.00 sec)

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

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

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

mysql>
mysql>

   
    
    
    
    
    
    
  








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