Home | Previous Page | Next Page   Composing Advanced SELECT Statements > Subqueries in SELECT Statements >

Subqueries in a Projection Clause

A subquery can occur in the projection list of another SELECT statement. Figure 300 shows how you might use a subquery in a projection clause to return the total shipping charges (from the orders table) for each customer in the customer table. You could also write this query as a join between two tables.

Figure 300. Query
SELECT customer.customer_num,
   (SELECT SUM(ship_charge) 
       FROM orders
       WHERE customer.customer_num = orders.customer_num) 
         AS total_ship_chg
   FROM customer
Figure 301. Query Result
customer_num total_ship_chg

         101         $15.30
         102
         103
         104         $38.00
         105
        
·
·
·
123 $8.50 124 $12.00 125 126 $13.00 127 $18.00 128
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]