HomeMySQL Page 3 - Using Subqueries In MySQL (part 1)
Turning The Tables - MySQL
One of the most powerful new features in MySQL 4.1 is subqueries, which make it possible to nest multiple SQL queries within each other. This introductory segment of a two-part tutorial demonstrates how to use this new feature to build sophisticated data retrieval queries, illustrating (among other things) how to use subqueries in WHERE and HAVING clauses, with comparison and logical operators, with aggregate functions, and with inner, left, right and self joins.
Before we begin, a quick introduction to the tables I'll be using throughout this tutorial seems to be in order. In order to explain subqueries, I'll be using a fictitious company's accounting database, which consists of the following four tables:
1. The "services" table: The fictitious company under discussion offers customers a number of outsourced services, each of which is associated with a fee and has a unique service ID. This information is stored in a "services" table, which looks like this:
2. The "clients" table: The company also has a list of its current clients stored in a separate "clients" table. Each client is identified with a unique customer ID.
3. The "branches" table: Each customer may have one or more branch offices. The "branches" table lists the branch offices per customer, together with each branch's location. Each branch has a description, a unique branch ID, and a foreign key reference to the customer ID.
mysql> SELECT * FROM branches;
+------+-----+--------------------------------+------+
| bid | cid | bdesc | bloc |
+------+-----+--------------------------------+------+
| 1011 | 101 | Corporate HQ | CA |
| 1012 | 101 | Accounting Department | NY |
| 1013 | 101 | Customer Grievances Department | KA |
| 1041 | 104 | Branch Office (East) | MA |
| 1042 | 104 | Branch Office (West) | CA |
| 1101 | 110 | Head Office | CA |
| 1031 | 103 | N Region HO | ME |
| 1032 | 103 | NE Region HO | CT |
| 1033 | 103 | NW Region HO | NY |
+------+-----+--------------------------------+------+
9 rows in set (0.01 sec)
4. The "branches_services" table: Services supplied to each branch office are listed in this table, which contains pairs of branch IDs and service IDs (foreign keys into the "branches" and "services" table respectively).
In order for you to try out the examples in this tutorial, you should recreate these tables in your development environment. Here are the SQL queries I used:
#
# Table structure for table `branches`
#
CREATE TABLE branches (
bid int(8) NOT NULL default '0',
cid tinyint(4) NOT NULL default '0',
bdesc varchar(255) NOT NULL default '',
bloc char(3) NOT NULL default ''
) TYPE=MyISAM CHARSET=latin1;
#
# Dumping data for table `branches`
#
INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1011, 101, 'Corporate
HQ', 'CA'); INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1012, 101,
'Accounting Department', 'NY'); INSERT INTO branches (bid, cid, bdesc, bloc)
VALUES (1013, 101, 'Customer Grievances Department', 'KA'); INSERT INTO branches
(bid, cid, bdesc, bloc) VALUES (1041, 104, 'Branch Office (East)', 'MA'); INSERT
INTO branches (bid, cid, bdesc, bloc) VALUES (1042, 104, 'Branch Office (West)',
'CA'); INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1101, 110, 'Head
Office', 'CA'); INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1031, 103,
'N Region HO', 'ME'); INSERT INTO branches (bid, cid, bdesc, bloc) VALUES (1032,
103, 'NE Region HO', 'CT'); INSERT INTO branches (bid, cid, bdesc, bloc) VALUES
(1033, 103, 'NW Region HO', 'NY');
#
# Table structure for table `branches_services`
#
CREATE TABLE branches_services (
bid int(8) NOT NULL default '0',
sid tinyint(4) NOT NULL default '0'
) TYPE=MyISAM CHARSET=latin1;
#
# Dumping data for table `branches_services`
#
INSERT INTO branches_services (bid, sid) VALUES (1011, 1); INSERT INTO branches_services
(bid, sid) VALUES (1011, 2); INSERT INTO branches_services (bid, sid) VALUES
(1011, 3); INSERT INTO branches_services (bid, sid) VALUES (1011, 4); INSERT
INTO branches_services (bid, sid) VALUES (1012, 1); INSERT INTO branches_services
(bid, sid) VALUES (1013, 5); INSERT INTO branches_services (bid, sid) VALUES
(1041, 1); INSERT INTO branches_services (bid, sid) VALUES (1041, 4); INSERT
INTO branches_services (bid, sid) VALUES (1042, 1); INSERT INTO branches_services
(bid, sid) VALUES (1042, 4); INSERT INTO branches_services (bid, sid) VALUES
(1101, 1); INSERT INTO branches_services (bid, sid) VALUES (1031, 2); INSERT
INTO branches_services (bid, sid) VALUES (1031, 3); INSERT INTO branches_services
(bid, sid) VALUES (1031, 4); INSERT INTO branches_services (bid, sid) VALUES
(1032, 3); INSERT INTO branches_services (bid, sid) VALUES (1033, 4);