You are on page 1of 6

SQL> CREATE TABLE SCHOOL

2 ( Sno int,
3 Name char(20) NOT NULL,
4 Age int NOT NULL CHECK(Age>25),
5 Dept char(15) NOT NULL,
6 ID_no int NOT NULL PRIMARY KEY,
7 Sex char(8) DEFAULT'MALE' );

Table created.
SQL> DESCRIBE SCHOOL;
Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER(38)
NAME NOT NULL CHAR(20)
AGE NOT NULL NUMBER(38)
DEPT NOT NULL CHAR(15)
ID_NO NOT NULL NUMBER(38)
SEX CHAR(8)

SQL> INSERT INTO SCHOOL


2 VALUES(1,'PANKAJ',24,'COMPUTER',101,'MALE');

INSERT INTO SCHOOL


*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C003038) violated

SQL> INSERT INTO SCHOOL


2 VALUES(1,'PANKAJ',26,'COMPUTER',101,'MALE');

1 row created.

SQL> INSERT INTO SCHOOL


2 VALUES(2,'SHALINI',34,'ENGLISH',112,'');

1 row created.

SQL> INSERT INTO SCHOOL


2 VALUES(3,'SANJAY',30,'MATHS',110,'MALE');

1 row created.

SQL> INSERT INTO SCHOOL


2 VALUES( 4,'SUDHA',35,'POLSCIENCE',15,'DEFAULT');

1 row created.

SQL> INSERT INTO SCHOOL


2 VALUES( 5,'SONU',36,'HINDI',116,DEFAULT);

1 row created.

SQL> INSERT INTO SCHOOL


2 VALUES(6,'RAKESH',39,'ECONOMICS',105,'MALE');

1 row created.
SQL> SELECT * FROM SCHOOL;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
1 PANKAJ 26 COMPUTER 101 MALE
2 SHALINI 34 ENGLISH 112
3 SANJAY 30 MATHS 110 MALE
4 SUDHA 35 POLSCIENCE 115 DEFAULT
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

6 rows selected.

SQL> SELECT * FROM SCHOOL


2 WHERE SEX='MALE';

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- ------
1 PANKAJ 26 COMPUTER 101 MALE
3 SANJAY 30 MATHS 110 MALE
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

SQL> SELECT * FROM SCHOOL


2 ORDER BY AGE;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
1 PANKAJ 26 COMPUTER 101 MALE
3 SANJAY 30 MATHS 110 MALE
2 SHALINI 34 ENGLISH 112
4 SUDHA 35 POLSCIENCE 115 DEFAULT
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

6 rows selected.

SQL> SELECT * FROM SCHOOL


2 ORDER BY Age desc;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
6 RAKESH 39 ECONOMICS 105 MALE
5 SONU 36 HINDI 116 MALE
4 SUDHA 35 POLSCIENCE 115 DEFAULT
2 SHALINI 34 ENGLISH 112
3 SANJAY 30 MATHS 110 MALE
1 PANKAJ 26 COMPUTER 101 MALE

6 rows selected.

SQL> SELECT * FROM SCHOOL


2 WHERE Age BETWEEN 35 AND 40 ;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
4 SUDHA 35 POLSCIENCE 115 DEFAULT
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

SQL> SELECT * FROM SCHOOL


2 WHERE NAME LIKE'S%';

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
2 SHALINI 34 ENGLISH 112
3 SANJAY 30 MATHS 110 MALE
4 SUDHA 35 POLSCIENCE 15 DEFAULT
5 SONU 36 HINDI 116 MALE

SQL> SELECT AVG(AGE) FROM SCHOOL;

AVG(AGE)
----------
33.3333333

SQL> SELECT MAX(AGE) FROM SCHOOL;

MAX(AGE)
----------
39

SQL> SELECT MIN(AGE) FROM SCHOOL;

MIN(AGE)
----------
26

SQL> SELECT AGE*ID_NO FROM SCHOOL;

