Wednesday, March 30, 2011

More on Subqueries

The following examples of subqueries, using subqueries inside SELECT statements and in the FROM clause, use the Ice Cream Shop database located here.

Subqueries inside SELECT statements:
  1. /*  Using subquery in SELECT statement.
  2.     This example uses the IceCreamStore database.
  3. */
  4. SELECT (SELECT SUM(F.flavorID)) AS 'Varieties Available', B.Name AS 'Brand Name'
  5. FROM Flavors AS F
  6. INNER JOIN xBrandFlavorDistributors AS XFD
  7. ON Xfd.flavorID = F.flavorID
  8. INNER JOIN Brands AS B
  9. ON XFD.brandID = B.brandID
  10. GROUP BY B.Name;

Subqueries inside the FROM clause:
  1. /*  Using subquery in FROM clause.
  2.     This example uses the IceCreamStore database.
  3. */
  4. SELECT F.Name
  5. FROM (
  6.         SELECT * FROM Flavors
  7.      ) AS F

No comments:

Post a Comment

I Break Code Where code gets done.
ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |