SQL Codes

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

SQL> create table market(item varchar(255), price int);

Table created.
SQL> desc market
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEM VARCHAR2(255)
PRICE NUMBER(38)
insert into market values('beans',50);

1 row created.

SQL> insert into market values('rice',70);

1 row created.

SQL> insert into market values('potatoes',40);

1 row created.

SQL> insert into market values('carrots',35);

1 row created.

SQL> insert into market values('chicken',120);

1 row created.

SQL> insert into market values('spices',20);

1 row created.

SQL> insert into market values('cucumber',60);

1 row created.

SQL> insert into market values('cabbage',20);

1 row created.

SQL> insert into market values('eggs',100);

1 row created.

SQL> insert into market values('pasta',65);

1 row created.

SQL> select * from market;

10 rows selected.

SQL> select * from markett;

ITEM PRICE
---------- ----------
pasta 65
eggs 100
cabbage 20
cucumber 60
spices 20
chicken 120
carrots 35
potatoes 40
rice 70
rice 70
beans 50

update markett set price=150 where price=100;

1 row updated.

SQL> select * from markett;

ITEM PRICE
---------- ----------
pasta 65
eggs 150
cabbage 20
cucumber 60
spices 20
chicken 120
carrots 35
potatoes 40
rice 70
rice 70
beans 50

SQL> alter table markett add quantity varchar(10);

Table altered.

SQL> select * from markett;

ITEM PRICE QUANTITY


---------- ---------- ----------
pasta 65
eggs 150
cabbage 20
cucumber 60
spices 20
chicken 120
carrots 35
potatoes 40
rice 70
rice 70
beans 50

11 rows selected.

SQL> UPDATE markett set quantity=5 where price=65;

1 row updated.

SQL> UPDATE markett set quantity=5 where price=150;


1 row updated.

SQL> UPDATE markett set quantity=5 where price=4;

0 rows updated.

SQL> UPDATE markett set quantity=5 where price=8;

0 rows updated.

SQL> UPDATE markett set quantity=7 where price=20;

2 rows updated.

SQL> UPDATE markett set quantity=4 where price=60;

1 row updated.

SQL> UPDATE markett set quantity=8 where price=120;

1 row updated.

SQL> UPDATE markett set quantity=6 where price=35;

1 row updated.

SQL> UPDATE markett set quantity=2 where price=40;

1 row updated.

SQL> UPDATE markett set quantity=9 where price=70;

2 rows updated.

SQL> UPDATE markett set quantity=3 where price=50;

1 row updated.

SQL> select * from markett;

ITEM PRICE QUANTITY


---------- ---------- ----------
pasta 65 5
eggs 150 5
cabbage 20 7
cucumber 60 4
spices 20 7
chicken 120 8
carrots 35 6
potatoes 40 2
rice 70 9
rice 70 9
beans 50 3

11 rows selected.

SQL> alter table markett drop column price;

Table altered.
SQL> select * from markett;

ITEM QUANTITY
---------- ----------
pasta 5
eggs 5
cabbage 7
cucumber 4
spices 7
chicken 8
carrots 6
potatoes 2
rice 9
rice 9
beans 3

11 rows selected.

SQL> truncate table markett;

Table truncated.

SQL> select * from markett;

no rows selected

SQL> insert into markett values('carrots',35);

1 row created.

SQL> select * from markett;

ITEM QUANTITY
---------- ----------
carrots 35

SQL> insert into markett values('potato',50);

1 row created.

SQL> insert into markett values('eggs',60);

1 row created.

SQL> insert into markett values('tomato',20);

1 row created.

SQL> insert into markett values('rice',90);

1 row created.

SQL> select*from markett;

ITEM QUANTITY
---------- ----------
carrots 35
potato 50
eggs 60
tomato 20
rice 90

SQL> alter table markett add price varchar(10);

Table altered.

SQL> select * from markett;

ITEM QUANTITY PRICE


