Convert subqueries to JOINs : Join Select « Join « SQL / MySQL






Convert subqueries to JOINs

    
mysql>
mysql> CREATE TABLE jobs (
    ->   employee varchar(30)
    ->  ,title    varchar(30)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO jobs VALUES ('Gordon Russell','Lecturer');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO jobs VALUES ('Andrew Cumming','Teaching Fellow');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO jobs VALUES ('Jim Smith','Technician');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> CREATE TABLE ranks (
    ->   title     varchar(30)
    ->  ,rank    varchar(30)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO ranks VALUES ('Lecturer','LECT1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ranks VALUES ('Teaching Fellow','LECT2');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ranks VALUES ('Technician','TECH1');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> CREATE TABLE salary (
    ->   rank     varchar(30)
    ->  ,payment  DECIMAL(10,2)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO salary VALUES ('LECT1',2000.00);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO salary VALUES ('LECT2',3000.00);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO salary VALUES ('TECH1',5000.00);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO salary VALUES ('TECH2',6000.00);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> select * from jobs;
+----------------+-----------------+
| employee       | title           |
+----------------+-----------------+
| Gordon Russell | Lecturer        |
| Andrew Cumming | Teaching Fellow |
| Jim Smith      | Technician      |
+----------------+-----------------+
3 rows in set (0.00 sec)

mysql> select * from ranks;
+-----------------+-------+
| title           | rank  |
+-----------------+-------+
| Lecturer        | LECT1 |
| Teaching Fellow | LECT2 |
| Technician      | TECH1 |
+-----------------+-------+
3 rows in set (0.00 sec)

mysql> select * from salary;
+-------+---------+
| rank  | payment |
+-------+---------+
| LECT1 | 2000.00 |
| LECT2 | 3000.00 |
| TECH1 | 5000.00 |
| TECH2 | 6000.00 |
+-------+---------+
4 rows in set (0.00 sec)

mysql>
mysql> SELECT title FROM jobs WHERE employee = 'Andrew Cumming';
+-----------------+
| title           |
+-----------------+
| Teaching Fellow |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT rank FROM ranks WHERE title = 'Teaching Fellow';
+-------+
| rank  |
+-------+
| LECT2 |
+-------+
1 row in set (0.00 sec)

mysql> SELECT payment FROM salary WHERE rank = 'LECT2';
+---------+
| payment |
+---------+
| 3000.00 |
+---------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT payment FROM salary WHERE rank =
    ->   (SELECT rank FROM ranks WHERE title =
    ->     (SELECT title FROM jobs WHERE employee = 'Andrew Cumming'))
    -> ;
+---------+
| payment |
+---------+
| 3000.00 |
+---------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT payment FROM salary,ranks,jobs
    -> WHERE  salary.rank = ranks.rank
    -> AND    ranks.title = jobs.title
    -> AND    jobs.employee = 'Andrew Cumming'
    -> ;
+---------+
| payment |
+---------+
| 3000.00 |
+---------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT payment
    -> FROM salary JOIN ranks ON (salary.rank = ranks.rank)
    ->      JOIN jobs ON (ranks.title = jobs.title)
    -> WHERE jobs.employee = 'Andrew Cumming'
    -> ;
+---------+
| payment |
+---------+
| 3000.00 |
+---------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT salary.rank FROM salary
    -> WHERE rank NOT IN (SELECT rank FROM ranks)
    -> ;
+-------+
| rank  |
+-------+
| TECH2 |
+-------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT salary.rank
    -> FROM salary LEFT OUTER JOIN ranks ON (salary.rank = ranks.rank)
    -> WHERE ranks.rank IS NULL
    -> ;
+-------+
| rank  |
+-------+
| TECH2 |
+-------+
1 row in set (0.00 sec)

mysql>
mysql> DROP TABLE jobs;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE ranks;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE salary;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
  








Related examples in the same category

1.Simple Join two tables
2.JOINs Across Two Tables
3.JOINs Across Two Tables with link id
4.JOINs Across Three or More Tables
5.Table joins and where clause
6.Select distinct column value during table join
7.Select distinct column values in table join
8.Count joined table
9.Performing a Join Between Tables in Different Databases
10.LINESTRING type column: One or more linear segments joining two points; one-dimensional.
11.Select other columns from rows containing a minimum or maximum value is to use a join.
12.Retrieve the overall summary into another table, then join that with the original table:
13.Tests a different column in the book table to find the initial set of records to be joined with the author tab
14.Select the maximum population value into a temporary table, Then join the temporary table to the original one
15.Creating a temporary table to hold the maximum price, and then joining it with the other tables:
16.To display the authors by name rather than ID, join the book table to the author table
17.To display the author names, join the result with the author table
18.The summary is written to a temporary table, which then is joined to the cat_mailing table to produce the reco
19.Addition of a WHERE clause for table join
20.Join more than two tables together.
21.Creating Straight Joins: STRAIGHT_JOIN
22.Use the basic join syntax and you specify the STRAIGHT_JOIN table option in the SELECT clause
23.Creating Natural Joins
24.Joining Columns with CONCAT
25.A basic join
26.Rewriting Sub-selects as Joins
27.Display game code, name, price and vendor name for each game in the two joined tables
28.Get the player number, the sex, and the name of each player who joined the club after 1980.
29.Join two tables with char type columns
30.Join with another database
31.Join two tables with shared columns values
32.Qualify column name with table name during the table join
33.Three tables join together
34.Compare date type value during table join
35.Join on syntax
36.Natural join syntax
37.Join with Integer type column
38.Join three table together