AGE*ID_NO
----------
2626
3808
3300
525
4176
4095

6 rows selected.

SQL> SELECT AGE/SNO FROM SCHOOL;

AGE/SNO
----------
26
17
10
8.75
7.2
6.5

6 rows selected.

SQL> SELECT DEPT,COUNT(*)


2 FROM SCHOOL
3 GROUP BY DEPT;

DEPT COUNT(*)
--------------- ----------
COMPUTER 1
ECONOMICS 1
ENGLISH 1
HINDI 1
MATHS 1
POLSCIENCE 1

6 rows selected.

SQL> SELECT SEX,COUNT(*)


2 FROM SCHOOL
3 GROUP BY SEX;

SEX COUNT(*)
-------- ----------
DEFAULT 1
MALE 4
1

SQL> SELECT SEX,COUNT(*),MIN(AGE)


2 FROM SCHOOL
3 GROUP BY SEX;

SEX COUNT(*) MIN(AGE)


-------- ---------- ----------
DEFAULT 1 35
MALE 4 26
1 34

SQL> SELECT AGE,COUNT(*)


2 FROM SCHOOL
3 GROUP BY AGE
4 HAVING AGE>29;

AGE COUNT(*)
---------- ----------
30 1
34 1
35 1
36 1
39 1

SQL> SELECT NAME,SEX


2 FROM SCHOOL
3 WHERE NAME LIKE'S%';

NAME SEX
-------------------- --------
SHALINI
SANJAY MALE
SUDHA DEFAULT
SONU MALE

SQL> UPDATE SCHOOL


2 SET ID_NO=115
3 WHERE ID_NO=15;

1 row updated.

SQL> UPDATE
2 SCHOOL
3 SET SEX='FEMALE'
4 WHERE SEX='DEFAULT';

1 row updated.

SQL> SELECT * FROM SCHOOL;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- -------
1 PANKAJ 26 COMPUTER 101 MALE
2 SHALINI 34 ENGLISH 112
3 SANJAY 30 MATHS 110 MALE
4 SUDHA 35 POLSCIENCE 115 FEMALE
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

6 rows selected.

SQL> DELETE FROM SCHOOL


2 WHERE ID_NO=112;

1 row deleted.

SQL> SELECT * FROM SCHOOL;

SNO NAME AGE DEPT ID_NO SEX


---------- -------------------- ---------- --------------- ---------- --------
1 PANKAJ 26 COMPUTER 101 MALE
3 SANJAY 30 MATHS 110 MALE
4 SUDHA 35 POLSCIENCE 115 FEMALE
5 SONU 36 HINDI 116 MALE
6 RAKESH 39 ECONOMICS 105 MALE

SQL> ALTER TABLE SCHOOL


2 ADD(SALARY DECIMAL(7,2));

Table altered.

SQL> UPDATE SCHOOL


2 SET SALARY=35000
3 WHERE ID_NO <=116;

5 rows updated.

SQL> CREATE VIEW RECORD


2 AS SELECT * FROM SCHOOL
3 WHERE AGE>=35;

View created.
SQL> SELECT * FROM RECORD;

SNO NAME AGE DEPT ID_NO SEX


SALARY
---------- -------------------- ---------- --------------- ---------- -------
---------

4 SUDHA 35 POLSCIENCE 115 FEMALE


35000

5 SONU 36 HINDI 116 MALE


35000

6 RAKESH 39 ECONOMICS 105 MALE


35000

SQL> UPDATE RECORD


2 SET SALARY=40000
3 WHERE AGE>=35;

3 rows updated.

SQL> SELECT * FROM RECORD;

SNO NAME AGE DEPT ID_NO SEX


SALARY

---------- -------------------- ---------- --------------- ---------- --------


-----------

4 SUDHA 35 POLSCIENCE 115 FEMALE


40000

5 SONU 36 HINDI 116 MALE


40000

6 RAKESH 39 ECONOMICS 105 MALE


40000

SQL> DROP VIEW RECORD;

View dropped.