Monday, 8 April 2013

GROUP BY, ORDER BY, HAVING IN SQL


SQL> create table product(cid int,pid int,pname varchar2(20),brandname varchar2(20),price int,quantity int,primary key(cid,pid),foreign key(cid)  references customer);
Table created.
SQL> insert into  product  values('&cid','&pid','&pname','&brandname','&price','&quantity');
Enter value for cid: 1
Enter value for pid: 1
Enter value for pname: x220
Enter value for brandname: sony
Enter value for price: 2500
Enter value for quantity: 5
1 row created.
SQL> select * from product;

CID     PID     PNAME     BRANDNAME     PRICE     QUANTITY
------    ------   ------------   ---------------------   ----------    ------------------
1          1           x220         sony                        2500                 5
2          2           xpro          sony                       10000               3
3          3           star 3        samsung                  7000                 1

SQL>select * from product order by quantity;
CID     PID     PNAME     BRANDNAME     PRICE     QUANTITY
------    ------   ------------   ---------------------   ----------    ------------------
3          3           star 3        samsung                  7000                 1
2          2           xpro          sony                       10000               3
1          1           x220         sony                        2500                 5




SQL>select quantity from product group by quantity;
QUANTITY
------------------
      1
      3
      5

SQL>select pname from product group by quantity having quantity>1;
PNAME    
------------
x220    
xpro     

No comments:

Post a Comment