Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL FAQs - SQL SELECT Statements with JOIN and Subqueries

By: FYIcenter.com

Part:   1  2  3  4   5 

(Continued from previous part...)

How To Use Subqueries in the FROM clause?

If you have a query returning many rows of data, and you want to perform another query on those rows, you can put the first query as a subquery in the FROM clause of the second query. A subquery used in this way become a temporary table, and you must provide a table alias name for the subquery as in "SELECT ... FROM (SELECT ...) aliasName". The following statement shows you how to use a subquery as base table for the main query:

mysql> SELECT * FROM (SELECT l.id, l.url, r.comment
   FROM fyi_links l LEFT OUTER JOIN fyi_rates r 
   ON l.id = r.id) WHERE url LIKE '%er%';
ERROR 1248 (42000): Every derived table must have its own alias

mysql> SELECT * FROM (SELECT l.id, l.url, r.comment
   FROM fyi_links l LEFT OUTER JOIN fyi_rates r 
   ON l.id = r.id) s WHERE s.url LIKE '%er%';
+-----+-------------------+-----------+
| id  | url               | comment   |
+-----+-------------------+-----------+
| 101 | dev.fyicenter.com | The best  |
| 102 | dba.fyicenter.com | Well done |
| 103 | sqa.fyicenter.com | Thumbs up |
| 107 | www.winrunner.com | NULL      |
+-----+-------------------+-----------+
4 rows in set (0.06 sec)

How To Count Groups Returned with the GROUP BY Clause?

If you use the COUNT(*) function on groups returned with the GROUP BY clause, it will count the number of rows within each group, not the number of groups. If you want to count the number of groups, you can put the GROUP BY query into a subquery and apply the COUNT(*) function on the main query as shown in the following tutorial exercise:

mysql> SELECT tag AS Category, YEAR(created) AS Year, 
   COUNT(*) AS Counts FROM fyi_links GROUP BY tag, 
   YEAR(created);
+----------+------+--------+
| Category | Year | Counts |
+----------+------+--------+
| DBA      | 2005 |      1 |
| DBA      | 2006 |      2 |
| DEV      | 2004 |      1 |
| DEV      | 2006 |      1 |
| SQA      | 2003 |      1 |
| SQA      | 2006 |      1 |
+----------+------+--------+
6 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM (
   SELECT tag AS Category, YEAR(created) AS Year, 
   COUNT(*) AS Counts FROM fyi_links GROUP BY tag, 
   YEAR(created) ) groups;
+----------+
| COUNT(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

How To Return Top 5 Rows?

If you want the query to return only the first 5 rows, you can use the LIMIT clause, which takes one parameter as the maximum number of rows to return. The following statement returns the first 5 rows from the fyi_links:

mysql> SELECT id, url, counts, tag FROM fyi_links 
   ORDER BY counts DESC LIMIT 5;
+-----+-------------------+--------+------+
| id  | url               | counts | tag  |
+-----+-------------------+--------+------+
| 107 | www.winrunner.com |      8 | SQA  |
| 105 | www.oracle.com    |      7 | DBA  |
| 103 | sqa.fyicenter.com |      6 | SQA  |
| 101 | dev.fyicenter.com |      4 | DEV  |
| 106 | www.php.net       |      4 | DEV  |
+-----+-------------------+--------+------+
5 rows in set (0.00 sec)

(Continued on next part...)

Part:   1  2  3  4   5 

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...