4. SQL Statements : SELECT : SELECT Examples
 
Share this page                  
SELECT Examples
1. Find the amount of business that was billed, shipped, and returned in the last quarter of 2011:
SELECT   l_returnflag, l_linestatus, sum(l_quantity) as sum_qty,
         sum(l_extendedprice) as sum_base_price,
         sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
         sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
         avg(l_quantity) as avg_qty, avg(l_extendedprice) as avg_price,
         avg(l_discount) as avg_disc, count(*) as count_order
FROM     lineitem
WHERE    l_shipdate <= date '2011-12-01' - interval '90' day
GROUP BY l_returnflag, l_linestatus
ORDER BY l_returnflag, l_linestatus;
2. Find the top ten brands for sales orders placed in 2011:
SELECT FIRST 10 p.p_brand
, SUM(l_extendedprice) total_price
FROM lineitem l
INNER JOIN part p ON p.p_partkey = l.l_partkey
INNER JOIN orders o ON o.o_orderkey = l.l_orderkey
WHERE o.o_orderdate BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY p.p_brand
ORDER BY total_price DESC;