---------- ---------- ----------
carrots 35
potato 50
eggs 60
tomato 20
rice 90

SQL> select sum(quantity) from markett;

SUM(QUANTITY)
-------------
255

SQL> select min(quantity) from markett;

MIN(QUANTI
----------
20

SQL> select max(quantity) from markett;

MAX(QUANTI
----------
90

SQL> select count('carrots') from markett;

COUNT('CARROTS')
----------------
5

SQL> connect system


Enter password:
Connected.
SQL> desc markett;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEM VARCHAR2(10)
QUANTITY VARCHAR2(10)
PRICE VARCHAR2(10)

SQL> select * from markett;

ITEM QUANTITY PRICE


---------- ---------- ----------
carrots 35
potato 50
eggs 60
tomato 20
rice 90

SQL> UPDATE markett set price=50 where quantity=35;

1 row updated.

SQL> UPDATE markett set price=95 where quantity=50;

1 row updated.

SQL> UPDATE markett set price=65 where quantity=60;

1 row updated.

SQL> UPDATE markett set price=40 where quantity=20;

1 row updated.

SQL> UPDATE markett set price=70 where quantity=90;

1 row updated.

SQL> select * from markett;

ITEM QUANTITY PRICE


---------- ---------- ----------
carrots 35 50
potato 50 95
eggs 60 65
tomato 20 40
rice 90 70

SQL> input into markett values('beans',55,67);


SQL>
SQL>
SQL> insert into markett values('beans',55,67);

1 row created.

SQL> insert into markett values('spices',66,80);

1 row created.

SQL> insert into market values('chicken',120);

1 row created.

SQL> select * from markett;

ITEM QUANTITY PRICE


---------- ---------- ----------
carrots 35 50
potato 50 95
eggs 60 65
tomato 20 40
rice 90 70
beans 55 67
spices 66 80

7 rows selected.

SQL> select * from markett where price=(select max(price) from markett);

ITEM QUANTITY PRICE


---------- ---------- ----------
potato 50 95

SQL> create table shopping(item varchar(10), price int, quantity int);

Table created.

SQL> desc shopping;


Name Null? Type
----------------------------------------- -------- ----------------------------
ITEM VARCHAR2(10)
PRICE NUMBER(38)
QUANTITY NUMBER(38)

SQL> insert into shopping select * from markett where item in (select item from
markett):
2
SQL>
SQL> insert into shopping (item) select item from markett;

7 rows created.

SQL> select * from shopping;

ITEM PRICE QUANTITY


---------- ---------- ----------
carrots
potato
eggs
tomato
rice
beans
spices

SQL> set serveroutput on;


SQL> DECLARE
2 a number := 50;
3 BEGIN
4 if(a < 60)
5 THEN
6 dbms_output.put_line('a is less than 60');
7 ELSE
8 dbms_output.put_line('a is greater than 60');
9 END if;
10 END;
11 /
a is less than 60

PL/SQL procedure successfully completed.


SQL> set serveroutput on;
SQL> DECLARE
2 a number := 100;
3 BEGIN
4 if (a > 80)
5 THEN
6 dbms_output.put_line('very good');
7 ELSE
8 dbms_output.put_line('just average');
9 END if;
10 END;
11 /
very good

PL/SQL procedure successfully completed.

SQL> set serveroutput on;


SQL> DECLARE
2 a integer := &a;
3 BEGIN
4 CASE a
5 when 1 then dbms_output.put_line('monday');
6 when 2 then dbms_output.put_line('tuesday');
7 when 3 then dbms_output.put_line('wednesday');
8 when 4 then dbms_output.put_line('thursday');
9 when 5 then dbms_output.put_line('friday');
10 when 6 then dbms_output.put_line('saturday');
11 when 7 then dbms_output.put_line('sunday');
12 ELSE
13 dbms_output.put_line('no date');
14 END CASE;
15 END;
16 /
Enter value for a: 5

You might also like